Briage桥接设计模式
设计模式 桥接
2023-09-14 09:02:00 时间
目录
在抽象和具体实现类两个维度同时发展,不会产生类爆炸的问题。
概念
程序模拟
版本一
帅哥类
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!");
}
}
相关文章
- 从ES6重新认识JavaScript设计模式: 装饰器模式
- 设计模式的C语言应用-状态机模式-第二章
- 设计模式 -- 桥接模式(Bridge Pattern)
- 设计模式--桥接模式
- 【设计模式】桥接模式
- Tomcat 系统架构与设计模式
- cocos2d-x设计模式发掘之三:管理者模式
- 设计模式大杂烩(概念)
- 实例解析Python设计模式编程之桥接模式的运用
- 设计模式--桥接模式
- 【设计模式】MVC模式
- 【设计模式】装饰者模式
- 【设计模式】桥接模式
- JavaScript面试系列:JavaScript设计模式之桥接模式和懒加载
- Java代理设计模式(Proxy)的具体实现:静态代理和动态代理
- 设计模式 - 组合模式(composite pattern) 迭代器(iterator) 具体解释
- python 设计模式之桥接模式 Bridge Pattern
- 设计模式 Template Method模式 显示程序猿的一天
- 设计模式入门之桥接模式Bridge
- UI自动化测试最佳设计模式POM,不会还不知道吧
- 中级软件设计师考试(软考中级)设计模式分类及其典型特点
- 也来学学设计模式