在Ruby中实现的所有设计模式的备忘单? [英] Cheat sheet for all design patterns implemented in Ruby?

查看:75
本文介绍了在Ruby中实现的所有设计模式的备忘单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道Ruby中实现的所有设计模式是否都有欺骗性的热忱,因此您不必重新发明。

解决方案

设计模式可用于组织大量的代码。因为您不需要像#{verbose_algol_derivitive_language}中那样编写尽可能多的代码来执行ruby,所以它们的重要性不一样。



你将会看到什么使用所有时间是使用块实现的策略和构建器(构建器的一个例子是在rails视图中为form_for块,策略的一个例子是File.open)我不能真的想到上次我看到任何其他(gof模式无论如何)



编辑:回复


你的意思是用红宝石我们不需要
想想大多数
的设计模式?另一个问题,如果我使用
Rails,我实际上是否必须考虑
关于设计模式?因为我没有
知道在哪里使用它们。他们不会
似乎适合
MVC的任何组件。设计模式仅适用于正在构建大量
库/框架的
人员。 Rails,
DataMapper,MongoID等,而不是
只有使用这些
框架/库的其他人?


在大多数情况下,rails会为您做出很多决定,直到您的应用程序达到相当高的复杂程度。即使你正在使用像sinatra这样的东西(不会为你决定任何东西),你仍然不会真的需要达到这些GoF的模式,就像在一个像(例如)java这样的语言中一样。 / p>

这是因为设计模式的整体点是瓶装方式来保持灵活性和可维护性。如果这是内置的语言,通常它们甚至不需要。



例如,在java中实现的策略模式看起来像这样

  // StrategyExample测试应用程序

class StrategyExample {

public static void main(String [ ] args){

上下文上下文;

//遵循不同策略的三个上下文
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实现Strategy {

public int execute(int a, int b){
System.out.println(Called ConcreteStrategyAdd's execute());
返回a + b; //添加a和b
}

}

class ConcreteStrategySubtract implements Strategy {

public int execute(int a ,int b){
System.out.println(Called ConcreteStrategySubtract的execute());
返回a - b; //使用a和b
进行减法

}

class ConcreteStrategyMultiply实现Strategy {

public int execute(int a ,int b){
System.out.println(Called ConcreteStrategyMultiply's execute());
返回a * b; //与a和b进行乘法b

$ b}

//配置了一个ConcreteStrategy对象并维护对一个Strategy对象的引用
私人战略策略;

//构造函数
public Context(策略策略){
this.strategy = strategy;
}

public int executeStrategy(int a,int b){
return strategy.execute(a,b);
}

}

这是很多工作,但是你最终得到的是值得的很多时间,可以是一大块泥土之间的区别,还有一些在地狱中保持机会的东西。现在可以在ruby中执行

  class Context 
def initialize(& strategy)
@strategy =策略
end

def execute
@ strategy.call
end
end



a = Context.new {puts'做任务正常的方式'}
a.execute#=>执行任务正常方式

b = Context.new {puts'替代任务'}
b.execute#=>做任务

c = Context.new {puts'做更多的任务'}
c.execute#=>做更多的任务

它很难甚至调用一个模式,你只是使用块!当语言涵盖模式所需的需求时,有效地使用该语言将意味着您在大多数情况下并不真正需要该模式。这也意味着你可以优雅地解决这种问题,当做一个java风格的策略是可怕的。


I wonder if there are cheat cheets for all design patterns implemented in Ruby so that you don't have to reinvent the wheel.

解决方案

Design patterns are useful for organizing massive amounts of code. since you don't need to write as much code to do things in ruby as you do in #{verbose_algol_derivitive_language}, they don't have the same degree of importance.

What you will see used all the time is strategy and builder implemented with blocks (an example of builder would be form_for blocks in rails views, an example of strategy would be File.open) I can't really think of the last time I saw any others (gof patterns anyways)

EDIT: responding to

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?

For the most part, rails makes a lot of your decisions for you, and will until your app hits a fairly high level of complexity. Even if you are using something like sinatra though (which doesn't decide anything for you), you still won't really need to reach for those GoF patterns the same way as you would in a language like (for example) java.

This is because the whole point of design patterns is bottled ways to keep things flexible and maintainable. If that is built into the language, often they aren't even needed.

For example, a strategy pattern implemented in java looks sort of like this

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

}

It is a lot of work, but what you end up with is worth it a lot of the time, and can be the difference between a big ball of mud, and something that has a chance in hell of being maintained. Now lets do it in 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

its hard to even call that a pattern, you are just using blocks! When the language covers the needs that the pattern addresses, effectively using the language will mean you don't really need the pattern in most circumstances. It also means you can elegantly address that kind of problem when it would be horrible overkill to do a java style strategy.

这篇关于在Ruby中实现的所有设计模式的备忘单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