Simple Factory mode, factory method mode and abstract Factory mode-Design pattern learning

Source: Internet
Author: User

1. Simple Factory mode

The Simple Factory mode is a staticfactory mode, also called the Static factory method mode, but not one of the 23 gof design patterns. A simple factory model is a factory object that determines which product class instances are created. Simple Factory mode is the simplest and most practical mode in the factory model family, which can be understood as a special implementation of different factory patterns.

Factory (Creator) role

The core of the simple factory pattern, which is responsible for implementing the internal logic of creating all instances. The method of creating a product class for a factory class can be called directly by the outside world to create the desired product object.

Abstract Product Role

The parent class of all objects created by the simple factory pattern, which is responsible for describing the common interfaces common to all instances.

Specific products (concrete product) roles

is the creation target of the simple factory pattern, and all created objects are instances of a specific class that acts as the role.

Advantages

The factory class is the key to the whole pattern. Contains the necessary logical judgments, based on the information given by the outside world, to determine exactly what specific class of objects should be created. By using the factory class, outsiders can get rid of the embarrassing situation of directly creating specific product objects, just need to be responsible for "consumption" object. Without having to worry about how these objects are created and how they are organized. The respective responsibilities and rights are clarified, which facilitates the optimization of the whole software architecture.

Disadvantages

Because the factory class centralizes all instances of the creation logic, violates the cohesion responsibility allocation principle, the whole creation logic is concentrated in a factory class, it can create the class can only be considered beforehand, if you need to add new classes, you need to change the factory class.

As the number of specific product classes in the system increases, there may be a need for factory classes to create different instances based on different conditions. This kind of judgment of the condition and the judgment of the specific product type staggered together, it is difficult to avoid the spread of the module function, the maintenance and expansion of the system is very unfavorable;

These shortcomings have been overcome in the factory method model.

UML Class Diagram


Simple Factory mode # include <iostream>using namespace Std;class product{public:    virtual void show () = 0;}; Class Product1:public Product{public:    virtual void Show ()    {        cout<< "I am Product1" <<endl;    }} ; class Product2:public Product{public:    virtual void Show ()    {        cout<< "I am Product2" <<endl;    } };class factory{public:    Product *create (int n)    {        switch (n)        {case        1:            return new Product1;        Case 2:            return new Product2;        Default:            cout<< "I dont ' t know" <<endl;            return NULL;}}    ; int main (void) {    Factory fac;    Product *P1 = fac.create (1);    Product *p2 = fac.create (2);    P1->show ();    P2->show ();    Delete P1;    Delete P2;    return 0;}

2, factory method mode

The meaning of the factory method mode is to define a factory interface that creates the product object and defer the actual creation to the subclass Factory. The Core factory class is no longer responsible for product creation, so that the core class becomes an abstract factory role and is responsible only for the interfaces that a specific factory subclass must implement, and the benefit of further abstraction is that the factory method pattern allows the system to introduce new products without modifying the specific factory roles.

The factory method model is a derivative of the simple factory model, which solves the problem of many simple factory models. First, the ' open-closed principle ' is fully realized and extensible. Second, more complex hierarchy, can be applied to product results in complex situations.

The factory method model abstracts the Simple factory model. There is an abstract factory class (which can be abstract classes and interfaces), and this class will no longer be responsible for the production of specific products, but only to formulate some specifications, specific production work by its subclasses to complete. In this mode, the factory class and the product class can often correspond in turn. That is, an abstract factory corresponding to an abstract product, a specific factory corresponding to a specific product, this specific factory is responsible for the production of the corresponding products.

Abstract Factory (Creator) role

Is the core of the factory method pattern and is not application-agnostic. Any factory class of objects created in the schema must implement this interface.

Specific factory (concrete Creator) role

This is the specific factory class that implements the abstract factory interface, contains logic that is closely related to the application, and is called by the application to create the Product object. There are two such roles in: Bulbcreator and Ubecreator.

Abstract Product Role

The super type of the object created by the factory method pattern, which is the common parent class of the Product object or the co-owned interface. In, this role is light.

Specific products (concrete product) roles

This role implements the interface defined by the abstract product role. A specific product has a specific factory-created, often one by one correspondence between them.

UML Class Diagram


