Structural Pattern of Design Pattern

Source: Internet
Author: User

Article 1: Creation Mode of Design Mode

  

In this section, we focus on the structure mode in gof, which is mainly used to describe how to combine classes to form a larger structure. Structural modes include adapter, decorator, bridge, flyweight, facade, composite, and proxy modes.

Next we will describe the modes mentioned above separately.

1)Adapter). When we have developed a module with a clear set of interfaces, and the module is being used by a function (meaning that the module interface is less likely to change ), if there is another function that also needs to use the function of this module, But it corresponds to a set of completely different interfaces, then the adapter can play a role.

There are two adapter modes: Object Adapter and class Adapter. The UML diagram of the Object Adapter is as follows:

  

Here, adaptee1 and adaptee2 refer to two sets of different subsystems. They exist as the attributes of the adapter and can be specified using IOC.

The UML diagram of the class adapter is as follows:
  

There are two different subsystems, but here we create two adapter classes to point to two subsystems respectively. Here, we can set up an adapter factory between the client and itarget to create different adpater instances based on business needs.
2)Decorator)If we have developed a set of functions, and then add some sub-functions as needed, and these sub-functions can be added or deleted when compared to scattered ones, then if we directly modify the interface, the interface functions are complex and unstable. In this case, we can use the decoration mode. The corresponding UML diagram is as follows:

  

Concretecomponent has implemented the basic functions of component. If some additional functions are not suitable for concretecomponent, we can create a decorator-based class like concretedecoratora, the setcomponent method is used to concatenate core functions and auxiliary functions.

Sometimes, for simplicity, we can also directly Mount concretedecorator under concretecomponent.

3)Bridge), Several best practices advocated by Object-Oriented include: 1) Encapsulation changes; 2) interface-oriented programming; 3) combination over inheritance; 4) Class responsibilities should be as single as possible. The bridge adapter perfectly embodies these. Through the creation mode, we can well achieve the goal of interface-oriented programming. That is to say, the Declaration type of each variable in the program is the interface type or abstract class, the specific implementation type is specified by different design modes. This is good when the interface or abstract class is basically stable, but what should we do when the interface needs to change? You can see the UML diagram of the bridge:

  

From this figure, we can see that the implementor interface changes have basically no impact on the client. The role action will hold an instance of implementor.

4)Flyweight)When we need to use a large number of small objects in our system, but we do not want to create all the small objects, we can consider using the enjoy mode, it extracts the public part of a small object, encapsulates it as the base class, creates small objects for different conditions, and maintains these small objects in the object pool. When you need to use small objects, first, search in the object pool. If yes, return directly. For the "personal" part of a small object, it is maintained by the client that calls the small object. The corresponding UML diagram is as follows:
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.