Advantages and disadvantages of various design patterns

Simple factory pattern

The simple factory pattern is to let a factory class assume the responsibility of constructing all objects. What product the caller needs, let the factory produce it.

Advantages: The benefits of encapsulating the construction process can not only reduce coupling, if a product construction method is quite complicated, using the factory model can greatly reduce code duplication.

Disadvantages: First, if there are too many products that need to be produced, this mode will cause the factory class to be too large, assume too many responsibilities, and become a super class;

​ The second is that when a new product is generated, a new branch must be added to the factory class. This violates the principle of opening and closing

Factory method pattern

The factory method pattern is to split the various responsibilities of a simple factory into a factory class of professional responsibilities

Advantages: The factory method model was born to solve the shortcomings of the simple co-plant model. When more and more types of products are produced, the factory class will not become a super class (the operating process of each factory can be modified separately); when new products need to be produced, there is no need to change the existing factory, just add new ones The factory is fine. Maintained object-oriented scalability, in line with the principle of opening and closing.

Disadvantages: Compared with a simple factory, the factory method has more user operation classes, which increases the amount of code on the user side, but compared to its degree of improvement, this problem is still acceptable

Abstract factory pattern

The further improvement of the factory method pattern when abstracting the factory pattern, extracting its factory interface

Advantages: Since the client only calls the method in the abstract factory (each concrete factory implements its method, but the calling code on the client is the same), this makes it very easy to replace the factory. It also inherits the advantages of the factory method model, and gives full play to the principle of opening and closing and the principle of dependency inversion.

Disadvantages: The abstract factory model is too heavy. If the abstract interface needs to add new functions, it will affect all concrete factory classes. All factory models are suitable for horizontal expansion requirements of similar factories, but not suitable for vertical expansion such as adding new functions.

Singleton mode

The singleton mode is to enable the class to create its own instance when the class is created (initialization function is provided in the class).

There are two singleton modes, which are divided into lazy man mode and hungry man mode. The lazy man mode provides an initialization function in the class (initialize the class if it is not initialized, and end the call if the class is initialized). This initialization function is called locally; the hungry man mode is to declare when the variable of the class is created, and then give a declaration function to return the variable. The specific process and various advantages and disadvantages refer to my blog ().

Builder mode

The builder mode is also called the generator mode, which separates the construction of a complex object from its representation, so that the same construction process can create different representations

Implementation

The director directly communicates with the client on demand;

After the communication, the commander divides the customer's demand for creating products into the construction request of each component (Builder);

Delegate the construction request of each component to a concrete builder (ConcreteBuilder);

Each specific builder is responsible for the construction of product components;

Finally build into a specific product (Product).

advantage

1. Using the builder mode can make the client do not need to know the details of the internal composition of the product.

2. The specific builder classes are independent of each other, which is conducive to the expansion of the system.

3. The specific builders are independent of each other, so the construction process can be gradually refined without any impact on other modules.

Disadvantage

1. The products created by the builder mode generally have more in common, and their components are similar; if the products are very different, they are not suitable for using the builder mode, so their scope of use is subject to certain restrictions.

2. If the internal changes of the product are complex, it may lead to the need to define a lot of specific builder classes to achieve this change, resulting in the system becoming very large.

Prototype mode

In the prototype mode, prototype instances are used to specify the types of objects to be created, and new objects are created by copying these prototypes. In fact, it is to create another customizable object from one object without knowing any creation details.

advantage:

Using the prototype mode to create an object is much better in terms of performance than directly creating an object, because the clone method of the Object class is a local method that directly manipulates the binary stream in memory, especially when copying large objects, the performance difference is very obvious.

Another advantage of using prototype mode is to simplify the creation of objects, making creating objects as easy as copying and pasting when editing documents.

Adapter mode

Convert the interface of one class to another interface that the customer wants, so that the classes that cannot work because of the incompatible interface can work together.

advantage:

The existing classes are reused, and the problem of inconsistency between the existing classes and the reuse environment requirements is solved.

An object adapter can adapt multiple different adaptor classes to the same target, that is, the same adaptor can adapt both the adaptor class and its subclasses to the target interface.

Disadvantages:

For the object adapter, the implementation process of replacing the adapter is more complicated.

