桥接模式

桥接模式简介

桥接模式可以取代多层继承结构、处理多维度变化的场景,将各个维度设计成独立的继承结构,使各个维度可以独立的扩展在抽象层建立联系。

 

桥接模式实际开发中的应用场景

--JDBC驱动程序

--AWT中的Peer架构

--人力资源系统中的奖金计算模块

    奖金分类:个人奖金、团队奖金、激励奖金

    部分分类:人事部门、销售部门、研发部门

 

类图UML

 

代码实现

Brand接口

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("销售戴尔品牌");
    }
    
}
View Code

 

Computer类

public abstract class Computer {
    
    protected Brand brand;

    public Computer() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Computer(Brand brand) {
        super();
        this.brand = brand;
    }

    public Brand getBrand() {
        return brand;
    }

    public void setBrand(Brand brand) {
        this.brand = brand;
    }
    
    
    public abstract void sale();
    
}


class Desktop extends Computer{

    public Desktop() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Desktop(Brand brand) {
        super(brand);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void sale() {
        brand.sale();
        System.out.println("销售台式机");
    }
    
}



class Pad extends Computer{

    public Pad() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Pad(Brand brand) {
        super(brand);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void sale() {
        brand.sale();
        System.out.println("销售平板");
    }
    
}
View Code

 

测试Main类

public class Main {
    
    public static void main(String[] args) {
        
        Computer lenovoPad =new Pad(new Lenovo());
        lenovoPad.sale();
    }

}
View Code

 

posted @ 2018-06-03 21:43  小亮的BLOG  阅读(211)  评论(0)    收藏  举报