Design Pattern (23) --- Bridge pattern

Source: Internet
Author: User

Design Pattern (23) --- Bridge pattern

Definition: Decouples abstraction and implementation so that the two can change independently.

Abstract role
--- | Defines the behavior of the role and saves a reference to the role. This role is generally an abstract class
Implementor
--- | It is an interface or abstract class that defines the behaviors and attributes required by the role.
RefinedAbstraction corrected abstract roles
--- | It references implementation roles to correct abstract roles
ConcreteImplementor
--- | It implements methods and attributes defined by interfaces or abstract classes.
------ Abstract role reference implements roles, or some implementations of abstract roles are completed by referencing and implementing roles.
The bridge mode uses common functions such as aggregation, inheritance, and overwriting between classes.

 

Public class BridgeTest {public static void main (String [] args) {// define an implementation role Implementor imp = new ConcreteImplementor1 (); // define an Abstract role Abstract abs = new refined1_action (imp); abs. request () ;}}/*** implementation role * defines the method to be implemented * @ author admin **/interface Implementor {// basic method public void doSomething (); public void doAnything ();}/*** specific implementation role * @ author admin */class ConcreteImplementor1 implements Implementor {@ Overridepublic void doSomething () {System. out. println ("Implementation 1, doSomething") ;}@ Overridepublic void doAnything () {System. out. println ("specific implementation 1, doAnything") ;}/ *** specific implementation role * @ author admin */class ConcreteImplementor2 implements Implementor {@ Overridepublic void doSomething () {System. out. println ("implementation 2, doSomething") ;}@ Overridepublic void doAnything () {System. out. println ("implementation 2, doAnything") ;}/ *** abstract a role to define its behavior, save a reference to an implemented role * @ author admin **/abstract class Abstract {// Define reference to the implemented role private Implementor imp; // The constraint subclass must implement this constructor public Abstract (Implementor imp) {this. imp = imp;} // its own behavior and attribute public void request () {this. imp. doSomething () ;}// get the implemented role public Implementor geibd () {return this. imp ;}/ *** specific Abstract role * @ author admin **/class refined?action extends Abstract {// override the constructor public refined=action (Implementor imp) {super (imp) ;}// corrected the behavior of the parent class @ Overridepublic void request () {super. request (); super. geibd (). doAnything ();}}

 

Example:

One merchant opened two companies, one specialized in formal products and the other (whether formal or not) products that can earn money.
Therefore, when the market is profitable, he will immediately transform to do this. But the company's internal staff will not change.
How can we achieve the company's transformation without changing its internal structure? This requires a bridge to connect.

 

Public class BridgeT {public static void main (String [] args) {// create a product object House = new house (); // create the company object Corp houseCorp = new HouseCorp (house); houseCorp. makeMoney () ;}}/*** abstract company class, * defines the methods that each subsidiary needs to implement. * Simultaneously Save the reference to the Implementation class * @ author admin */abstract class Corp {private Product product; // reference public Corp (Product product) {this. product = product;} // The main purpose of the company is to make money public void makeMoney () {// this. product. beProducted (); // sell this again. product. beSell () ;}/ *** specific company implementation class, implement reference to the implementation class * @ author admin **/class HouseCorp extends Corp {public HouseCorp (Product product) {super (product) ;}@ Overridepublic void makeMoney () {// The company's strategy super. makeMoney (); System. out. println ("real estate companies make a lot of money... ") ;}}/*** Abstract class, the method to be implemented by a subsidiary * @ author admin **/abstract class Product {// Product sales public abstract void beSell (); // Product production public abstract void beProducted ();}/*** specific Product implementation class * @ author admin */class House extends Product {@ Overridepublic void beSell () {System. out. println ("the house was sold out like this... ") ;}@ Overridepublic void beProducted () {System. out. println ("the house is produced in this way... ");}}
Advantages of the Bridge Mode:
--- | Abstraction and implementation Separation
--- | Excellent scalability
--- | The implementation details are transparent to the customer.
Use Cases of Bridge Mode
--- | Do not want or do not apply to use inheritance scenarios
--- | Scenarios with unstable interfaces or abstract classes
--- | Scenarios with high reusability requirements.

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.