Simple factory, factory methods, and abstract factory design patterns

Source: Internet
Author: User

Simple factories, factory methods, and abstract factories are creation-type design patterns.

The three have the following in common:

1. There are two different categories: product and factory. The factory class instantiates the product class in its own method (that is, using the new command to generate the product class Object), and provides the generated product class object for external use.

2. the creation process is similar: the factory class gets a flag (ProgramInput, you can also read the content in the configuration file), return a product object.

 

Differences:

1. Simple Factory

In a simple factory method,Abstract Product(This class can be an interface or an actual class). All required product classes are subclasses of this abstract product class (if it is an interface, that is to say, all product classes inherit this interface ).

A simple factory generally only contains a specific factory class, which generates all product class objects. The method used to generate a product class is generally similar to the switch structure. Based on the input flag, select to create different types of objects. Because you do not know which class the created object belongs to, the type of the return value of the method is "abstract product class ".

 

2. Factory method

Abstract Factory, includingAbstract FactoryAndAbstract ProductAnd contains more than one factory class. All factory classes must be subclasses of "Abstract Factory classes" and all products must be subclasses of "abstract product classes.

Compared with a simple factory, the factory method generally starts from an abstract factory. Generally, a static method is provided in the abstract factory class, which generates differentSpecific factory typeAnd then a specific product is generated by the specific product category. Note,A specific factory class can only generate objects of one specific product class, Different factories generate different products, instead,A factory class can generate multiple objects of different product classes. As you can understand, when selecting different factory classes, you can select the generated product. Compared with a simple factory, the product selection action is in advance.

Because I do not know which factory class is created, the type of the return value of the static method that generates the factory class is"Abstract Factory". The method for generating product classes for specific factory classes. The type of returned values must also be"Abstract Product"(Because the sameCode).

 

3. Abstract Factory

Abstract Factory and factory methods are similar, with the following differences:

 

Factory method mode:
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.

 

abstract factory model:
multiple abstract product classes, each of which can be derived from multiple specific product classes.
An abstract factory class can be derived from multiple factory classes.
you can create instances of multiple product classes for each specific factory class.
difference:
the factory method mode has only one abstract product class, while the abstract factory mode has multiple.
the factory method mode can only create one instance of a specific product class, while the abstract factory mode can create multiple instances.

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.