- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我想知道在 Ruby 中实现的所有设计模式是否都有备忘单,这样您就不必重新发明轮子了。
最佳答案
设计模式对于组织大量代码很有用。因为在 ruby 中不需要像在#{verbose_algol_derivitive_language} 中那样编写那么多代码,所以它们的重要性程度不同。
你将看到的所有时间都是用 block 实现的策略和构建器(构建器的一个例子是 rails View 中的 form_for block ,一个策略的例子是 File.open)我可以真的想起了我上次看到其他人的时候(无论如何都是 gof 模式)
编辑:回应
You mean with ruby we don't have to think about design patterns in most cases? Another question, if I'm using Rails, do I actually have to think about design patterns? Cause I don't know where to use them. They don't seem to fit in any component of the MVC. Are design patterns only for people that are building massive libraries/frameworks eg. Rails, DataMapper, MongoID etc and not for others that only using these frameworks/libraries?
在大多数情况下,Rails 会为您做出很多决定,直到您的应用达到相当高的复杂度为止。即使你使用的是像 sinatra 这样的东西(它不会为你做任何决定),你仍然不需要像使用(例如)java 这样的语言那样使用那些 GoF 模式。
这是因为设计模式的全部意义在于保持灵 active 和可维护性的瓶装方式。如果这是语言内置的,通常甚至不需要它们。
例如,用 java 实现的策略模式看起来有点像这样
//StrategyExample test application
class StrategyExample {
public static void main(String[] args) {
Context context;
// Three contexts following different strategies
context = new Context(new ConcreteStrategyAdd());
int resultA = context.executeStrategy(3,4);
context = new Context(new ConcreteStrategySubtract());
int resultB = context.executeStrategy(3,4);
context = new Context(new ConcreteStrategyMultiply());
int resultC = context.executeStrategy(3,4);
}
}
// The classes that implement a concrete strategy should implement this
// The context class uses this to call the concrete strategy
interface Strategy {
int execute(int a, int b);
}
// Implements the algorithm using the strategy interface
class ConcreteStrategyAdd implements Strategy {
public int execute(int a, int b) {
System.out.println("Called ConcreteStrategyAdd's execute()");
return a + b; // Do an addition with a and b
}
}
class ConcreteStrategySubtract implements Strategy {
public int execute(int a, int b) {
System.out.println("Called ConcreteStrategySubtract's execute()");
return a - b; // Do a subtraction with a and b
}
}
class ConcreteStrategyMultiply implements Strategy {
public int execute(int a, int b) {
System.out.println("Called ConcreteStrategyMultiply's execute()");
return a * b; // Do a multiplication with a and b
}
}
// Configured with a ConcreteStrategy object and maintains a reference to a Strategy object
class Context {
private Strategy strategy;
// Constructor
public Context(Strategy strategy) {
this.strategy = strategy;
}
public int executeStrategy(int a, int b) {
return strategy.execute(a, b);
}
}
这是很多工作,但你最终得到的东西在很多时候都是值得的,这可能是一团泥泞和有机会维护的东西之间的区别。现在让我们用 ruby 来做
class Context
def initialize(&strategy)
@strategy = strategy
end
def execute
@strategy.call
end
end
a = Context.new { puts 'Doing the task the normal way' }
a.execute #=> Doing the task the normal way
b = Context.new { puts 'Doing the task alternatively' }
b.execute #=> Doing the task alternatively
c = Context.new { puts 'Doing the task even more alternatively' }
c.execute #=> Doing the task even more alternatively
甚至很难称之为模式,你只是在使用 block !当语言涵盖了模式解决的需求时,有效地使用该语言将意味着在大多数情况下您并不真正需要该模式。这也意味着您可以优雅地解决这类问题,而采用 Java 风格的策略可能会太过分了。
关于ruby - 在 Ruby 中实现的所有设计模式的备忘单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3570611/
我是一名优秀的程序员,十分优秀!