Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Ruby中实现的所有设计模式的备忘单?_Ruby_Design Patterns - Fatal编程技术网

在Ruby中实现的所有设计模式的备忘单?

在Ruby中实现的所有设计模式的备忘单?,ruby,design-patterns,Ruby,Design Patterns,我想知道在Ruby中实现的所有设计模式是否都有欺骗手段,这样你就不必重新发明轮子。有一些来自GoF的设计模式的实际实现的好例子 它可能是一个“备忘单”,但我不认为它是设计模式的银弹参考,因为一旦你分析了一个问题,它们就会出现,并且模式适合你给出的解决方案。这不是一个食谱,而是一个很好的参考 当然,还有一本好书设计模式对于组织大量代码非常有用。由于在ruby中不需要像在#{verbose_algol_derivitive_language}中那样编写大量代码来完成任务,因此它们的重要性并不相同 您

我想知道在Ruby中实现的所有设计模式是否都有欺骗手段,这样你就不必重新发明轮子。

有一些来自GoF的设计模式的实际实现的好例子

它可能是一个“备忘单”,但我不认为它是设计模式的银弹参考,因为一旦你分析了一个问题,它们就会出现,并且模式适合你给出的解决方案。这不是一个食谱,而是一个很好的参考


当然,还有一本好书

设计模式对于组织大量代码非常有用。由于在ruby中不需要像在#{verbose_algol_derivitive_language}中那样编写大量代码来完成任务,因此它们的重要性并不相同

您将看到一直使用的是策略和用块实现的构建器(构建器的一个示例是rails视图中块的form_,策略的一个示例是File.open),我真的想不起我最后一次看到任何其他模式(无论如何,gof模式)

编辑:回应


你是说对ruby我们不必 考虑大多数情况下的设计模式 案例?另一个问题,如果我使用 Rails,我真的需要思考吗 关于设计模式?因为我不知道 知道在哪里使用它们。他们没有 似乎适合任何组件的 MVC。设计模式是否仅适用于 建造大型建筑的人们 库/框架,例如Rails, 数据映射器、MongoID等,不适用于 其他人认为只有使用这些 框架/库

在大多数情况下,rails会帮你做很多决定,直到你的应用程序达到相当高的复杂度。即使您使用的是sinatra之类的东西(这并不能为您决定任何事情),您仍然不需要像在java这样的语言中那样使用这些GoF模式

这是因为设计模式的全部要点是保持事物的灵活性和可维护性。如果这是语言中固有的,通常它们甚至不需要

例如,用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

这甚至很难称之为模式,你只是在使用块!当该语言涵盖了模式所满足的需求时,有效地使用该语言将意味着在大多数情况下您并不真正需要该模式。这也意味着您可以优雅地解决这类问题,如果采用java风格的策略,那将是一种可怕的过激行为。

我喜欢这本书的一半左右。另一半是对ruby的介绍,这对我来说没有多大意义。我的意思是,设计模式是一个相当高级的主题,你可以假设如果有人在寻找它,他们知道变量和类是如何工作的……很好的链接!这本书似乎没有对所有设计模式都有实现参考。快速查找非常好。是的,它对初学者很好,因为Ruby的介绍,你必须给它的作者推荐。这本书的畅销书《野性》部分也有“模式”。我喜欢的另一件事是,他以一种非常鲁比的方式处理模式。你的意思是,在大多数情况下,我们不必考虑设计模式?另一个问题,如果我使用Rails,我真的需要考虑设计模式吗?因为我不知道在哪里使用它们。它们似乎不适合MVC的任何组件。设计模式是否仅适用于正在构建大型库/框架(如Rails、DataMapper、MongoID等)的人,而不适用于仅使用这些框架/库的其他人?