工厂方法设计模式 [英] factory method design pattern

查看:120
本文介绍了工厂方法设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据这本书:


Factory Pattern的本质是为
定义一个界面,创建一个对象,但让子类决定哪个类到
实例化。Factory方法允许类将实例化推迟到
子类。

The essence of the Factory Pattern is to "Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses.



说我有一个创作者类:

Say I have a Creator class:

class Product; //this is what the Factory Method should return
class Creator {
    public:
        Creator()   //ctor
        { //... }

        virtual Product make(//args)
        { //... }
}

好的,那是我的创作者类,但是我不明白

Ok, that's my Creator class, but I don't understand


Factory方法让一个类将实例化推迟到子类

The Factory method lets a class defer instantiation to subclasses

与子类有什么关系?
我应该使用子类?

What does it have to do with subclasses? And what am I supposed to use subclasses for?

任何人都可以给我一些例子?

Anyone can give me some example?

推荐答案

您的创作者类是工厂。让我们称之为 ProductFactory ,以使该示例更加明确。

Your Creator class is the factory. Let's call it ProductFactory, in order to make the example more explicit.

(我假设你使用C ++)

class Book : public Product
{
};

class Computer : public Product
{
};

class ProductFactory
{
public:
  virtual Product* Make(int type)
  {
    switch (type)
    {
      case 0:
        return new Book();
      case 1:
        return new Computer();
        [...]
    }
  }
}

调用如下:

ProductFactory factory = ....;
Product* p1 = factory.Make(0); // p1 is a Book*
Product* p2 = factory.Make(1); // p2 is a Computer*
// remember to delete p1 and p2

所以,回答你的问题:


它与子类有什么关系?我应该使用
子类?

What does it have to do with subclasses? And what am I supposed to use subclasses for?

工厂模式的定义是工厂定义用于创建某种类型(通常是接口或抽象类)的实例的通用API,但返回的实现的实际类型(因此是子类引用)是工厂的责任。在该示例中,工厂返回产品实例,其中计算机是有效的子类。

What the definition for factory pattern is saying is that the factory defines a common API for creating instances of a certain type (normally an interface or abstract class), but the real type of the implementations returned (thus the subclass reference) is the responsibility of the factory. In the example, the factory returns Product instances, for which Book and Computer are valid subclasses.

工厂还有其他成语,就像有工厂的API和工厂的具体实现接受一个类型像我的例子,但它们与返回的实例类型相结合,如下所示:

There are other idioms for factory, like having an API for the factory and the concrete implementations of the factory do not accept a type like in my example, but they are coupled with the type of instances returned, like this:

class ProductFactory
{
public:
  virtual Product* Make() = 0;
}

class BookProductFactory : public ProductFactory
{
public:
    virtual Product* Make()
    {
      return new Book();
    }
}

在这个类 BookProductFactory 总是返回实例。

In this class BookProductFactory always returns Book instances.

ProductFactory* factory = new BookProductFactory();
Product* p1 = factory->Make(); // p1 is a Book
delete p1;
delete factory;






为了说清楚,因为似乎有在抽象工厂工厂方法设计模式之间有点混乱,我们来看一个具体的例子:


To make it clear, since there seems to be a bit of confusion between Abstract Factory and Factory method design patterns, let's see a concrete example:

使用抽象工厂

class ProductFactory {
protected:
  virtual Product* MakeBook() = 0;
  virtual Product* MakeComputer() = 0;
}

class Store {
public:
   Gift* MakeGift(ProductFactory* factory) {
     Product* p1 = factory->MakeBook();
     Product* p2 = factory->MakeComputer();
     return new Gift(p1, p2);
   }
}

class StoreProductFactory : public ProductFactory {
protected:
  virtual Product* MakeBook() { return new Book(); }
  virtual Product* MakeComputer() { return new Computer(); }
}

class FreeBooksStoreProductFactory : public StoreProductFactory {
protected:
  virtual Product* MakeBook() {
    Book* b = new FreeBook(); // a FreeBook is a Book with price 0
    return b;
  }
}

这样使用:

Store store;
ProductFactory* factory = new FreeBooksStoreProductFactory();
Gift* gift = factory->MakeGift(factory);
// gift has a FreeBook (Book with price 0) and a Computer
delete gift;
delete factory;

使用工厂方法

class Store {
public:
   Gift* MakeGift() {
     Product* p1 = MakeBook();
     Product* p2 = MakeComputer();
     return new Gift(p1, p2);
   }

 protected:
   virtual Product* MakeBook() {
     return new Book();
   }

   virtual Product* MakeComputer() {
     return new Computer();
   }
}

class FreeBooksStore : public Store {
protected:
  virtual Product* MakeBook() {
    Book* b = new FreeBook(); // a FreeBook is a Book with price 0
    return b;
  }
}

这样使用:

Store* store = new FreeBooksStore();
Gift* gift = store->MakeGift();
// gift has a FreeBook (Book with price 0) and a Computer
delete gift;
delete store;

当您使用一个类型在原始示例中,我们使用参数化工厂方法 - 一种知道如何创建不同类型的对象的方法。但是可以出现在抽象工厂工厂方法模式中。一个简单的技巧:如果你是扩展工厂类,你正在使用抽象工厂。如果您使用创建方法扩展类,那么您正在使用工厂方法。

When you use a type discriminator like I did in the original example, we are using parametized factory methods - a method that knows how to create different kind of objects. But that can appear in either an Abstract Factory or Factory Method pattern. A brief trick: if you are extending the factory class you are using Abstract Factory. If you extending the class with the creation methods, then you are using Factory Methods.

这篇关于工厂方法设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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