Object-Oriented Advanced (2) - Creation Mode

1. Simple Factory Pattern

By specifically defining a class to be responsible for instances of other classes, the created instances usually have a common parent class.

1. Included roles and their responsibilities

(1) Factory role

The core of the Simple Factory pattern, which takes care of the internal logic of creating all instances. The factory class can be called directly by the outside world to create the required product objects.

(2) Abstract roles

The superclass of all objects created by the Simple Factory pattern, which is responsible for describing the common interface common to all instances.

(3) Specific product roles

A concrete instance object created by the simple factory pattern.

2. Advantages and disadvantages of the simple factory pattern

In this pattern, the factory class is the key to the whole pattern . It contains the necessary logical judgment, and can decide which specific class object should be created according to the information given by the outside world . When using, users can directly create required instances according to the factory class , without knowing how these objects are created and how they are organized. Conducive to the optimization of the entire software architecture . The disadvantage of the simple factory pattern is also reflected in its factory class. Because the factory class concentrates the creation logic of all instances, it does not do a good job of "high cohesion". In addition, when the specific product categories in the system continue to increase, it may appear that the factory class is required to be modified accordingly, and the scalability is not good .

 

Second, the factory mode

1. Concept

The factory method belongs to the class creation pattern, also known as the polymorphic factory pattern. The meaning of the factory pattern is to define a factory interface for creating a product object, and defer the actual creation to subclasses.

The core factory class is no longer responsible for the creation of products, so the core class is called an abstract factory role, and is only responsible for the interfaces that specific factory subclasses must implement. Introducing new products without the role.

2. Roles and responsibilities in the model

(1) Abstract factory role:

The core of the factory method pattern, any factory class must implement this interface

(2) Specific factory roles:

A concrete factory class is an implementation of an abstract factory that is responsible for instantiating product objects

(3) Abstract roles:

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

(4) Specific product roles:

Concrete instance objects created by the factory method pattern

3. Comparison of factory mode and simple factory mode

The difference in structure is not very noticeable. The core of the factory method class is an abstract factory class, while the simple factory pattern puts the core on a concrete class. The factory method pattern has an alias called the polymorphic factory pattern because concrete classes have a common interface, or a common abstract parent class . When the system expansion needs to add a new product object, only a specific object and a specific factory object need to be added. The original factory object does not need to be modified, nor the client, which is in line with the "open-closed" "in principle. The simple factory pattern has to modify the factory method after adding a new product object, and the extensibility is not good. The factory method pattern can be evolved into a simple factory pattern after degeneration.

 

3. Abstract Factory Pattern

The abstract factory pattern is the most abstract and general of all the factory patterns. The abstract factory pattern can provide an interface to the client, so that the client can the product.

1. The roles and responsibilities contained in the pattern
(1) Abstract Factory (Creator) role 
The core of the abstract factory pattern includes the declaration of multiple product structures, and any factory class must implement this interface.
(2) Role of Concrete Creator
A concrete factory class is an implementation of an abstract factory, responsible for instantiating product objects in a product family.
(3) Abstract (Product) role
The parent class of all objects created by the abstract pattern, which is responsible for describing the common interface common to all instances.
(4) Concrete Product role
The concrete instance object created by the abstract pattern

Comparison of three factory patterns:

Simple Factory: Used to produce any product within the same hierarchy. (For adding new products, can do nothing)
Factory method: used to produce fixed products in the same hierarchical structure. (Supports adding any product)   
Abstract Factory: used to produce all products of different product families. (Inability to add new products; support adding product families)

 

4. Builder Mode

1. The roles and responsibilities included in the pattern

(1) Builder: To create various parts of the product, unify the abstract interface.
(2) ConcreteBuilder: Specifically create each part of the product, part A, part B, part C.
(3) Director: Construct an object using the Builder interface.
(4) Product: Represents the constructed complex object.
ConcreteBuilder creates an internal representation of the product and defines its assembly process, including defining the classes that make up the components, including the interfaces for assembling these components into the final product.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324466168&siteId=291194637