Another major type of design pattern is structural. Seven modes are included, including the adapter mode, Bridge Mode, combination mode, decoration mode, appearance mode, metadata mode, and proxy mode. We will discuss the features and usage in the following two aspects. Thank you!
Adapter mode ):Converts an interface of a class to another interface that the customer wants. The adapter mode allows the classes that cannot work together due to incompatibility of interfaces to work together. [Big talk design model]
Features:The two parties that need to communicate should consider using the adapter when both data and behavior match, but the interface does not match, in order to facilitate communication and reuse. It can be understood as follows, with the same meaning and different expressions.
Usage:By creating an adapter class, you can call this class to convert or translate the content to be converted. It is a bit similar to "change yuan ".
Bridge ):Separate the abstract part from its implementation part so that they can all change independently. [Big talk design model]
Features:By separating parts, you can freely match them later, enhancing flexibility.
Usage:The implementation system may have multi-angle classification, and each type of classification may change. In this case, the multi-angle separation will allow them to change independently and reduce coupling between them. Refined division of labor makes work more professional and flexible.
Composite ):The object is combined into a tree structure to represent the 'partial-all' hierarchy. The combination mode ensures consistency between the use of a single object and a composite object. [Big talk design model]
Features:Departments with the same functions and tasks have different administrative levels after affiliated levels. The combination mode can be intelligently and accurately determined for allocation.
Usage:The combination mode is used when you want to ignore the differences between a composite object and a single object and uniformly use all objects in the composite structure. Through "parent", "child", and "child", the inheritance continues to reflect the relationship between the whole and the part.
Decorator ):Dynamically add some additional responsibilities to an object. As for the added function, the decoration mode is more flexible than the subclass generation. [Big talk design model]
Features:Dynamically add more functions to existing functions. Put each function to be decorated in a separate class, and let the class wrap the object to be decorated.
Usage:Each function is separated by a class, and an encapsulation method is used in the call process. This encapsulation method uses the object name as a parameter to store the current state of the object as the initial state of the next stage. It is easy to say and understand how to wear clothes: If wearing a shoes and so is a complete action, first wear a so and then wear a shoes; but you cannot take off the so before wearing the shoes. In this case, this action will never end.
Facade ):Provides a consistent interface for a group of interfaces in the subsystem. The appearance mode defines a high-level interface, which makes the subsystem easier to use. [Big talk design model]
Features:Multiple to one, one to multiple. This is a bit "from the people and from the people ".
Usage:In the initial stage of design, the layer is consciously separated. in the development stage, an interface is established to reduce inter-layer dependencies. When the new and old systems are maintained, a simple and clear interface (facade) is established ), allows the New and Old systems to smoothly interact with important resources.
(Appearance mode, feature thumbnail)
Flyweight ):In order to use the sharing technology to effectively support a large number of fine-grained objects. [Big talk design model]
Features:Avoid overhead of a large number of very similar classes.
Usage:If a program uses a large number of objects, and a large number of these objects cause a large storage overhead, you should consider using them. Attributes that are unchanged from the primary node can be regarded as internal states. The properties of objects at different times are external States. If an object's external State is deleted, a relatively small number of shared objects can be used to replace many group objects, the metadata mode should also be used.
Proxy mode ):Provides a proxy for other objects to control access to this object. [Big talk design model]
Features:If you do not do it yourself, but use a third-party proxy, the function is similar to object-oriented delegation.
Usage:Remote Proxy: to hide the fact that an object exists in different spaces, you can provide a local representation of an object in different address spaces. Virtual Proxy: Creates objects with high overhead as needed, and stores real objects that need to be instantiated for a long time.