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命令设计模式