二十三种设计模式之结构型模式之桥接模式
来源:互联网 发布:禁止 域名 ip 编辑:程序博客网 时间:2024/05/23 21:04
桥接模式
概述
将抽象部分与它的实现部分分离,使它们都可以独立地变化。
适用性
1.你不希望在抽象和它的实现部分之间有一个固定的绑定关系。 例如这种情况可能是因为,在程序运行时刻实现部分应可以被选择或者切换。2.类的抽象以及它的实现都应该可以通过生成子类的方法加以扩充。 这时Bridge模式使你可以对不同的抽象接口和实现部分进行组合,并分别对它们进行扩充。3.对一个抽象的实现部分的修改应对客户不产生影响,即客户的代码不必重新编译。4.正如在意图一节的第一个类图中所示的那样,有许多类要生成。 这样一种类层次结构说明你必须将一个对象分解成两个部分。5.你想在多个对象间共享实现(可能使用引用计数),但同时要求客户并不知道这一点。
参与者
1.Abstraction 定义抽象类的接口。 维护一个指向Implementor类型对象的指针。2.RefinedAbstraction 扩充由Abstraction定义的接口。3.Implementor 定义实现类的接口,该接口不一定要与Abstraction的接口完全一致。 事实上这两个接口可以完全不同。 一般来讲,Implementor接口仅提供基本操作,而Abstraction则定义了基于这些基本操作的较高层次的操作。4.ConcreteImplementor 实现Implementor接口并定义它的具体实现。
类图
例子
Abstraction
public abstract class Person { private Clothing clothing; private String type; public Clothing getClothing() { return clothing; } public void setClothing() { this.clothing = ClothingFactory.getClothing(); } public void setType(String type) { this.type = type; } public String getType() { return this.type; } public abstract void dress();}
RefinedAbstraction
public class Man extends Person { public Man() { setType("男人"); } public void dress() { Clothing clothing = getClothing(); clothing.personDressCloth(this); }}public class Lady extends Person { public Lady() { setType("女人"); } public void dress() { Clothing clothing = getClothing(); clothing.personDressCloth(this); }}
Implementor
public abstract class Clothing { public abstract void personDressCloth(Person person);}
ConcreteImplementor
public class Jacket extends Clothing { public void personDressCloth(Person person) { System.out.println(person.getType() + "穿马甲"); }}public class Trouser extends Clothing { public void personDressCloth(Person person) { System.out.println(person.getType() + "穿裤子"); }}
Test
public class Test { public static void main(String[] args) { Person man = new Man(); Person lady = new Lady(); Clothing jacket = new Jacket(); Clothing trouser = new Trouser(); jacket.personDressCloth(man); trouser.personDressCloth(man); jacket.personDressCloth(lady); trouser.personDressCloth(lady); }}
result
男人穿马甲男人穿裤子女人穿马甲女人穿裤子
Copyright © 2008 Angus Young
疯狂Java联盟:http://www.crazyit.org
0 0
- 二十三种设计模式之结构型模式之桥接模式
- 二十三种设计模式之结构型模式之适配器模式
- 二十三种设计模式之结构型模式之组合模式
- 二十三种设计模式之结构型模式之装饰模式
- 二十三种设计模式之结构型模式之外观模式
- 二十三种设计模式之结构型模式之享元模式
- 二十三种设计模式之结构型模式之代理模式
- 二十三种设计模式之桥接模式
- 二十三 设计模式之桥梁模式
- 设计模式之结构型模式二
- 二十三种设计模式之创建型模式之建造者模式
- 二十三种设计模式之创建型模式之单态模式
- 二十三种设计模式之创建型模式之原型模式
- 二十三种设计模式之行为型模式之责任链模式
- 二十三种设计模式之行为型模式之命令模式
- 二十三种设计模式之行为型模式之解释器模式
- 二十三种设计模式之行为型模式之迭代器模式
- 二十三种设计模式之行为型模式之中介者模式
- servlet学习---第一天
- eclipse 中git解决冲突
- matlab实现聚类分析
- 七个比喻拆穿美国“加息婊”的真面目
- # hihocoder #1051 补提交卡
- 二十三种设计模式之结构型模式之桥接模式
- Hibernate的缓存机制
- 题目11 奇偶数分离
- poj2229动态规划
- git pull不了的问题
- 分页查询中sql server和mysql
- LightOJ 1341 Aladdin and the Flying Carpet(算术基本定理)
- 温故而知新-----一、Java常识
- java表格增删改功能