红宝石图案问题

发布于 2024-10-12 17:16:02 字数 643 浏览 2 评论 0原文

抱歉这个蹩脚的标题:我不知道如何总结这个......

我有一组类是公式(或公式,如果你喜欢),以及其他类集(称为外部类),它们将利用它们。

公式类有许多属性(比如大约 20 个)和一个计算函数。外部类是持久性类,因此具有公式类的所有属性以及它自己的更多属性。

在我的系统中,用户可以配置要使用的公式类,并且实际上可以选择使用多个公式进行计算,例如用于比较报告。

我正在尝试弄清楚如何在内部/公式类和外部/持久性类之间传输属性值,而无需一行又一行的 inner.x = external.x 代码。

在我看来,我不能使用:

class Outer
   include Formula1
end

...因为我希望实际的 Formula 类是可配置的。

我想到的一个想法是,我可以有一组属性从外部类传递下来,然后循环它们并发送,如下所示:

# not tested
['x', 'y', 'z'].each{|a|@formula.send("#{a.to_sym}=", self.send("#{a.to_sym}") }

我应该考虑的任何其他红宝石魔法或模式?

谢谢,

Sorry for the lame title: I'm not sure how to sum this one up...

I have a set of classes that are formulas (or formulae if you like), and other set of classes (call them outer classes) that will utilize them.

The formula classes have many attributes (say about 20) and a calculate function. The outer class is a the persistence class and therefore has all the attributes of the formulae class and several more of its own.

In my system, users can configure which formulae class to use, and indeed may chose to calculate using more than one formula for a comparison report for example.

I'm trying to work out how I can transfer attribute values between the inner/formula class and the outer/persistence class without line after line of inner.x = outer.x code.

It seems to me that I can't use:

class Outer
   include Formula1
end

...because I want the actual Formula class to be configurable.

One idea that springs to mind is I could have an array of attributes to pass down from my outer class, and loop over them and send, something like this:

# not tested
['x', 'y', 'z'].each{|a|@formula.send("#{a.to_sym}=", self.send("#{a.to_sym}") }

Any other ruby magic or patterns I should be considering?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒中人 2024-10-19 17:16:02

听起来您正在寻找一个策略模式来插入公式,如果您不想通过公式实例调用公式的属性,则可能需要一个外观。

制定策略的简单方法是将 Formula 的实例作为 Outer 的属性之一:

class Outer

  attr_accessor :formula #this bit is Strategy

  def x #this bit is Facade
    @formula.x
  end

  def calculate #Facade
    @formula.calculate
  end

end

outer1 = Outer.new

outer1.formula = Formula17.new

#this is the same
outer1.formula.x = 2
outer1.x = 2

#these are the same
puts outer1.formula.x
puts outer1.x

#and these are the same
puts outer1.formula.calculate
puts outer1.calculate

#and this would be the same as the above
formula17 = Formula17.new
formula17.x = 2
outer1 = Outer.new
outer1.formula = formula17

为了处理大量访问器,也许您可​​以尝试缺少方法来调用,或者在调用方法时为您定义和调用方法?

def method_missing(meth, *args, &block)
  if @formula.respond_to?(meth)
    @formula.send(meth, *args, &block)
  end
  else
    super(meth, *args, &block)
  end
end

Sounds like you're look for a Strategy pattern to plug in the formula and then maybe a Facade if you don't want to call the formula's attribute via the formula instance.

Easy way to make a Strategy is to make an instance of Formula one of Outer's attributes:

class Outer

  attr_accessor :formula #this bit is Strategy

  def x #this bit is Facade
    @formula.x
  end

  def calculate #Facade
    @formula.calculate
  end

end

outer1 = Outer.new

outer1.formula = Formula17.new

#this is the same
outer1.formula.x = 2
outer1.x = 2

#these are the same
puts outer1.formula.x
puts outer1.x

#and these are the same
puts outer1.formula.calculate
puts outer1.calculate

#and this would be the same as the above
formula17 = Formula17.new
formula17.x = 2
outer1 = Outer.new
outer1.formula = formula17

to deal with lots of accessors perhaps you could try method missing to either call, or define and call the methods for you as they're called?

def method_missing(meth, *args, &block)
  if @formula.respond_to?(meth)
    @formula.send(meth, *args, &block)
  end
  else
    super(meth, *args, &block)
  end
end
素衣风尘叹 2024-10-19 17:16:02

我的 ruby​​-fu 很低,我可能错过了您要求的一些微妙之处,所以这可能不是最好的选择,但是您可以有一个模块,您可以在其上定义属性,然后将该模块包含在您的 Outer 和 Formula 类中,像这样:

module FormulaAttributes
  attr_accessor :x, :y ...
end

class Formula1
  include FormulaAttributes
end

class Outer1
  include FormulaAttributes
  attr_accessor :a, :b ...
end

My ruby-fu is low and I am probably missing some subtlety of your requirement, so this is probably not the best option, but you could have a module on which you define the attributes and then include that module in your Outer and Formula classes, like this:

module FormulaAttributes
  attr_accessor :x, :y ...
end

class Formula1
  include FormulaAttributes
end

class Outer1
  include FormulaAttributes
  attr_accessor :a, :b ...
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文