构建者设计模式和工厂设计模式之间的区别是什么?[英] What is the difference between Builder Design pattern and Factory Design pattern?

本文是小编为大家收集整理的关于构建者设计模式和工厂设计模式之间的区别是什么?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

建筑商设计模式与工厂设计模式有什么区别?

哪一个更有利,为什么?

如果要测试和比较/对比这些模式,我该如何表示我的发现作为图形?

推荐答案

使用设计模式,通常没有针对所有情况的"更有利的"解决方案.这取决于您需要实施的内容.

来自Wikipedia:

  • 建筑商专注于构建 复杂的对象逐步.抽象的 工厂强调产品家族 对象(简单或复杂). 建筑商将产品作为决赛返回 步骤,但就抽象而言 工厂关心,产品得到 立即返回.
  • 建筑商通常会建立一个复合材料.
  • 通常,设计开始使用工厂方法(较少复杂,更多 可自定义的子类扩散) 并发展为抽象工厂, 原型或构建器(更灵活, 更复杂)作为设计师 发现更多灵活性在哪里 需要.
  • 有时创建模式是互补的:建筑商可以使用一个 实施的其他模式 哪些组件被构建.抽象的 工厂,建筑商和原型可以 在他们的 实施.

Wikipedia用于工厂设计模式: http://en.wikipedia.org/wiki/wiki/factory_method_method_method_pattern

Wikipedia的建设者设计模式:

其他推荐答案

工厂只是围绕构造函数的包装功能(可能是另一个类别中的一个).关键区别在于,出厂方法模式需要在单个方法调用中构建整个对象,所有参数都在一行上传递.最终对象将返回.

另一方面,

构建器模式本质上是一个包装对象,围绕所有可能的参数,您可能想传递到构造方调用中.这使您可以使用Setter方法慢慢构建参数列表.构建器类上的另一种方法是build()方法,它只需将构建器对象传递到所需的构造函数中,然后返回结果.

在诸如Java之类的静态语言中,当您拥有少数(潜在的可选)参数时,这将变得更加重要,因为它避免了对所有可能的参数组合都具有望远镜构造函数的要求.此外,一个构建器允许您使用Setter方法来定义构造器后无法直接修改的仅读取或私有字段.

基本工厂示例

// Factory
static class FruitFactory {
    static Fruit create(name, color, firmness) {
        // Additional logic
        return new Fruit(name, color, firmness);
    }
}

// Usage
Fruit fruit = FruitFactory.create("apple", "red", "crunchy");

基本构建器示例

// Builder
class FruitBuilder {
    String name, color, firmness;
    FruitBuilder setName(name)         { this.name     = name;     return this; }
    FruitBuilder setColor(color)       { this.color    = color;    return this; }
    FruitBuilder setFirmness(firmness) { this.firmness = firmness; return this; }
    Fruit build() {
        return new Fruit(this); // Pass in the builder
    }
}

// Usage
Fruit fruit = new FruitBuilder()
        .setName("apple")
        .setColor("red")
        .setFirmness("crunchy")
        .build();

可能值得比较这两个Wikipedia页面的代码样本:

其他推荐答案

工厂模式几乎可以看作是构建器模式的简化版本.

在工厂模式中,工厂负责根据需求创建对象的各种子类型.

出厂方法的用户不需要知道该对象的确切子类型.工厂方法createCar的一个示例可能会返回Ford或Honda键入对象.

在构建器模式中,也通过构建器方法创建不同的子类型,但是对象的组成可能在同一子类中不同.

继续使用汽车示例,您可能会有一个createCar构建器方法,该方法使用4缸发动机创建Honda键入的对象,或带有6个圆柱体的Honda typed对象.构建器模式允许这种较细的粒度.

建筑商模式本文地址:https://www.itbaoku.cn/post/627521.html

问题描述

What is the difference between the Builder design pattern and the Factory design pattern?

Which one is more advantageous and why ?

How do I represent my findings as a graph if I want to test and compare/contrast these patterns ?

推荐答案

With design patterns, there usually is no "more advantageous" solution that works for all cases. It depends on what you need to implement.

From Wikipedia:

  • Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract Factory is concerned, the product gets returned immediately.
  • Builder often builds a Composite.
  • Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.
  • Sometimes creational patterns are complementary: Builder can use one of the other patterns to implement which components get built. Abstract Factory, Builder, and Prototype can use Singleton in their implementations.

Wikipedia entry for factory design pattern: http://en.wikipedia.org/wiki/Factory_method_pattern

Wikipedia entry for builder design pattern: http://en.wikipedia.org/wiki/Builder_pattern

其他推荐答案

A factory is simply a wrapper function around a constructor (possibly one in a different class). The key difference is that a factory method pattern requires the entire object to be built in a single method call, with all the parameters passed in on a single line. The final object will be returned.

A builder pattern, on the other hand, is in essence a wrapper object around all the possible parameters you might want to pass into a constructor invocation. This allows you to use setter methods to slowly build up your parameter list. One additional method on a builder class is a build() method, which simply passes the builder object into the desired constructor, and returns the result.

In static languages like Java, this becomes more important when you have more than a handful of (potentially optional) parameters, as it avoids the requirement to have telescopic constructors for all the possible combinations of parameters. Also a builder allows you to use setter methods to define read-only or private fields that cannot be directly modified after the constructor has been called.

Basic Factory Example

// Factory
static class FruitFactory {
    static Fruit create(name, color, firmness) {
        // Additional logic
        return new Fruit(name, color, firmness);
    }
}

// Usage
Fruit fruit = FruitFactory.create("apple", "red", "crunchy");

Basic Builder Example

// Builder
class FruitBuilder {
    String name, color, firmness;
    FruitBuilder setName(name)         { this.name     = name;     return this; }
    FruitBuilder setColor(color)       { this.color    = color;    return this; }
    FruitBuilder setFirmness(firmness) { this.firmness = firmness; return this; }
    Fruit build() {
        return new Fruit(this); // Pass in the builder
    }
}

// Usage
Fruit fruit = new FruitBuilder()
        .setName("apple")
        .setColor("red")
        .setFirmness("crunchy")
        .build();

It may be worth comparing the code samples from these two wikipedia pages:

http://en.wikipedia.org/wiki/Factory_method_pattern
http://en.wikipedia.org/wiki/Builder_pattern

其他推荐答案

The Factory pattern can almost be seen as a simplified version of the Builder pattern.

In the Factory pattern, the factory is in charge of creating various subtypes of an object depending on the needs.

The user of a factory method doesn't need to know the exact subtype of that object. An example of a factory method createCar might return a Ford or a Honda typed object.

In the Builder pattern, different subtypes are also created by a builder method, but the composition of the objects might differ within the same subclass.

To continue the car example you might have a createCar builder method which creates a Honda-typed object with a 4 cylinder engine, or a Honda-typed object with 6 cylinders. The builder pattern allows for this finer granularity.

Diagrams of both the Builder pattern and the Factory method pattern are available on Wikipedia.