工厂模式和生成器模式(混合两种或多种模式)



我知道这个问题被问了很多次,看起来这个问题可能是重复的,但我试图理解SO,谷歌,GoF上的帖子,但没有找到答案给我...

我了解工厂方法和生成器之间的区别:工厂方法 - 创建派生自特定基类的对象,将具体实现与客户端分离

生成器方法 - 对客户端隐藏对象创建的复杂性

现在我看到了我在互联网上找到的示例(依赖注入的抽象工厂设计模式),它是关于抽象工厂的,但这对我的问题无关紧要。这只是我查看的众多文章之一

//Abstract Factory for Dependency Injection
//Factory interface
public interface Module1ServiceFactory {
 ComponentA getComponentA();
 ComponentB getComponentB();
}
//Concrete factory
public class Module1ServiceFactoryImpl {
 private Module1ServiceFactory instance;
 private Module1ServiceFactoryImpl() {}
 public static synchronized Module1ServiceFactory getInstance() {
  if (null == instance) {
   instance = new Module1ServiceFactoryImpl();
  }
return instance;
 }
*** SUBJECT METHOD ***
 public ComponentA getComponentA() {
  ComponentA componentA = new ComponentAImpl();
  ComponentB componentB = getComponentB();
  componentA.setComponentB(componentB);
  return componentA;
 }
 public ComponentB getComponentB() {
  return new ComponentBImpl();
 }
}

我在那个例子中看到的,ComponentA 是一个复杂的类型,getComponentA() 方法用来构建它。

  1. 那么为什么这被称为工厂而不是建造者呢???或者这意味着 Module1ServiceFactoryImpl 实现了 Factory AND Builder 模式?
  2. 在软件架构设计中创建实现多个设计模式的类/对象是否正确(常用)?

对不起我的英语:)

那么为什么这被称为工厂而不是建造者呢???或者这意味着 Module1ServiceFactoryImpl 实现 Factory AND Builder 模式?

它被称为工厂,因为它是工厂,而且只有工厂

*** SUBJECT METHOD ***
 public ComponentA getComponentA() {
  ComponentA componentA = new ComponentAImpl();
  ComponentB componentB = getComponentB();
  componentA.setComponentB(componentB);
  return componentA;
 }
 public ComponentB getComponentB() {
  return new ComponentBImpl();
 }

我在这里看不到任何构建器。当然,这不是最简单的方法,但如果引入构建器模式,它绝对不会那么复杂。这更像是JavaBean方式,并在新创建的对象上使用setter。生成器模式要求在最后一步中创建对象,以使创建的对象无法处于即用型和尚未就绪之间的状态。

创建以下类/对象是否正确(常用) 在软件架构中实现多个设计模式 设计?

是的,有时它是正确的和使用过的。这并不常见,因为每种模式都会增加一些复杂性,而多种模式会增加多种复杂性:)但是,当然,您可以将构建器与工厂混合使用(构建器需要一些被分解的部分),工厂与构建器混合使用(工厂封装使用构建器创建对象的用法)。模式在协同工作方面很擅长。MVC是一个很好的例子:

GoF(四人帮)并没有将MVC称为设计模式,而是将其视为"构建用户界面的一组类"。在他们看来,它实际上是其他三种经典设计模式的变体:观察者(Pub/Sub),策略和复合模式。根据 MVC 在框架中的实现方式,它还可能使用工厂和装饰器模式。

http://addyosmani.com/blog/understanding-mvc-and-mvp-for-javascript-and-backbone-developers/

工厂模式基本上隐藏了实例化具体对象(与构造函数不同),而是强调所创建对象的接口

相反,构建

器模式通常有助于实现多步骤构建行为,其中某些步骤也可能是可选的。它强调将对象创建拆分为逻辑单元,而不是一次在语义上创建它,这与构造函数(或工厂方法)不同。

我希望以下示例可能会更清楚地表达差异。

public class ComponentABuilder {
  private ComponentB componentB;
  private ComponentC componentC;
  public ComponentABuilder BuildComponentB(ComponentB b) {
    this.componentB = b;
  }
  public ComponentABuilder BuildComponentC(ComponentC c) {
    // Added another ComponentC, since just building B would
    // be pointless
    this.componentC = c;
  }
  public ComponentA Build() {
    // Might also be implemented via setters or whatever, the
    // point is that build now returns an object that is fully
    // built according to the steps the consumer performed via
    // the builder's methods, whereas a Factory Method would
    // do all the stuff by theirself.
    return new ComponentA(this.componentB, this.componentC);
  }
}
// Consumer sticks together his very own ComponentA
// in (usually) multiple steps
ComponentA c = new ComponentABuilder()
  .BuildComponentB(new ComponentBImpl()) // step 1
  .BuildComponentB(new ComponentCImpl()) // step 2
  .Build();
// Factory builds their ComponentA according to their
// own internal application, and getInstance is exposed
// as an "atomic" step to the consumer
ComponentA c2 = new ComponentAFactory().getInstance();

生成器示例是一个相当特殊的情况,应用它是有意义的。但是,它也应该可用于不同的应用程序。

只是我的两分钱:

  1. 模块1ServiceFactoryImpl似乎正在实现单例模式,所有字段和构造函数都是私有的,并且 getInstance() 用于获取 self 的新实例。
  2. 如果 Module1ServiceFactoryImpl 实现了 Module1ServiceFactory,这可能是工厂模式。然后,客户端代码可以使用 工厂如下。
class Client {
    public static void main(String[] args) {
        Module1ServiceFactory factory = Module1ServiceFactoryImpl.getInstance();
        ComponentA compA = factory.getComponentA();
        compA.printComponentA();
        ComponentB compB = factory.getComponentB();
        compB.printComponentB();
    }
}
class Module1ServiceFactoryImpl implements Module1ServiceFactory {
    private static Module1ServiceFactory instance;
    private Module1ServiceFactoryImpl() {
    }
    public static synchronized Module1ServiceFactory getInstance() {
        if (null == instance) {
            instance = new Module1ServiceFactoryImpl();
        }
        return instance;
    }
    public ComponentA getComponentA() {
        ComponentA componentA = new ComponentAImpl();
        ComponentB componentB = getComponentB();
        componentA.setComponentB(componentB);
        return componentA;
    }
    public ComponentB getComponentB() {
        return new ComponentBImpl();
    }
}
interface Module1ServiceFactory {
    ComponentA getComponentA();
    ComponentB getComponentB();
}
interface ComponentA {
    void setComponentB(ComponentB componentB);
    void printComponentA();
}
class ComponentAImpl implements ComponentA {
    ComponentB componentB;
    @Override
    public void setComponentB(ComponentB componentB) {
        this.componentB = componentB;
    }
    @Override
    public void printComponentA() {
        System.out.println("ComponentA");
    }
}
interface ComponentB {
    public void printComponentB();
}
class ComponentBImpl implements ComponentB {
    @Override
    public void printComponentB() {
        System.out.println("ComponentB");
    }
}

最新更新