Cheat sheet for all Ruby design patterns?

I wonder if there are cheats for all Ruby design templates, so you don’t need to reinvent the wheel.

+3
source share
2 answers

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, :

//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 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 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 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);
    }

}

, , , , , , .

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

, , ! , , . , , , java.

+6

GoF.

"cheatsheet", , - , , , , . , .

, , Ruby

+6

Source: https://habr.com/ru/post/1761605/


All Articles