zoukankan      html  css  js  c++  java
  • 设计模式之桥接模式 Bridge

     

     代码实现

    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("销售戴尔电脑");
        }
        
    }
    接口类与实现
    /**
     * 电脑类型的维度
     * @author bzhx
     * 2017年3月13日
     */
    public class Computer {
        
        protected Brand brand;
        
        public Computer(Brand brand) {
            super();
            this.brand = brand;
        }
    
        public void sale(){
            brand.sale();
        }
    }
    
    
    class Desktop extends Computer{
    
        public Desktop(Brand brand) {
            super(brand);
        }
    
        @Override
        public void sale() {
            super.sale();
            System.out.println("销售台式机!");
        }
        
    }
    
    class Laptop extends Computer{
    
        public Laptop(Brand brand) {
            super(brand);
            // TODO Auto-generated constructor stub
        }
    
        @Override
        public void sale() {
            super.sale();
            System.out.println("销售笔记本!");
        }
        
    }
    View Code
    public class Client {
        public static void main(String[] args) {
            //销售联想笔记本
            Computer c = new Laptop(new Lenovo());
            c.sale();
        }
    }
    调用
  • 相关阅读:
    正则表达式
    爬虫原理和网页构造
    简单的博客系统之二
    配置编辑器geany
    linux删除多文件
    eNSP交换路由基础
    NTP centOS6.5
    shell脚本之lftp上传
    进度条
    maketrans与translate函数
  • 原文地址:https://www.cnblogs.com/qingdaofu/p/7473013.html
Copyright © 2011-2022 走看看