Design Mode 6: Bridge Mode

Source: Internet
Author: User

Definition:
Separate the abstract part from its implementation part so that they can all change independently. (The abstract Part and implementation part here are not the relationships between the parent class and child classes, interfaces, and implementation classes we generally think, but composite relations. That is to say, the implementation part is called by the abstract part to complete (implement) the abstract Part of the function .)

Role composition:
1) Abstract action role: it defines the abstract class interface and maintains a reference pointing to the Implementor role.
2) Precise abstraction action: Implements and expands interfaces defined by abstract roles.
3) Implementor role: Provides the interface for implementing classes. The interface here can be different from the interface in the abstract role.
4) ConcreteImplementor role: describes the specific implementation of the Role Definition interface.

Note:
In the book Thinking in Patterns with Java, the author calls the abstract Part "front-end" (right and translated as "front-end "), the implementation part is "back-end" (backend ). This method is much better to understand than abstract implementation.

 

Class diagram:

 

 

Code:

[Java]
<Span style = "font-size: 14px;"> /**
* Abstract Part (front-end) Abstract role
*/
Public class internal action {
 
// Maintain a reference to the role of Implementor.
Private Implementation implementation;
 
Public role action (Implementation imp ){
Implementation = imp;
}
 
// The frontend (abstract Part) interface is defined below
Public void findA (){
// Uses the existing interfaces of the backend (implementation part) to implement functions in combination.
Implementation. serviceA ();
Implementation. serviceB ();
}
 
Public void findB (){
Implementation. serviceB ();
Implementation. serviceC ();
}
 
Public void findC (){
Implementation. serviceA ();
Implementation. serviceB ();
Implementation. serviceC ();
}
 
Protected Implementation getImplementation (){
Return implementation;
}
}
</Span>

[Java]
<Span style = "font-size: 14px;"> /**
* Precise abstract roles of the abstract Part (front-end)
* Independent front-end Evolution
*/
Public class RefinedAbstractionA extends action {
 
Public RefinedAbstractionA (Implementation imp ){
Super (imp );
}
 
// Combine the methods provided by the abstract role to complete a function
// This is why precise abstract roles (Correction abstract roles)
Public void find1 (){
FindA ();
FindB ();
}
 
Public void find2 (){
FindC ();
}
 
}
</Span>

[Java]
<Span style = "font-size: 14px;"> /**
* Another precise abstract role is ignored by me like above
* Independent front-end Evolution
*/
Public class RefinedAbstractionB extends action {

Public RefinedAbstractionB (Implementation imp ){
Super (imp );
}
 
// Combine the methods provided by the abstract role to complete a function
// This is why precise abstract roles (Correction abstract roles)
Public void serviceA (){
FindA ();
FindB ();
}
 
Public void serviceB (){
FindC ();
}
 
// Some functions are implemented directly here.
Public void serviceE (){
GetImplementation (). serviceC ();
}

} </Span>

[Java]
<Span style = "font-size: 14px;"> /**
* Implementation part (backend) implementation role
*/
Public interface Implementation {
 
// This interface only defines certain Interfaces
Void serviceA ();
 
Void serviceB ();
 
Void serviceC ();
 
}
</Span>

[Java]
<Span style = "font-size: 14px;"> /**
* The specific role is to implement the interface provided by the role.
* And complete certain functions
* Independent backend Evolution
*/
Public class ImplementationA implements Implementation {
 
@ Override
Public void serviceA (){}
 
@ Override
Public void serviceB (){}
 
@ Override
Public void serviceC (){}
 
}
</Span>

Purpose:
System design is always filled with various variables, which prevents accidental defense. For example, a customer representative may ask to modify a requirement and add a function. In the face of such changes, you can only constantly modify the design and code, and
And a new round of testing .......
So what method can be used to better solve the impact of changes on the system? You can analyze the types of changes, define the unchanged framework using abstract classes, and then implement the changed content using specific subclasses. In this way, the customer is only an abstract class. This method can effectively avoid the impact of adding new implementations to the existing interfaces in the abstract class, reducing the impact of changes. However, this may result in an explosion in the number of child classes, and in some cases it is not flexible.
However, when the behavior of each subclass changes frequently, or there are some duplicates and combinations, we may wish to extract these behaviors and provide them through interfaces, then, the service is provided to the original subclass in combination. In this way, the frontend and the used backend are changed independently, and the backend is reused.
In fact, this is the birth of the bridge model.

Applicable environment:
1) when similar behaviors or combinations of similar behaviors are used in your system in multiple places, you can consider using the Bridge Mode to improve reuse, and reduce child classes generated by different behaviors.
2) the behavior of a class in the system may have several different changes. To effectively encapsulate the changes, you can consider extracting the behavior of the class.
3) Of course, this can also be the case. The behavior may be used by different similar classes, or the bridge mode can be used for implementation.

Advantages:
The bridge mode uses a combination of low coupling to replace inheritance, which provides many benefits.
1) encapsulate the parts that may be changed separately to minimize the impact of the changes without compiling unnecessary code.
2) The abstract part and the implementation part can be changed separately, and the expansion of each part will not damage the bridge mode.
3) for the customer program, your implementation details are transparent.

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.