zoukankan      html  css  js  c++  java
  • 【设计模式】7、桥接模式

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

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

      

  • 相关阅读:
    Days like that:
    获取网站访问来源URL
    论坛里面的一个帖子
    Days like that:
    简单项目布署
    论坛里面的一个帖子
    获取网站访问来源URL
    piwik开放源代码的Web统计软件
    p标签里面不能嵌套div
    IO操作 第二篇 学习(转载)
  • 原文地址:https://www.cnblogs.com/cutter-point/p/5196456.html
Copyright © 2011-2022 走看看