Briage桥接设计模式
生活随笔
收集整理的这篇文章主要介绍了
Briage桥接设计模式
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
目录
概念
程序模拟
版本一
版本二:礼物各种各样
版本三:类爆炸演示
版本四:桥接模式
在抽象和具体实现类两个维度同时发展,不会产生类爆炸的问题。
概念
程序模拟
版本一
帅哥类
GG要追MM,想要给MM礼物
public class GG {public void chase(MM mm) {Gift g = new Book();give(mm, g);}public void give(MM mm, Gift g) {}}MM类
public class MM {String name; }礼物类
public abstract class Gift {} public class Book extends Gift {} public class Flower extends Gift {}版本二:礼物各种各样
礼物有各种各样的,如果用继承的话,就会产生类的大爆炸!
public abstract class Gift {} public class Book extends Gift {} public class Flower extends Gift {} public class WarmGift extends Gift {} public class WildGift extends Gift {}版本三:类爆炸演示
各种类型的礼物层出不穷:
/*** 或者从WarmGift继承* 或者从Flower继承*/ public class WarmFlower extends Flower { }版本四:桥接模式
public abstract class Gift {GiftImpl impl; } public class GiftImpl {} public class WarmGift extends Gift {public WarmGift(GiftImpl impl) {this.impl = impl;} } public class WildGift extends Gift {public WildGift(GiftImpl impl) {this.impl = impl;} } public class Book extends GiftImpl {} public class Flower extends GiftImpl {} public class MM {String name; }这么来使用:
public class GG {public void chase(MM mm) {Gift g = new WarmGift(new Flower());give(mm, g);}public void give(MM mm, Gift g) {System.out.println(g + "gived!");}}与50位技术专家面对面20年技术见证,附赠技术全景图
总结
以上是生活随笔为你收集整理的Briage桥接设计模式的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: Adapter适配器设计模式
- 下一篇: Command命令设计模式