标签:view 组合 桥接 display 桥接模式 很多 需要 图片 分享图片
1.背景
追MM的例子,Boy追MM,需要送礼物Gift,有Flower,Ring继承自Gift;
还有WarmGift温暖的礼物,WildGift狂野的礼物;
Boy:
public class Boy { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void pursue(MM mm){ Gift g = new Ring(); give(g, mm); } public void give(Gift g, MM mm){ } }
MM
public class MM { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
其他的一些礼物类,都继承于Gift:
public class Gift { } public class Flower extends Gift{ } public class Ring extends Gift{ } public class WarmGift extends Gift{ } public class WildGift extends Gift{ }
Gift在往子类扩展,扩展类型的时候,它会有两个维度上的扩展;
/** * 礼物具体的实现 * @author CY * */ public class GiftImpl extends Gift{ }
Gift:
public class Gift { protected GiftImpl impl; }
Flower:
public class Flower extends GiftImpl{ }
Ring
public class Ring extends GiftImpl{ }
WarmGift:
public class WarmGift extends Gift{ public WarmGift(GiftImpl impl){ this.impl = impl; } }
WildGift:
public class WildGift extends Gift{ public WildGift(GiftImpl impl){ this.impl = impl; } }
Boy:
public class Boy { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void pursue(MM mm){ //Gift g = new WarmGift(new Flower()); //温暖的花 Gift g = new WildGift(new Ring()); //狂野的戒指 give(g, mm); } public void give(Gift g, MM mm){ } }
感觉有一点绕,需要仔细体会下....
3.小结:
标签:view 组合 桥接 display 桥接模式 很多 需要 图片 分享图片
原文地址:https://www.cnblogs.com/tenWood/p/9136392.html