Summary of big talk design model 3-three sisters in the factory

Source: Internet
Author: User

 

The three sisters of the factory are simple factory model, factory method model, and abstract factory model.

 

I. Simple factory model:

1. in terms of the design pattern type, the simple factory pattern belongs to the creation pattern, which is also called the static factory method pattern, the simple factory mode is determined by a factory object to create a product instance. The simple factory model is the simplest and Practical Model in the factory model family. It can be understood as a special implementation of different factory models.

2. Advantage: it includes necessary logical judgment to dynamically instantiate related classes based on the client selection conditions. For the client, dependencies with specific products are removed.

3. disadvantages: Because the factory class integrates the creation logic of all instances, it violates the High Cohesion responsibility allocation principle and integrates all creation logic into a factory class; the class it can create can only be considered in advance, if you need to add a new class, you need to change the factory class. When the number of product categories in the system increases, the requirements for the factory category to create different instances based on different conditions may arise. This kind of condition judgment and product type judgment are intertwined, it is difficult to avoid the spread of module functions, it is very unfavorable for system maintenance and expansion. The class it can create can only be considered in advance. If you need to add a new class, you need to change the factory class, it violates the "open-close principle ".

The structure is as follows:


 

 


Ii. Factory method mode

Factory method mode: defines an interface for creating objects, so that the subclass determines which class to instantiate and delays the instantiation of a class to its subclass. The core factory category is no longer responsible for product creation. In this way, the core class becomes an abstract factory role and is only responsible for the interfaces that must be implemented by specific factory subclass, the advantage of further abstraction is that the factory method mode enables the system to introduce new products without modifying the specific factory role. The solution solves many simple factory models. First, the 'openness-close' principle is fully realized and the scalability is realized. Second, a more complex hierarchy can be applied to scenarios where the product results are complex.

The factory method mode is used to produce fixed products in the same level structure (support for adding and scaling) as follows:

1. A factory creates a product, and all specific factories inherit from an abstract factory.

2. The client first creates different product factories, and then the factory creates specific products,

3. The product creation logic is scattered in each specific factory. The client only depends on the abstract factory and abstract product, and does not rely on the specific factory and specific product growth. To add new products, you need to add factory and product categories to comply with the OCP principles.

In short, the factory method moves the logic judgment of the simple factory to the client. To add the function, the simple factory mode is the factory class, and the factory method is the client.


The structure is as follows:




 

Resolution:

Abstract Factory role: the Core of the factory method Model and Its ApplicationProgramIrrelevant. Any factory class of the object created in the mode must implement this interface.

Concretecreator: this is a specific factory class that implements Abstract Factory interfaces. It contains the logic closely related to applications and is called by applications to create product objects.

Abstract Product role: the super type of the object created in the factory method mode, that is, the common parent class of the product object or the common interfaces. In, this role is light.

Specific product role: this role implements the interface defined by the abstract Product role. A specific product is created in a specific factory, which is usually one-to-one.

 

 

Iii. Abstract Factory Model

 

Abstract Factory: provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.

Abstract Factory mode is the most abstract and general form in the factory model family.

The structure is as follows:



 

Advantage: The first advantage is that it is easy to interact with the product series. Because of the specific factory class, you only need to appear once during initialization in an application, this makes it very easy to change the specific factory of an application. It only needs to change the specific factory to use different product configurations. The second advantage is that it separates the process of creating an instance from the client. The client operates the instance through their abstract interface, and the specific class name of the product is also separated by the implementation of the specific factory, will not appear in the customerCode.

Disadvantage: if you add a function, you must add at least one abstract product abstractproductc and two specific products productc1 and productc2. You must also change the abstract factory interface abstractfactory and the specific factory concretefactory1 and concretefactory2.

 

 

The example in the big talk design pattern book shows: performing addition, subtraction, multiplication, division operations. They will extract an abstract class: operation class, and four specific operation classes: Addition class, subtraction class, multiplication class, division class. There is also a simple factory class in the simple factory model. In this simple factory class, it will determine whether it is addition, subtraction, multiplication or division. The factory method model has an abstract factory class and four specific ones: Addition factory, subtraction factory, multiplication factory, and Division factory.

The structure is as follows:

 

1. Simple factory model:


 

2. Factory method mode:


 

3. In the abstract factory model, he adds more than one product class, that is, in addition to one product computing class and other product classes.

(Figure omitted)

 

Iv. Summary:

In order, the degree of abstraction is deepened in sequence. Simple factory models are the easiest to understand. When the same interface is required but the content in the interface is different, you can use a factory to generate an instance of multiple classes. The most obvious difference is that there is a factory class to generate interface instances.

The factory method mode is a simplified factory abstraction, which does not require an independent object to be created. Instead, the required objects are created by subclass delay. For example, an engine object is included in an automobile object, but the engine object is not produced by a specific factory type, but depends on what kind of automobile, and different cars have different engine objects, this is done by the subclass.

Abstract Factory is a bit similar to simple factory mode + factory method mode. It isolates a specific class through an abstract class, and determines the specific class to be generated through a unique entry. The prerequisite for using it is a series of similar classes.

 

 

 

 

 

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.