Bridge mode

Separate the abstract part from its actual part so that they can all change independently (the realization system may have multiple angles of classification, and each category may change, then separate this multi-angle to let them change independently, reducing Coupling between them)

The usage scenarios of the bridge mode:
1. When an object has multiple change factors, by abstracting these change factors, it will depend on the concrete realization and be modified to depend on the abstraction.
2. When a certain change factor is shared among multiple objects. We can abstract this change factor, and then realize these different change factors.
3. When we expect that multiple change factors of an object can change dynamically without affecting the use of the client's program.

Combination mode

Combine the objects into a tree structure to represent the "part-whole" hierarchy. The combined mode has consistency in the use of single user objects and combined objects.

Disadvantages: the client needs to spend more time clarifying the hierarchical relationship between classes

Advantages: A single object that does not require relational processing, or a combined object container, to achieve decoupling between containers. It is easy to add in when there are new parts.

When to use combination mode:

When the requirements reflect the partial and overall hierarchical structure, and when you want users to ignore the difference between the combined object and the single object, and use all the objects in the combined structure uniformly.

Intermediary model

Use an intermediary object to encapsulate a series of object interactions. The intermediary makes each object reference each other without explicit, thus making it loosely coupled, and can independently change the interaction between them.

advantage:

Transform many-to-many communication into many-to-one communication, reducing the complexity of the system; Mediator still has the characteristics of decoupling the system; centralized control

Disadvantages:

Due to the centralization of control, the complexity of interaction has become the complexity of the intermediary, which makes the intermediary more complex than any ConcreteColleague

Memo mode

Capture the internal state of an object without destroying the encapsulation, and maintain this state outside the object. In this way, the object can be restored to its original state.

The benefits of using the memo mode:
1) Sometimes the internal information of some initiator objects must be stored outside the initiator object, but it must be read by the initiator object itself. At this time, the use of the memo mode can save complex internal information of the initiator Shield other objects so that the boundaries of the package can be properly maintained.
2) This mode simplifies the initiation of humans. Initiators no longer need to manage and save individual versions of their internal states, and clients can manage the versions of these states they need by themselves.
3) When the status of the initiator role changes, the status may be invalid. At this time, the temporarily stored memo can be used to restore the status.

Disadvantages of using the memo mode:
1) If the status of the initiator role needs to be completely stored in the memo object, the memo object will be expensive in terms of resource consumption.
2) When the person-in-charge role stores a memo, the person-in-charge may not know how much storage space this state will occupy, and thus cannot remind the user whether an operation is expensive.

State mode

When the internal state of an object is allowed to change its behavior, the object looks like it has changed its class.

advantage:

Put specific state-related behaviors into an object. Since all state-related code exists in a certain ConcreteState, it is easy to add new states and transitions by defining new subclasses

Strategy mode

It defines a family of algorithms and encapsulates them separately so that they can be replaced with each other. The sub-mode allows the change of the algorithm to not affect the customers who use the algorithm

advantage:

The strategy mode perfectly complies with the "opening and closing principle". Users can select algorithms or behaviors without modifying the original system, or flexibly add new algorithms or behaviors.

Disadvantages:

Although the strategy mode defines the algorithm, it does not provide the choice of algorithm, that is, which algorithm is most suitable for which problem is not concerned by the strategy mode, so the choice of strategy still has to be done by the client. The client must clearly know the difference between each algorithm and when and where to use which strategy is the most appropriate, which increases the burden on the client.

Template method

Define the skeleton of an algorithm, and delay some steps to subclasses. The template method allows subclasses to redefine certain steps in the algorithm without changing the structure of the algorithm.

advantage:

It reflects its advantages by moving the invariant behavior to the super class and removing the duplicate code in the subclass, providing a good code reuse platform

Visitor mode

The operations that act on the elements in a certain object structure allow us to define new operations that act on these elements without changing the class of each element.

advantage:

The data structure is decoupled from the operations on the structure, so that the set of operations can evolve relatively freely

Disadvantages:

Makes it difficult to add new data structures

(Applicable to systems with relatively stable data structure)

Guess you like

Origin blog.csdn.net/weixin_43244265/article/details/108569105