Factory method pattern # include <iostream>using namespace Std;class product{public:    virtual void show () = 0;}; Class Product1:public Product{public:    virtual void Show ()    {        cout<< "I am Product1" <<endl;    }} ; class Product2:public Product{public:    virtual void Show ()    {        cout<< "I am Product2" <<endl;    } };class ifactory{public:    virtual Product *create () = 0;}; Class Product1factory:public Ifactory{public:    virtual Product *create ()    {        return new Product1;    }}; Class Product2factory:public Ifactory{public:    virtual Product *create ()    {        return new Product2;    }}; int main (void) {    ifactory *fac1 = new Product1factory;    Product *p1 = Fac1->create ();    P1->show ();    Delete P1;    Delete Fac1;    return 0;}


3. Abstract Factory mode

Abstract Factory mode is the most abstract and general form of the factory pattern in all forms. Abstract Factory mode refers to a factory pattern that is used when there are multiple abstract roles. Abstract Factory mode provides the client with an interface that enables the client to create product objects in multiple product families without having to specify the product's specific circumstances. According to the LSP principle, any place that accepts a parent type should be able to accept subtypes. Therefore, what the system needs is only some instances of the same type as these abstract product roles, not instances of these abstract products. In other words, they are instances of specific subclasses of these abstract products. The factory class is responsible for creating instances of the specific subclasses of the abstract product.

UML Class Diagram


Product family refers to the family of products that are related to function in different product hierarchy. Are generally located in the same position in different hierarchical structures. Obviously, each product family contains the number of products, and the number of product grade structure is equal, the formation of a two-dimensional coordinate system, horizontal coordinate is the product hierarchy, ordinate is the product family.

When there are multiple products of different hierarchical structure, it is necessary to use a factory method pattern to deal with the hierarchical structure of these products using multiple independent plant hierarchy structures. If these product hierarchies are parallel, they result in multiple parallel plant hierarchy structures.

Abstract Factory mode uses the same factory hierarchy to create the product objects for these different product hierarchies.

For each product family, there is a specific factory. Each specific factory is created to belong to the same product family, but belong to different hierarchical structure of the product.

By introducing the abstract factory model, you can handle the creation of product objects in multiple product families with the same (or similar) hierarchical structure.

         because each specific factory role is responsible for the creation of two different hierarchy of product objects, each factory role needs to provide two factory methods, respectively, for creating two hierarchical products. Since each specific factory role needs to implement these two factory methods, it is generic and can be abstracted and moved to the abstract factory role to be declared.

Abstract Factory mode # include <iostream>using namespace std;class producta{public:virtual void Show () = 0;}; Class productb{public:virtual void show () = 0;};    Class producta_1:public producta{public:virtual void Show () {cout<< "I am producta_1" <<endl; }};class producta_2:public producta{public:virtual Void Show () {cout<< "I am producta_2" <<en    dl }};class productb_1:public productb{public:virtual Void Show () {cout<< "I am productb_1" <<endl    ; }};class productb_2:public productb{public:virtual Void Show () {cout<< "I am productb_2" <<endl    ;    }};class ifactory{public:virtual producta *createa () = 0; Virtual PRODUCTB *createb () = 0;};    Class Factory1:public ifactory{public:virtual producta *createa () {return new producta_1;    } virtual PRODUCTB *createb () {return new productb_1; }};class factory2:public ifactory{public:virtual ProDucta *createa () {return new producta_2;    } virtual PRODUCTB *createb () {return new productb_2;    }};int Main (void) {ifactory *P1IFAC = new Factory1;    Ifactory *P2IFAC = new Factory2;    ProductA *PA1 = P1ifac->createa ();    PRODUCTB *PB2 = P2ifac->createb ();    Pa1->show ();    Pb2->show ();    Delete PA1;    Delete Pb2;    Delete P1ifac;    Delete P2ifac; return 0;}

Abstract Factory mode differs from Factory mode:
Factory method Mode: An abstract product class that can derive multiple specific product classes.
An abstract factory class that can derive a number of specific factory classes.
Each specific factory class can only create an instance of a specific product class.
Abstract Factory mode: Multiple abstract product classes, each abstract product class can derive multiple specific product classes.
An abstract factory class that can derive a number of specific factory classes.
Each specific factory class can create instances of more than one specific product class.


References :

1. Simple Factory mode-Baidu Encyclopedia

2, factory method mode-Baidu Encyclopedia

3. Abstract Factory mode-Baidu Encyclopedia

4, "Big talk design mode" explain the factory model chapter

Simple Factory mode, factory method mode and abstract Factory mode-Design pattern learning

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.