首页 > 其他 > 详细

设计模式之桥接模式 Bridge

时间:2017-09-04 12:45:20      阅读:272      评论:0      收藏:0      [点我收藏+]

 

技术分享

技术分享

 

 技术分享

 

 代码实现

技术分享
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();
    }
}
调用

 

设计模式之桥接模式 Bridge

原文:http://www.cnblogs.com/qingdaofu/p/7473013.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!