Simple factory, factory method, Abstract Factory

Source: Internet
Author: User

Expression 1

Simple factories, factory methods, and abstract factories all belong to the Creation Mode in the design mode. Its main function is to help us extract the instantiation part of the object, optimize the system architecture, and enhance the scalability of the system.

This article is my summary of these three models and my understanding of the differences between them.

Simple Factory

Factory classes in simple factory mode generally use static methods to return different object instances by receiving different parameters.

Without modifying the code, it cannot be expanded.

Factory method

The factory method provides a factory class for each product. Use different factory instances to create different product instances.

Any product can be added to the same level structure.

Abstract Factory

Abstract Factory should deal with the concept of product family. For example, every automobile company may want to produce cars, trucks, and buses at the same time. Every factory must create cars, trucks, and buses.

It is easy to add new product lines, but new products cannot be added.

Summary

★In the factory model, the most important thing is the factory class, not the product class. Product classes can be in multiple forms, multi-level inheritance, or a single class. But it should be clear that only one type of instance is returned for the interface in the factory mode. This is worth noting when designing the product class. It is best to have a parent class or a commonly implemented interface.

★In factory mode, the returned instance must be created by the factory, rather than obtained from other objects.

★The instances returned in factory mode can not be newly created, and the returned instances created by the factory can also be.

Differences

Simple Factory: used to produce any product in the same level structure. (Unable to add new products)

Factory model: used to produce fixed products in the same level structure. (Any product can be added)
Abstract Factory: used to produce all products of different product families. (There is nothing to do with adding new products; Support for adding product families)

The above three factory methods have different levels of support in the hierarchical structure and product family. Therefore, you should consider which method should be used based on the actual situation.

Second statement

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 (which can be input by a program or read from the configuration file) and returns a product object.

Differences:

1. Simple Factory

A simple factory method includes an "abstract product class" (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, 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

Factory methods include "Abstract Factory classes" and "abstract product classes", and contain 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. A specific factory class can only generate one specific product class object. Different factories generate different products, rather than just like in a simple factory, 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 you 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 class ". The method for generating product classes for specific factory classes. The type of returned values must also be "abstract product classes" (because the same code is required for access during frontend calls ).

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 mode:
Multiple abstract product classes. Each abstract product class can be derived from multiple specific product classes.
An abstract factory class can be derived from multiple factory classes.
You can create multiple product instances for each specific factory class.
Differences:
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 product instance for a specific factory class, while the abstract factory mode can create multiple

Reprinted: http://wenku.baidu.com/view/76228b2b453610661ed9f403.html

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.