原版设计模式之工厂方法

Intent (定义)

Define an interface for creating an object(定义一个接口去创建对象), but let subclasses decide which class to instantiate.(但是由子类决定哪个类将被实例化) Factory Method lets a class defer instantiation to subclasses.(工厂方法让类的实例化推迟到子类中)

Also Known As (别名)

Virtual Constructor

Motivation (案例)

Consider a framework for applications that can present multiple documents to the user.(例如,一个向用户提供多种文档的应用程序)
Two key abstractions in this framework are the classes Application and Document.(在案例中有两个关键的抽象,Application和Document类) Both classes are abstract, and clients have to subclass them to realize their application-specific implementations. (这两个类都是抽象的,为了实现特定应用程序的功能,客户端需要实现他们的子类)
To create a drawing application(当要创建一个绘画程序时), for example, we define the classes DrawingApplication and DrawingDocument.(我们需要定义DrawingApplication和DrawingDocument)
The Application class is responsible for managing Documents and will create them as required—when the user selects Open or New from a menu, for example.(Application类用来管理文档,同时当用户从菜单中选择打开或者新建文档时,能根据需要创建Document)
Because the particular Document subclass to instantiate is application-specific, the Application class can’t predict the subclass of Document to instantiate (因为要实例化的是特殊的文档,而Application类并不知道要实例化哪个Document的子类)— the Application class only knows when a new document should be created, not what kind of Document to create.(Application 类仅仅知道,需要新建一份文档,而不知道需要创建哪种文档)
This creates a dilemma: The framework must instantiate classes, but it only knows about abstract classes, which it cannot instantiate.(这就面临一个困境,框架需要实例化类,但他仅仅知道一堆无法实例化的抽象类)

而工厂方法模式就是让类的实例化推迟到子类中。
在这里插入图片描述
Applicaiton依赖Document创建文档,并打开。

  • Application: 抽象的应用程序;
  • MyApplication: 具体的应用程序,createDocument就是工厂方法,它用于创建一个产品出来;
  • Document: 抽象的文档接口
  • MyDocument: 具体的某种文档

实现的案例代码如下:
工厂方法代码

Applicability (哪些情况适合用)

  • a class can’t anticipate the class of objects it must create.(一个类不能预知到必须创建的对象类)
  • a class wants its subclasses to specify the objects it creates.(一个类希望它的子类来创建对象)
  • classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.(类将职责分配给几个助手子类,并由你决定哪个助手子类)
    在这里插入图片描述
    Client依赖抽象的Creator来创建产品。

Participants (上述类说明)

  • Product (Document) 抽象产品
    • defines the interface of objects the factory method creates.(定义工厂方法创建对象的接口)
  • ConcreteProduct (MyDocument) 具体产品
    • implements the Product interface. (实现产品接口)
  • Creator (Application) 工厂方法
    • declares the factory method, which returns an object of type Product. (声明工厂方法,并返回产品对象)
      Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object. (Creator也可以定义一个用来返回一个默认具体产品的默认工厂方法。)
    • may call the factory method to create a Product object.(调用工厂方法并创建一个产品)
  • ConcreteCreator (MyApplication) 具体工厂方法实现
    • overrides the factory method to return an instance of a
      ConcreteProduct.(覆盖父类的工厂方法,并返回一个具体的产品)

Collaborations (约定)

  • Creator relies on its subclasses to define the factory method so that it returns an instance of the appropriate ConcreteProduct.(Creator依赖它的子类来定义具体的工厂方法,以便返回对应的具体产品)

Related Patterns (相关模式)

  • Abstract Factory (99) is often implemented with factory methods. The Motivation example in the Abstract Factory pattern illustrates Factory Method as well.(在抽象工厂中经常使用工厂方法,在抽象工厂模式的案例中也表明了这一点。)

  • Factory methods are usually called within Template Methods (360). In the document example above, NewDocument is a template method.(工厂方法通常在模板方法中调用,上述案例中的 newDocument方法就是一个模板方法。)

  • Prototypes (133) don’t require subclassing Creator. However, they often require an Initialize operation on the Product class. (原型方式不需要子类Creator,但是它通常需要对产品类有初始化操作–此处指Client中new一个产品出来)Creator uses Initialize to initialize the object. Factory Method doesn’t require such an operation. (Creator使用初始化方法去初始化对象,而工厂方法不需要如此操作。 此处翻译不怎么合理,但没想到该怎么解释

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值