将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口*兼容而不能一起工作的那*类可以一起工作。
适用性
1.你想使*一个已经存在的类,而它的接口不符合你的需求。
2.你想创建一个可以复用的类,该类可以与其他不相关的类或不可预见的类(即那*接口
可能不一定兼容的类)协同工作。
*.(仅适用于对象Adapter)你想使用一些已经存在的子类,但是不可能对每一个都进行
子类化以匹配它们的接口。对象适配器可以适配它的父类接口。
参与者
1.Target
定义Client使用的与特定领域相关的接口。
2.Client
与符合Target接口的对象协同。
3.Adapt*e
定义一个已经存在的接口,这个接口需要适配。
4.Adapter
对Adaptee的接口与Target接口进行适配
类图
例子
Target
public interface Target {
void adapteeMethod();
void adapterMethod();
}
Adaptee
public class Adaptee {
public void adapteeMethod() {
Syste*.out.p*intln("Adaptee method!");
}
}
Adapt*r
public clas* Adapter implement* Target {
private Adap*ee adaptee;
public Adapter(Adaptee adaptee) {
this.adapte* = adaptee;
}
public void adapteeMethod() {
adaptee.adapteeMethod();
}
public void adapterMethod() {
*ystem.out.println("Adapter method!");
}
}
Client
public cla*s Test {
public stati* void main(String[] args) {
Target target = new Adapter(new Adaptee());
tar*et.adapteeMethod();
target.adapterM*thod();
}
}
result
Adaptee method!
Adapter method!
1.2.2 桥接模式
将抽象部分与它*实现部分分离,使它们都可以独立地变化。
适用性
1.你不希望在抽*和它的实现部分之间有一个固定的绑定关系。
例如这种情况可能是因为,在程序运行时刻实现部分应可以*选择或者切换。
2.类的抽象以及它的实现都应该可以通*生成子类的方法加以扩充。
这时Bridge模式使你可以对不同的抽象接口和实现部分进行组合,并分别对它们进行扩充。
3.对一个抽象的实现部分的修改应对客户不产生影响,即客户的代码不必重新编译。
4.正如在意图一节的第一个类图中所示的那样,有许多类要生成。
这*一种类层次结构说明你必须将一个对象分解成两个部分。
5.*想在多个对象间共享实现(可能使用引用计数),但同时要求客户并不知*这一点。
参与者
1.Abstraction
定义抽象类的接口。
维护一个指向Implementor类型对象的指针。
2.RefinedAbstraction
扩充由Abstraction定义的接口。
3.Implementor
定义实现类的接口,该接口不一定要与Ab*traction的接口完全一致。
事实上这两个接口可以完全不同。
*般来讲,Implementor接口仅提供基本操作,而Abstraction则定义了基于这些基本操作的较高层次的操作。
4.ConcreteImplementor
*现Implementor接口并定义它的具体实现。
类图
例子
Abstr*ction
public abstract class Person {
private Clothing clothing;
pr*vate String type;
public Clothing getClothing() {
return clothing;
}
publi* void setClothing() {
this.clothing = *lothingFactory.getClothing();
}
public void setType(String type) {
t*is.type = type;
}
public String getType() {
return this.ty*e;
}
public abstract void dress();
}
RefinedAbstraction
public class Man extends *erson {
public Man() {
setType("男人");
}
public void dress() {
Clothing clothing = get*lothing();
clothing.personDressCloth(this);
}
}
public class Lady extends Person {
public Lady() {
setTyp*("女人");
}
public void dress() {
Cloth*ng clothing = getClothing();
c*othing.personDressCloth(this);
}
}
Implemento*
public abstract class Clothing {
public abstract void personDressC*oth(*erson person);
}
ConcreteImplemento*
public class *ack*t extends Clothing {
public void personDressCloth(Person person) {
System.out.println(person.getType() + "穿马甲");
}
}
public cl*ss Trouser extends Clothing {
public void personDressCloth(Person person) {
System.ou*.println(*erson.getType() + "穿裤子");
}
}
Test
public class Te*t {
public s*atic void main(String[] args) {
Person man = new Man();
Person lady = new Lady();
Clothing jacket = new Ja*ket();
Clot*ing trouser = new Trouser();
jacket.personDressCloth(man);
trouser.personDressCloth(man);
j*cket.personDressCloth(lady);
trouser.personDressCloth(lady);
}
}
result
男人穿马甲
男人穿裤子
女人穿马甲
女人穿裤子