No need to worry about distinguishing between adapter mode, decoration mode, proxy mode, and appearance mode anymore!

No need to worry about distinguishing between adapter mode, decoration mode, proxy mode, and appearance mode anymore!


When we analyze and use design patterns, we often confuse adapter patterns, decoration patterns, proxy patterns, and appearance patterns because they are so similar.

How should we distinguish?

Don't worry, it's very easy as long as we remember their main characteristics!

Proxy mode

Both the agent and the principal must implement the same interface, and the agent must hold the object of the principal.

decorator pattern

When the original class cannot meet the requirements, the original class is enhanced. The decoration mode must be passed in from the outside, usually the original object is passed as a parameter to the constructor of the decorator.

adapter pattern

Adapters are often required for adaptation due to incompatible interfaces.

appearance mode

Definition: Provides a unified interface to access a group of interfaces in the subsystem, and the appearance defines a high-level interface to make the subsystem easier to use.

Summarize

The proxy mode focuses on isolation restrictions and controls access to original objects.

The decoration mode focuses on the expansion of functions, focusing on dynamically adding methods on an object, and realizing more functions under the same method.

Adapter mode focuses on interface compatibility.

The appearance mode focuses on the integration and uniform adaptation of multiple classes.

Guess you like

Origin blog.csdn.net/u011578734/article/details/113745223