Summary of structural mode topics (from terrylee)

Source: Internet
Author: User

Abstract: The structure mode, as its name implies, discusses the structure of classes and objects. It uses an Inheritance Mechanism to combine interfaces or implementations (class structure mode), or by combining some objects, to implement new functions (object structure mode ). These structural models have great similarity in some aspects, but they have different focuses. This article attempts to make a simple summary of these structural patterns.

Main Content

1. Structural Mode Overview

2. Differences and comparison of structural modes

3. Encapsulation of changes

Structural mode Overview

The structure mode, as its name implies, discusses the structure of classes and objects. It uses an Inheritance Mechanism to combine interfaces or implementations (class structure mode), or by combining some objects, to implement new functions (object structure mode ). These structural models have great similarity in some aspects, but they have different focuses.

Adapter Mode focuses on converting existing interfaces through class inheritance or object combination; Bridge By separating abstraction and implementation, the pattern allows them to change independently. It emphasizes the changes of the system along multiple directions; Decorator The mode uses Object combination instead of inheritance to realize the ability to dynamically expand the object function at runtime. It emphasizes the Extended Interface; Composite The pattern blur the concepts of simple elements and complex elements. It emphasizes a hierarchical structure; Fa C Ade Mode integrates the internal subsystems and customers of Complex SystemsProgramDependency decoupling, which focuses on simplified interfaces and more on an architecture model; Flyweight The Mode solves the memory overhead caused by a large number of fine-grained objects. Fa C Ade The pattern is the opposite. The focus is on small objects; Proxy Mode provides a proxy for other objects to control access to this object. It focuses on adding indirect layers to simplify complex problems.

Differences and comparison of structural modes

1. Bridge Mode and decoration Mode

These two models are designed to reduce the number of child classes and avoid complex inheritance relationships. However, they have different solutions. In the decoration mode, the Child classes are placed in separate classes to meet the needs of new functions, when we encapsulate the classes that describe new functions into the objects of the base class, we get the required subclass objects. These classes that describe new functions can be combined to implement many function combinations, A brief diagram of the decoration mode is as follows:



Figure 1 decoration Mode

The Bridge Mode abstracts the implementation details of the original base class, constructs it into an implementation structure, and then transforms the original base class into an abstract level structure, in this way, the system can be changed independently in multiple dimensions. The diagram of the bridge mode is as follows:

Figure 2 brief diagram of the Bridge Mode

2. Appearance mode and Agent Mode

The appearance mode and Agent Mode have different focuses on solving the problem, but they solve the problem in the same way, that is, they all adopt the method of indirect layer, this is also a common method in our software systems. Although the appearance mode focuses on simplified interfaces, in some cases, the appearance mode can also be the responsibility of the proxy mode. For example, the appearance object may be a remote proxy of another address space object, this can be called the appearance proxy mode or the agent appearance mode. Their class diagram is as follows:

Figure 3 proxy Mode


Figure 4 appearance Mode

3. Adapter Mode

The adapter mode focuses on the conversion interface, which enables the two classes that cannot work together to work together, so they are often used in class libraries for reuse,CodeIn terms of migration, there is a taste of being overwhelmed. The class adapter and Object Adapter can be selected based on the actual situation, but the Object Adapter mode is generally recommended, as shown in. The class adapter mode is on the left, and the Object Adapter mode is on the right:

Figure 5 brief diagram of the adapter Mode

Encapsulation of changes

How to cope with changes is an eternal topic of Software Development. We may not be able to prevent changes, but at least we can minimize them through some means. "Finding the variable factors in the system and encapsulating it" is usually called encapsulation of changes. This question is explained in 《JavaIt is clearly stated in "Mode", simple implementation of abstraction and implementation, that is, "Open-As shown in:


Figure 6

In this inheritance structure, the first layer is abstraction, which encapsulates the abstract business logic, which is a constant part of the system; The second layer is implementation, it is the implementation of specific business logic, encapsulates the changes in the system, this implementation allows the realization of role polymorphism changes:


Figure 7

That is to say, the client depends on the abstract type object of the business logic, but it has nothing to do with the specific implementation of the abstraction, regardless of whether it is "Implementation" or "Implementation ".2"Or" Implementation"3", As shown in:


Figure 8

Each inheritance relationship encapsulates a factor of change, and an inheritance relationship should not deal with two factors of change. In other words, this simple inheritance relationship cannot deal with changes in abstraction and implementation, as shown in:


Figure 9

The two change factors in should be independent and can be independent without affecting the other. For example, the two levels below encapsulate their own change factors, respectively, each change factor can be expressed through a static link, so it is implemented by an inheritance link, for example:


Figure 10

What can we do with the connection between abstraction and implementation? There is only one good design, but there are many poor designs. The following design is a class diagram that continues to use inheritance for static Link Design:


Figure 11

There are actually many problems with such a design. The first problem is that there are multiple inheritance relationships. As the implementation increases, the inheritance relationships of sub-classes become very complex; second, if there is a new abstract correction or a new specific implementation role, we have to re-Modify the static relationship in the existing system to adapt to the new role, which violates the open-Closed principle. The correct design should be to use two independent hierarchical structures to encapsulate two independent change factors and use an aggregation relationship between them to achieve function reuse, this returns to our bridge mode, as shown in:


Figure 12

From another perspective, a good design usually has no more than two layers of inheritance level structure, or if there are more than two reasons for change, you need to find out which one is static, static relationships can be used. Which one is dynamic? An aggregation relationship is required.

More Design PatternsArticleYou can access 《. NetDesign Pattern Series"

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.