Structural patterns (structural pattern) describe how a class or object can be combined to form a larger structure. The structure pattern describes two different things: a class and an instance of a class. According to this point, the structure pattern can be divided into the structure pattern of the class and the structure pattern of the object.
Subsequent content will include the following architectural patterns:
Adapter mode (Adapter): Match Interfaces of different classes
Synthetic mode (composite): A tree structure of the simple and composite objects
Decoration mode (decorator): Add responsibilities to objects dynamically
Agent mode (proxy): An object representing another object
Enjoy meta mode (Flyweight): A fine-grained instance used for efficient sharing
Façade mode (façade): A single class that represents an entire subsystem
Bridge mode (Bridges): Separates an object interface from its implementation
First, adapter (Adapter) mode
The adapter pattern transforms the interface of a class into another interface that the client expects, so that two classes that do not match the original interface can work together.
Origin of name
This is much like a transformer (Adapter), which transforms one voltage into another. Electricity in the United States is 110V and the voltage in China is 220V. If you want to use American electrical appliances in China, you must have a transformer that converts a 220V voltage to a 110V voltage. This transformer is a adapter.
The adapter model is also similar to the packing process of the goods: the real appearance of the packaged goods is concealed and changed by the packaging, so some people call this pattern the packaging (wrapper) mode. In fact, we often write many of these wrapper classes, wrapping up some of the existing classes so that they can meet the needs of the interface.
Two forms of Adapter pattern
Adapter mode has both the adapter mode of the class and the adapter pattern of the object. We will discuss both of these adapter modes separately.