Design Pattern _ Bridge pattern

Source: Internet
Author: User

Design Pattern _ Bridge pattern

Bridge Pattern

Decouple an independent action from its implementation so that the two can vary independently)

 

 

ExampleTianxianxi, all of which are Lili; the world is bustling, all to benefit from reality and structure separated clothing company = clothing + Company = clothing company before decoupling is a clothing company, write a Corp abstract class, then, after decoupling, it becomes clothing + Company. clothing is abstracted into products, while the company is abstracted as a company. The creator can change independently and achieve decoupling at the abstract level.
Product Abstraction
public abstract class Product { public abstract void beProducted(); public abstract void beSelled();} 

 

Company Abstraction

 

public abstract class Corp { private Product product;  public Corp(Product _product){  this.product=_product; }  public void makeMoney(){  this.product.beProducted();  this.product.beSelled(); }}

 

 

The house is also a product, isn't it?

Public class House extends Product {@ Override public void beProducted () {System. out. println (The House produced is like this ...);} @ Override public void beSelled () {System. out. println (The House produced has been sold ....);}}

 

IPod, tall!
Public class IPod extends Product {@ Override public void beProducted () {System. out. println (the ipod produced is like this);} @ Override public void beSelled () {System. out. println (the IPod has been sold );}}

 

Real estate companies only deal with houses
Public class HouseCorp extends Corp {public HouseCorp (House _ house) {super (_ house);} @ Override public void makeMoney () {super. makeMoney (); System. out. println (the real estate company has made a lot of money );}}

 

All products of shanzhai company are sold
Public class ShanZhaiCorp extends Corp {public ShanZhaiCorp (Product _ product) {super (_ product) ;}@ Override public void makeMoney () {super. makeMoney (); System. out. println (I am a shanzhai, I make money );}}

 

Scenario Test

Public static void main (String [] args) {System. out. println (real estate companies run like this); House house = new House (); HouseCorp houseCorp = new HouseCorp (house); houseCorp. makeMoney (); System. out. println (); System. out. println (this is the way ShanZhaiCorp runs); shanZhaiCorp ShanZhaiCorp = new shanZhaiCorp (new IPod (); ShanZhaiCorp. makeMoney ();}

Look at the shanzhai company, what is produced when something is popular, the price is low, the quality is also a little guaranteed.


AdvantagesAbstraction and implementation separation and expansion capabilities NotesMainly consider how to split abstraction and implementation

 

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.