桥接模式的核心要点:
处理多层继承结构,处理多维度变化的场景,将各个维度设计成独立的继承结构,使各个维度可以独立的扩展在抽象成建立关联
package bridge;
/***
* 品牌
*
* @author zw
*
*/
public interface Brand {
void sale();
}
class Lenvo implements Brand{
@Override
public void sale() {
// TODO Auto-generated method stub
System.out.println("销售联想电脑");
}
}
class Dell implements Brand{
@Override
public void sale() {
// TODO Auto-generated method stub
System.out.println("销售戴尔电脑");
}
}
package bridge;
/***
* 电脑类型的维度
* @author zw
*
*/
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);
// TODO Auto-generated constructor stub
}
@Override
public void sale() {
// TODO Auto-generated method stub
super.sale();
System.out.println("销售台式机");
}
}
class LapTop extends Computer{
public LapTop(Brand brand) {
super(brand);
// TODO Auto-generated constructor stub
}
@Override
public void sale() {
// TODO Auto-generated method stub
super.sale();
System.out.println("销售笔记本");
}
}
package bridge;
public class Client {
public static void main(String[] args) {
//销售联想的笔记本
Computer c = new LapTop(new Lenvo());
c.sale();
}
}