加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

第22章《桥接模式》

发布时间:2020-12-13 20:56:21 所属栏目:百科 来源:网络整理
导读:/** @Author: cxh @CreateTime: 18/1/25 22:45 @ProjectName: JavaBaseTest */ public class BrandA1 extends PhoneBrand { @Override public void run() { super.softWare.run(); } } /** @Author: cxh @CreateTime: 18/1/25 22:47 @ProjectName: JavaBaseT

/**

  • @Author: cxh
  • @CreateTime: 18/1/25 22:45
  • @ProjectName: JavaBaseTest
    */
    public class BrandA1 extends PhoneBrand {
    @Override
    public void run() {
    super.softWare.run();
    }
    }


/**

  • @Author: cxh
  • @CreateTime: 18/1/25 22:47
  • @ProjectName: JavaBaseTest
    */
    public class BrandA2 extends PhoneBrand {
    @Override
    public void run() {
    super.softWare.run();
    }
    }


/**

  • @Author: cxh
  • @CreateTime: 18/1/25 22:46
  • @ProjectName: JavaBaseTest
    */
    public class BrandB1 extends PhoneBrand {
    @Override
    public void run() {
    super.softWare.run();
    }
    }


/**

  • @Author: cxh

  • @CreateTime: 18/1/25 22:51

  • @ProjectName: JavaBaseTest

  • 测试类
    */
    public class Client {
    public static void main(String[] args) {
    PhoneBrand brand;

     //手机品牌A1
     System.out.println("----手机品牌A1----");
     brand=new BrandA1();
     brand.setSoftWare(new SoftWareV1());
     brand.run();
     brand.setSoftWare(new SoftWareV2());
     brand.run();
     brand.setSoftWare(new SoftWareV3());
     brand.run();
    
     //手机品牌A2
     System.out.println("----手机品牌A2----");
     brand=new BrandA2();
     brand.setSoftWare(new SoftWareV1());
     brand.run();
     brand.setSoftWare(new SoftWareV2());
     brand.run();
     brand.setSoftWare(new SoftWareV3());
     brand.run();
    
     //手机品牌A3
     System.out.println("----手机品牌A3----");
     brand=new BrandB1();
     brand.setSoftWare(new SoftWareV1());
     brand.run();
     brand.setSoftWare(new SoftWareV2());
     brand.run();
     brand.setSoftWare(new SoftWareV3());
     brand.run();

    }
    }



/**

  • @Author: cxh

  • @CreateTime: 18/1/25 22:40

  • @ProjectName: JavaBaseTest
    */
    public abstract class PhoneBrand {
    //持有软件的引用
    protected PhoneSoftWare softWare;
    public void setSoftWare(PhoneSoftWare softWare) {
    this.softWare = softWare;
    }

    //调用软件功能方法
    public abstract void run();
    }



/**

  • @Author: cxh
  • @CreateTime: 18/1/25 22:39
  • @ProjectName: JavaBaseTest
    */
    public abstract class PhoneSoftWare {
    abstract void run();
    }


/**

  • @Author: cxh
  • @CreateTime: 18/1/25 22:48
  • @ProjectName: JavaBaseTest
    */
    public class SoftWareV1 extends PhoneSoftWare {
    @Override
    void run() {
    System.out.println("手机通信录功能运行");
    }
    }


/**

  • @Author: cxh
  • @CreateTime: 18/1/25 22:49
  • @ProjectName: JavaBaseTest
    */
    public class SoftWareV2 extends PhoneSoftWare{
    @Override
    void run() {
    System.out.println("手机游戏功能运行");
    }
    }


/**

  • @Author: cxh
  • @CreateTime: 18/1/25 22:50
  • @ProjectName: JavaBaseTest
    */
    public class SoftWareV3 extends PhoneSoftWare {
    @Override
    void run() {
    System.out.println("手机拍照功能运行");
    }
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读