"Object creation" mode

Bypassing new through the "object creation" mode avoids the tight coupling (dependence on specific classes) caused by the object creation (new) process and supports the stability of object creation. It is the first step after interface abstraction.

Typical pattern

  1. Factory Method
  2. Abstract Factory
  3. Prototype
  4. Builder

Factory Method 

 Motivation
In software systems, we are often faced with the task of creating objects; the specific types of objects that need to be created often change due to changes in requirements.
How to deal with this change? How to bypass the conventional object creation method (new) and provide an "encapsulation mechanism" to avoid the tight coupling of the client program and this "specific object creation work"?
 Schema definition

Define an interface for creating objects and let subclasses decide which class to instantiate. This pattern allows instantiation of a class to be deferred to subclasses. 

 Summary of key points
  1. Factory Method pattern is used to isolate the coupling relationship between users of class objects and concrete types. Faced with a concrete type that changes frequently, tight coupling (new) will lead to software fragility.
  2. The Factory Method pattern uses object-oriented techniques to defer the work of specific objects to be created to subclasses, thereby implementing an expansion (rather than changing) strategy, which better solves this tight coupling relationship.
  3. Factory Method pattern solves the demand changes of "single object". The disadvantage is that the creation methods/parameters are required to be the same.

 Abstract Factory Abstract Factory

 Motivation
In software systems, we are often faced with the creation of "a series of interdependent objects"; at the same time, due to changes in requirements, there is often the creation of more series of objects.
How to deal with this change? How to bypass the conventional object creation method (new) and provide an "encapsulation mechanism" to avoid the tight coupling between the client program and this "multiple series of specific object creation work"?
Pattern definition
provides an interface that is responsible for creating a series of "related or interdependent objects" without specifying their specific classes.
 ——"Design Patterns" GoF

 

Summary of key points
  1. If there is no need to cope with the demand changes of "multi-series object construction", there is no need to use the Abstract Factory pattern. At this time, it is perfectly fine to use a simple factory.
  2. "Series of objects" refers to the interdependence or interaction between objects in a specific series. Objects of different series cannot depend on each other.
  3. The Abstract Factory model is mainly designed to respond to changes in demand for "new series". Its disadvantage is that it is difficult to cope with changes in demand for "new objects". 

 

Guess you like

Origin blog.csdn.net/ME_Liao_2022/article/details/132901946