Bridging mode (bridge pattern)

Source: Internet
Author: User

Defined

The purpose of bridging mode is to separate the abstract part of the program from the implementation part, so that the mutual independence and no longer depend on each other, so that the abstract part and the implementation part of the modification will not cause the other party's modification.

Abstract part of the abstraction of the interface for the direct use of the client, while the implementation part of the definition of the abstract part of the interface needs to use, when the abstract part of the instance refers to the implementation of the interface, the so-called Bridge is formed. Bridging is the source of bridging by bridging the interface between the abstract interface and the implementation part by implementing part of the reference. The schematic diagram is as follows:


Abstracti On defines the interface that is used directly for the client, and references the implementor object, where implementor defines the interfaces for the implementation section, which are operationimp message to the abstract part of the object: thesansmonocondensed ">operationimp imp send message , and finally delivers the message to the specific object of the implementation section.

So, if you want to add another implementation to the implementation section, you just need to add a new class to implement the interface defined by implementor and implement a custom interface operation, and most importantly, the operation has no effect on the abstract part. The abstract part can still continue to work without your knowledge. Similarly, if you modify the abstract part, the implementation part is not necessary to modify, this is the charm of bridging mode, completely is the abstract part and the implementation part of independence. You can look at the definition of bridging mode again:



Bridging mode Usage Scenarios

    • When an object has multiple variables, by abstracting These variables, it relies on the concrete implementation and modifies it as a dependency abstraction.
    • When a change factor is shared among multiple objects. We can abstract out this change factor and then implement these different factors of change.
    • When we expect that multiple variables of an object can change dynamically and do not affect the use of the client's program.

code exampleThis example demonstrates the use of bridging design patterns by creating a game simulator. If we want to make our game simulator to support the bully and arcade games, we know that the game is controlled by the game joystick game, and the arcade is the use of the game stick (Arcade did not play, do not know whether to call this name), so the game control mode is different. If our simulator is to support both modes of operation, then it is necessary to define a control class for each control mode in a normal way, resulting in a large number of subclasses of the abstract class, resulting in redundancy and high coupling. Perhaps we should change the way, such as bridge mode, we know that different game control although the control method is not the same, but the game control commands are basically the same, there are up and down, start, and other similar functions, so we abstract a direct use for the client command class, and then abstract an implementation class, And all the control methods to achieve the abstraction of the implementation of the class interface can be, each specific class can be customized mode of operation. The schematic diagram is as follows:

SummarizeWhen we want the abstract and implementation parts to achieve low coupling, we should consider whether to use bridging mode. Of course, how to differentiate between the abstraction and the implementation part is a matter of our wit.


Bridging mode (bridge pattern)

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.