java23中设计模式之桥接模式
-- 不用桥接模式的实现
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.bjsxt.bridge; public interface Computer { void sale(); } class Desktop implements Computer { @Override public void sale() { System.out.println("销售台式机!"); } } class Laptop implements Computer { @Override public void sale() { System.out.println("销售笔记本!"); } } class Pad implements Computer { @Override public void sale() { System.out.println("销售平板电脑!"); } } class LenovoDesktop extends Desktop { @Override public void sale() { System.out.println("销售联想台式机"); } } class LenovoLaptop extends Laptop { @Override public void sale() { System.out.println("销售联想笔记本"); } } class LenovoPad extends Pad { @Override public void sale() { System.out.println("销售联想平板电脑"); } } class ShenzhouDesktop extends Desktop { @Override public void sale() { System.out.println("销售神舟台式机"); } } class ShenzhouLaptop extends Laptop { @Override public void sale() { System.out.println("销售神舟笔记本"); }
--使用桥接模式实现
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.bjsxt.bridge; /** * 品牌 * @author Administrator * */ public interface Brand { void sale(); } class Lenovo implements Brand { @Override public void sale() { System.out.println("销售联想电脑"); } } class Dell implements Brand { @Override public void sale() { System.out.println("销售Dell电脑"); } } class Shenzhou implements Brand { @Override public void sale() { System.out.println("销售神舟电脑"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.bjsxt.bridge; /** * 电脑类型的维度 * @author Administrator * */ public class Computer2 { protected Brand brand; public Computer2(Brand b) { this.brand = b; } public void sale(){ brand.sale(); } } class Desktop2 extends Computer2 { public Desktop2(Brand b) { super(b); } @Override public void sale() { super.sale(); System.out.println("销售台式机"); } } class Laptop2 extends Computer2 { public Laptop2(Brand b) { super(b); } @Override public void sale() { super.sale(); System.out.println("销售笔记本"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.bjsxt.bridge; public class Client { public static void main(String[] args) { //销售联想的笔记本电脑 Computer2 c = new Laptop2(new Lenovo()); c.sale(); //销售神舟的台式机 Computer2 c2 = new Desktop2(new Shenzhou()); c2.sale(); } }