Design Pattern-factory method simple factory Abstract Factory Template Method

Source: Internet
Author: User

Simple factory mode:
You can create multiple types of products based on the input parameter type.
There is only one Creator method, which is used to create different products. The input parameter type is used to determine the product to be created, which is easy to implement. However, if you need to expand the product, you need to modify the implementation of the Creator method.
There is a Pizza store that can produce different types of Pizza. The Code is as follows:
// This is an abstract pizza with many different types inherited from it.

class AbstractPizza{public:AbstractPizza(void);virtual ~AbstractPizza(void);virtual void Taste() = 0;int m_nPizzaType;};

// The specific pizza inherits from the abstract pizza. There are other different types of Pizza.

Class SimplePizzaFactory {public: enum PizzaType {CheesePizzaTpe, GreekPizzaType, PipperPizzaType, //... can be added later}; SimplePizzaFactory (void );~ SimplePizzaFactory (void); AbstractPizza * CreatePizzaByType (PizzaType type) {switch (type) {case SimplePizzaFactory: CheesePizzaTpe: {return new CheesePizza; // if you want, you can do additional initialization for new CheesePizza; // like: // AbstractPizza * pizza = new CheesePizza; // assert (pizza); // pizza-> Initialize ();} case SimplePizzaFactory: GreekPizzaType: {return new GreekPizza; // if you want, you can do additional initialization for new versions;} case SimplePizzaFactory: PipperPizzaType: {return new PipperPizza; // if you want, you can do additional initialization for new CheesePizza;} default: break ;}}};


To better manage the Pizza creation process, we generally create a management class, which is responsible for creating different types of pizza products. Through the management of SimplePizzaFactory, we can pass in different types of pizza products. External callers only need to pass in the type, and the Creator method will return the correct actual type of AbstractPizza. You can even define SimplePizzaFactory as a singleton mode (if the factory needs data members) to make it easier to use global variables. You can also define CreatePizzaByType as a static method (if there is no relevant data member), so that you do not need to create any project object, simply SimplePizzaFactory: CreatePizzaByType (nPizzaType. Since it is called a simple factory, it is indeed simple enough. There is only one Creator method (but it cannot be called a factory method. Note that there is no inheritance). When a new product is added, you must modify the Creator method, to meet the needs of new products, but to be honest, this simple factory model is sufficient for many common programs.

Class SimplePizzaFactory {public: enum PizzaType {CheesePizzaTpe, GreekPizzaType, PipperPizzaType, //... can be added later}; SimplePizzaFactory (void );~ SimplePizzaFactory (void); AbstractPizza * CreatePizzaByType (PizzaType type) {switch (type) {case SimplePizzaFactory: CheesePizzaTpe: {return new CheesePizza; // if you want, you can do additional initialization for new CheesePizza; // like: // AbstractPizza * pizza = new CheesePizza; // assert (pizza); // pizza-> Initialize ();} case SimplePizzaFactory: GreekPizzaType: {return new GreekPizza; // if you want, you can do additional initialization for new versions;} case SimplePizzaFactory: PipperPizzaType: {return new PipperPizza; // if you want, you can do additional initialization for new CheesePizza;} default: break ;}}};

Factory method mode:
It is open to expansion and closed to modification. For the simple factory above, the creator method may need to be modified later. Therefore, we can separate it from the aspects to be modified. This constructs the factory method mode. There is an abstract factory method that unifies external calls. A specific factory is responsible for the product creation process. The factory method mode delays the product creation process to sub-classes. The sub-classes are responsible for creating specific products, and one factory only produces one product. The specific factory and product correspond to each other, and the abstract Creator method corresponds to the abstract product.
This is often used in abstract classes and defines a method for creating objects. Subclass can overwrite this method to define the specific object to be created. It is usually used with the template method and method. when defining the template algorithm, you generally need to create a product first, and then create a process using the product method.

An abstract product class can be derived from multiple specific product classes.
An abstract factory class can be derived from multiple factory classes.
Each factory class can only create one instance of a specific product class.

We also use the example above. Now we need an abstract Creator method. You can regard it as an abstract factory. It is actually a special abstract factory that produces only one product.
However, the following is a little more advanced than the common factory method mode. It is a Parameterized Factory method mode with greater flexibility.

Class PizzaFactory {public: enum PizzaType {CheesePizzaTpe, GreekPizzaType, PipperPizzaType,}; PizzaFactory (void );~ PizzaFactory (void); AbstractPizza * PizzaFactory (PizzaType type) () ;}; // a specific factory responsible for producing pizza of BeiJing. Class BeiJingPizzaFactory: public PizzaFactory {public: BeiJingPizzaFactory (void );~ BeiJingPizzaFactory (void); AbstractPizza * extract (PizzaType type) () {switch (type) {case PizzaFactory: condition: return new factory; case PizzaFactory: GreekPizzaType: return new factory; case PizzaFactory: PipperPizzaType: return new BeiJingPipperPizza; default: return NULL ;};}};

For BeiJingCheesePizza, BeiJingGreekPizza, and BeiJingPipperPizza, This is not written, that is, it inherits from the abstract Pizza.

Open to extension and disable modification:
If there is another Pizza in Tianjin, Henan, and Italy. By using the factory method, even if you have another pizza store in other regions, you only need to add another specific factory and product. Don't worry about adding a new PizzaType. What should I do? The factory method model focuses on products in new regions, rather than new PizzaType. Because the most basic factory method model is a product in a factory, which corresponds to each other one by one. If you want to focus on the type and ignore the region, you can consider a PizzaType and a Pizza Factory. if you add a new type, you can add a new factory and product, perfectly compliant with the open to the extension and closed to the modification.
Subclass instantiation:
The factory method mode provides a factory method to the outside, which is an abstract interface, so that the external dependency is abstract rather than concrete, reducing code coupling. Place the specific new operation in the subclass factory for implementation.
Application:
To be honest, it has been a long time to find an application. After all, it is difficult for us to get started with Pizza throughout the day. It is just to know the model, but not to see its great potential and value.
All who learn MFC know the relationship between Document and Application. Different applications correspond to different documents. The app is responsible for creating the doc. An app is a factory, a doc is a product, and a factory is a product.

// The App is a factory that stores the created product // Appclass Application {public: void OpenDocument () {// template method m_pDoc = CreateDocment (); m_pDoc-> Open (); m_pDoc-> Read ();} void CloseDocument () {// template method m_pDoc-> Write (); m_pDoc-> Close ();} private: virtual Document * CreateDocment () = 0; // factory method private: Document * m_pDoc; // This} // abstract product class. The Document can be opened, closed, and read/write. Class Document {public: void Open (); void Close (); void Read (); void Write ();}

Explanation:
As soon as the two classes above are seen, they will be excited and combine the template method and the factory method perfectly. The factory method is concerned with object creation, and the specific factory MyApp is responsible for creating a specific product MyDocument. at the same time, the template method OpenDocument is designed for the abstract Creation Interface and defines a set of algorithms. When the subclass inherits the Application, it inherits a set of algorithms. The subclass does not need to care about the steps of Algorithm Execution, sub-classes only need implementation methods. The parent class is responsible for calling sub-class methods.
As we often see in the state machine code, the jump process of the state machine is defined by a pre-defined process, but the implementation of methods such as OnStateEnter and OnStateExit is defined by a specific application. The parent class is responsible for calling the implementation of the OnStateEnter and OnStateExit sub-classes and typical application of the template method.

Abstract Factory:
Abstract Factory focuses on creating a series of products that are implemented using factory methods, but with multiple factory methods.
The code will not be written.
Summary (excerpt of a paragraph, I feel that the write is good, address http://stan001140.iteye.com/blog/1737817 ):
A simple factory can package specific implementations so that the client can truly achieve interface-oriented programming.
The factory method can be encoded at the top level, so that the product line of the server can truly achieve interface-oriented programming.
Abstract Factory can aggregate the entire product cluster, so that multiple product lines of the entire server can truly achieve interface-oriented programming.
The template method is also encoded at the upper level, and also interface-oriented programming.
However, factory methods and abstract factory methods focus on product abstraction, while template methods focus on abstract steps.
We usually use the two together.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.