Vernacular design pattern-structural pattern-combination pattern, bridging pattern and adapter Pattern

Source: Internet
Author: User

In a twinkling of an eye, the design model has learned so much.

I think the design pattern is actually how to abstract and make use and implementation more reasonable. These practices make our software easier to cope with changes.

 

The bridging mode learned a few days ago.

When a change is oriented to two or more dimensions, we adopt a combination of abstract methods to deal with the change.
What combination abstraction? Let's take a look at the bridge mode and you will know.

An object may have two types of changes: A and B. Then, we can abstract a as AbstractA, and B as abstractb. Put them together, simply put, an instance of wannactb is put in AbstractA. B changes are auxiliary abstractions, and the main logic is in. Abstract B for future use. In the specific instance that implements AbstractA, we can use abstract B without worrying about how abstract B is implemented. You only need to input a specific implementation instance of abstract B. In Abstract A, we can combine these two dimensions at will, which solves the problem of dramatic changes in the two dimensions of an object. Even if they are staggered, we are not afraid.
The key point and focus of the Bridge Mode lies in the principle of object-oriented design: prefer combination to inheritance. In fact, using multi-inheritance can solve these problems, but it is not recommended by us. This multi-inheritance often violates the single responsibility principle. Besides, C # does not support multi-inheritance, so it must inherit two interfaces. Abstract A and abstract B make equal relations. Abstract B is not the main one, and it may not have any independent meaning. In this way, the relationships between classes are mixed.

 

So I learned the adapter mode before. As a structural mode, the adapter mode is more used to match the original interface to adapt to new requirements. for users, the original interface is black box and invisible.

The bridge mode is more used for changing the two lines.At the initial stage of design,To cope with the changes in the main logic and the secondary logic, we use the bridge mode to combine the secondary logical interfaces into the primary logical interfaces, and then let them implement them separately.

InAfter the design is completed or the development is completed,If the existing interfaces cannot meet the requirements, we will use the adapter mode to create a new interface to adapt to the original interface to meet the requirements.

 

After learning and practice, we can also understand that these two models are often used together. for example, in the following scenario: an external interface is required for an implementation of the auxiliary logic in the bridge mode. Then, the external interface can be adapted to this implementation, in this way, both bridge and adaptor are used. it's a bit abstract. I don't know if you can understand it, because I didn't find a very good example about it,If any comrade has it, please share it with us.

 

The combination model I learned today is too simple.

However, I heard that this isA very many, especially useful mode.So don't take it lightly. The combination mode can be seen from the name. It is a combination of something.

So why are they combined?

It is for more convenient calls.The caller (client) calls an interface method no matter whether it is a combination or a single one. the container contains a single singleobject. Both container and singleobject implement the same interface. The client only needs to call this interface. in the container, singleobject and its methods are cyclically called. In this way, the same interface facilitates client calls and simplifies client calls.CodeThe client does not need to know whether you are a container or a singleobject.

 

All the departments below a company are a good example.

The general manager said: I want to know the work of various departments this month. Then each department should submit a report to the Secretariat, and the secretary will summarize the report and hand it over to the general manager.

If we do not use the combination model, the general manager needs to ask one department at a time: How is your department doing this month? Will make him exhausted.

When the combination mode is used, the general manager is a client, which does not need to care about how the singleobject of each department is combined. Just look at the summary of the secretariat (interface. the report (container) summarized by the Secretariat is directly presented to the general manager.

 

The examples are not suitable. Please make sure that you can learn them together. it may not be correct for beginners to publish things in many places, but I hope to inspire everyone and let us think about it together to complete the great business of software design and development. Thank you. :)

 

 

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.