星空网 > 软件开发 > ASP.net

【设计模式】7、桥接模式

桥接模式就是对一个类的方法进行抽象化,吧不相关的因素提取出来,发展出第二个类

 

【设计模式】7、桥接模式

 

 1 package com.shejimoshi.structural.Bridge; 2  3  4 /** 5  * 功能:桥接模式使用 6  *    意图:将抽象部分与它的实现部分分离,使他们都可以独立的变化 7  *   适用性:你不希望在抽象和它的实现部分之间有一个固定的绑定关系。 8  *      类的抽象以及它的实现都应该可以通过生成子类的方法加以扩充 9  *     对一个抽象的实现部分的修改应对客户不产生影响,即客户的代码不必重新编译10  *     你想在多个对象间共享实现,但同时要求客户并不知道这一点11  * 时间:2016年2月17日下午7:45:1012  * 作者:cutter_point13 */14 public abstract class Systeml15 {16   //相应系统的软件17   protected Soft soft;18   19   public abstract void using();20   21   public void installSoft(Soft soft)22   {23     this.soft = soft;24   }25 }

 

 1 package com.shejimoshi.structural.Bridge; 2  3  4 /** 5  * 功能:桥接模式使用 6  *    意图:将抽象部分与它的实现部分分离,使他们都可以独立的变化 7  *   适用性:你不希望在抽象和它的实现部分之间有一个固定的绑定关系。 8  *      类的抽象以及它的实现都应该可以通过生成子类的方法加以扩充 9  *     对一个抽象的实现部分的修改应对客户不产生影响,即客户的代码不必重新编译10  *     你想在多个对象间共享实现,但同时要求客户并不知道这一点11  * 时间:2016年2月17日下午7:54:1212  * 作者:cutter_point13 */14 public class Window extends Systeml15 {16   //默认构造函数17   public Window(){}18   19   public Window(Soft soft)20   {21     //给系统安装相应的软件22     this.soft = soft;23   }24   25   @Override26   public void using()27   {28     System.out.print("Window 系统运行:");29     soft.run();//系统运行相应的软件30   }31   32 }

 

 1 package com.shejimoshi.structural.Bridge; 2  3  4 /** 5  * 功能:桥接模式使用 6  *    意图:将抽象部分与它的实现部分分离,使他们都可以独立的变化 7  *   适用性:你不希望在抽象和它的实现部分之间有一个固定的绑定关系。 8  *      类的抽象以及它的实现都应该可以通过生成子类的方法加以扩充 9  *     对一个抽象的实现部分的修改应对客户不产生影响,即客户的代码不必重新编译10  *     你想在多个对象间共享实现,但同时要求客户并不知道这一点11  * 时间:2016年2月17日下午7:57:1212  * 作者:cutter_point13 */14 public class Linux extends Systeml15 {16   //默认构造函数17   public Linux(){}18   19   public Linux(Soft soft)20   {21     //给系统安装相应的软件22     this.soft = soft;23   }24   25   @Override26   public void using()27   {28     System.out.print("Linux 系统运行:");29     soft.run();//系统运行相应的软件30   }31 32 }

 

 1 package com.shejimoshi.structural.Bridge; 2  3  4 /** 5  * 功能:桥接模式使用 6  *    意图:将抽象部分与它的实现部分分离,使他们都可以独立的变化 7  *   适用性:你不希望在抽象和它的实现部分之间有一个固定的绑定关系。 8  *      类的抽象以及它的实现都应该可以通过生成子类的方法加以扩充 9  *     对一个抽象的实现部分的修改应对客户不产生影响,即客户的代码不必重新编译10  *     你想在多个对象间共享实现,但同时要求客户并不知道这一点11  * 时间:2016年2月17日下午7:52:5812  * 作者:cutter_point13 */14 public interface Soft15 {16   public void run();17 }

 

 1 package com.shejimoshi.structural.Bridge; 2  3  4 /** 5  * 功能:编译器 6  * 时间:2016年2月17日下午7:58:17 7  * 作者:cutter_point 8 */ 9 public class Compiler implements Soft10 {11 12   @Override13   public void run()14   {15     System.out.println("运行编译器");16   }17 18 }

 

 1 package com.shejimoshi.structural.Bridge; 2  3  4 /** 5  * 功能:浏览器 6  * 时间:2016年2月17日下午8:00:09 7  * 作者:cutter_point 8 */ 9 public class Browser implements Soft10 {11 12   @Override13   public void run()14   {15     System.out.println("运行浏览器");16   }17 18 }

 

 1 package com.shejimoshi.structural.Bridge; 2  3  4 /** 5  * 功能:测试桥接模式 6  * 时间:2016年2月17日下午8:06:05 7  * 作者:cutter_point 8 */ 9 public class Test10 {11   public static void main(String[] args)12   {13     //我们的软件14     Soft browser = new Browser();15     Soft compiler = new Compiler();16     17     //我们在window系统上安装浏览器使用18     Systeml win = new Window();19     win.installSoft(browser);20     win.using();21     22     System.out.println("===============================");23     //Linux 上安装浏览器还要编译器24     Systeml ubuntu = new Linux(browser);25     ubuntu.using();26     ubuntu.installSoft(compiler);27     ubuntu.using();28     29   }30 }

测试结果:

 

Window 系统运行:运行浏览器===============================Linux 系统运行:运行浏览器Linux 系统运行:运行编译器

  

 




原标题:【设计模式】7、桥接模式

关键词:设计模式

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

敦煌网怎么卖东西:https://www.goluckyvip.com/tag/30848.html
敦煌网怎么用:https://www.goluckyvip.com/tag/30849.html
炎炎夏日:https://www.goluckyvip.com/tag/3085.html
敦煌网怎样:https://www.goluckyvip.com/tag/30850.html
敦煌网支付:https://www.goluckyvip.com/tag/30851.html
敦煌网智能营销计划:https://www.goluckyvip.com/tag/30852.html
恐怖游轮2002 恐怖游轮2022:https://www.vstour.cn/a/365178.html
时尚电商平台Meesho拟融资3亿美元!:https://www.kjdsnews.com/a/1836524.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流