Design patterns are useful for organizing a huge amount of code. since you don’t need to write as much code to do something in ruby as you do in # {verbose_algol_derivitive_language}, they do not have the same degree of importance.
What you will see is used all the time, this is a strategy and a builder implemented using blocks (an example of a constructor would be form_for blocks in rails representations, an example of a strategy would be File.open). I can’t think about the last time I saw any others (gof templates anyway)
EDIT: answer to
You mean with ruby, we don’t need to think about design patterns in most cases? Another question, if I use Rails, do I really need to think about design patterns? Because I don’t know where to use them. They do not seem to match any MVC component. Are design patterns only for people who build massive libraries / structures, for example. Rails, DataMapper, MongoID, etc., And not for others that use this framework / library?
, , , . - , ( ), GoF , , (java).
, - . , .
, , java, :
class StrategyExample {
public static void main(String[] args) {
Context context;
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);
}
}
interface Strategy {
int execute(int a, int b);
}
class ConcreteStrategyAdd implements Strategy {
public int execute(int a, int b) {
System.out.println("Called ConcreteStrategyAdd execute()");
return a + b;
}
}
class ConcreteStrategySubtract implements Strategy {
public int execute(int a, int b) {
System.out.println("Called ConcreteStrategySubtract execute()");
return a - b;
}
}
class ConcreteStrategyMultiply implements Strategy {
public int execute(int a, int b) {
System.out.println("Called ConcreteStrategyMultiply execute()");
return a * b;
}
}
class Context {
private Strategy strategy;
public Context(Strategy strategy) {
this.strategy = strategy;
}
public int executeStrategy(int a, int b) {
return strategy.execute(a, b);
}
}
, , , , , , .
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
b = Context.new { puts 'Doing the task alternatively' }
b.execute
c = Context.new { puts 'Doing the task even more alternatively' }
c.execute
, , ! , , . , , , java.