Bridge. 在抽象和具体两个维度上同时发展
具体的Gift实现
abstract class GiftImpl {}
class Car extends GiftImpl {
@Override
public String toString() {
return "汽车";
}
}
class Flower extends GiftImpl {
@Override
public String toString() {
return "花朵";
}
}
抽象的Gift描述
abstract class Gift {
protected GiftImpl impl;
public Gift(GiftImpl impl) {this.impl = impl;}
}
class WarmGift extends Gift {
public WarmGift(GiftImpl impl) {super(impl);}
public String toString() {
return "温暖的" + impl.toString();
}
}
class ColdGift extends Gift {
public ColdGift(GiftImpl impl) {super(impl);}
public String toString() {
return "寒冷的" + impl.toString();
}
}
客户端
System.out.println(new WarmGift(new Flower()));
System.out.println(new ColdGift(new Flower()));
System.out.println(new ColdGift(new Car()));