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