大话设计模式二十二:桥接模式(手机软件何时统一)
来源:互联网 发布:日经225指数数据分析 编辑:程序博客网 时间:2024/05/22 04:52
桥接模式:
当发现我们需要多角度去分类实现对象,而只用继承会造成大量的类增加,不能满足开放-封闭 原则,就应该考虑用桥接模式了。
// 手机接口public abstract class Mobile {private MobileSoft soft;private String brand;public Mobile(String brand) {this.brand = brand;}public MobileSoft getSoft() {return soft;}public void setSoft(MobileSoft soft) {this.soft = soft;}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public abstract void run();}// nokia手机public class NokiaMobile extends Mobile {public NokiaMobile(String brand) {super(brand);}@Overridepublic void run() {System.out.print("Nokia Mobile: ");this.getSoft().run();}}// moto手机public class MotoMible extends Mobile {public MotoMible(String brand) {super(brand);}@Overridepublic void run() {System.out.print("Moto Mobile: ");this.getSoft().run();}}// 手机软件public abstract class MobileSoft {public abstract void run();}// 手机软件: mp3播放器public class MobileMp3 extends MobileSoft {@Overridepublic void run() {System.out.println("run mobile mp3!");}}// 手机软件: 游戏public class MobileGame extends MobileSoft {@Overridepublic void run() {System.out.println("run mobile game!");}}public class BridgeMain {public static void main(String[] args) {Mobile nokia = new NokiaMobile("Nokia");MobileSoft game = new MobileGame();nokia.setSoft(game);nokia.run();Mobile moto = new MotoMible("Moto");MobileSoft mp3 = new MobileMp3();moto.setSoft(mp3);moto.run();}}
合成/聚合复用原则:
聚合表示一种弱的拥有关系,体现的是A对象可以包含B对象,但B对象不是A对象的一部分(大雁和雁群是聚合关系)。
合成表示一种强的拥有关系,体现了严格的部分和整体的关系,部分和整体的生命周期一样(大雁和翅膀就是合成关系)。
合成/聚合复用原则优点:
优先使用对象的合成/聚合将有助于你保持每个类被封装,这样类和类继承层次会保持较小规模,并且不太可能增长为不可控制的庞然大物。
为什么不用继承?
继承是一种强耦合的关系,父类变,子类也得跟着变,所以我们在用继承时,一定要在是‘is a’ 的关系时再考虑使用。
- 大话设计模式二十二:桥接模式(手机软件何时统一)
- 桥接设计模式-手机软件何时统一-大话设计模式
- 《大话设计模式》—— 读后感(22)手机软件何时统一——桥接模式
- 【结构型模式】《大话设计模式》——读后感 (13)手机软件何时能统一?——桥接模式
- 《大话设计模式》--手机软件何时统一--桥接模式<Bridge>(22)
- 大话设计模式--第22章 手机软件何时统一--桥接模式
- 【设计模式】手机软件何时统一 ---- 桥接模式
- 设计模式:22 手机软件何时统一_桥接模式
- 第二十二章 手机软件何时统一--桥接模式(读书笔记)
- 设计模式(二十二)--桥接模式
- 大话设计模式(二)
- 大话设计模式(二)
- 大话设计模式 二
- 大话设计模式学习(十二)——状态模式
- 大话设计模式读书笔记(十二) 抽象工厂模式
- 大话设计模式十二:外观模式(facade)
- 设计模式(二十二) 策略模式
- 读《大话设计模式》有感(二)
- 控制字符
- nyoj-469-擅长排列的小明II
- HttpURLConnection 下载文本文件
- Hdu 3692 Shade of Hallelujah Mountain (三维空间点的旋转和投影 二维凸包)
- CWinAppEx::GetTooltipManager与CTooltipManager::SetTooltipParams与CTooltipManager::CreateToolTip
- 大话设计模式二十二:桥接模式(手机软件何时统一)
- 大学生为什么要学习C语言
- trie树统计单词出现的个数
- Linux ip命令、ifconfig命令
- 冒泡算法的优化
- 贪心--poj2393
- ZOJ 3430 Detect the Virus 解题报告
- 逆波兰表示法计算器
- Android------- jni返回结构体