一:定义:
Adapter:Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
二:引入
先来看张图:
墙上的插座是三相的,而我手头的电器(如吹风机是两相的),那么我们就需要一个三相转两相的一个插头.
这个三相转两相的转换器就叫Adapter,对已有的接口不符合我们需要的接口时,写一个转换的接口,来完成对接功能.
/**//*
* 已有接口
*/
public class Util ...{
public static void printCollection(Iterator iter)
...{
while(iter.hasNext())
...{
System.out.println(iter.next());
}
}
}
public class Client ...{
public static void main(String[] args) ...{
List list=new ArrayList();
list.add("L1");
list.add("L2");
list.add("L3");
Util.printCollection(list.iterator());
Vector vct=new Vector();
vct.add("v1");
vct.add("v2");
vct.add("v3");
//Util.printCollection(vct.elements()); 不符合接口
Util.printCollection(new EnumerationIterator(vct.elements()));
}
}
/**//*
* Adapter--Adapter,Iterator 是target,目标是要转成Iterator
*/
public class EnumerationIterator implements Iterator...{
private Enumeration enumeration;
//Enumeration为Adaptee
public EnumerationIterator(Enumeration enumeration)
...{
this.enumeration=enumeration;
}
public boolean hasNext() ...{
return enumeration.hasMoreElements();
}
public Object next() ...{
return enumeration.nextElement();
}
public void remove() ...{
}
}
三:结构
adapter class
adapter object
四:实际应用
-
五:优缺点
参考文献:
1:阎宏,《Java与模式》,电子工业出版社
2:Eric Freeman & Elisabeth Freeman,《Head First Design Pattern》,O'REILLY
一:定义:
Decorator:Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
二:引入
假设现在有一家咖啡店,经营咖啡,茶等饮料,我们来为它设计一个系统。
问题:
- 饮料加糖或牛奶等调料时是要另收费的(每包1元),顾客可以要也可以不要,所以这个固定价格的cost方法不符合要求. 不符合OCP(open for extension,close for modification)
重新设计:
很显然,这种设计也不合适,会导致
- 类数量爆炸性增长(class exploson).
- 也不符合要求,比如有的顾客要两包糖等情况没有考虑到.
引入Decorator设计模式:
public abstract class Beverage ...{
private String discription;
public String getDiscription() ...{
return discription;
}
public void setDiscription(String discription) ...{
this.discription = discription;
}
public abstract double cost() ;
}
public class Coffee extends Beverage...{
public Coffee()
...{
setDiscription("coffee");
}
public double cost()
...{
return 10;
}
}
public abstract class CondimentDecorator extends Beverage...{
public abstract String getDiscription() ;
}
public class CondimentMilk extends CondimentDecorator...{
private Beverage beverage;
public CondimentMilk(Beverage beverage)
...{
this.beverage=beverage;
}
public double cost()
...{
return beverage.cost()+1;
}
public String getDiscription()
...{
return beverage.getDiscription()+",milk";
}
}
public class Client ...{
public static void main(String args[])
...{
Beverage coffee=new Coffee();
System.out.println(coffee.getDiscription()+":"+coffee.cost());
System.out.println(new CondimentMilk(coffee).getDiscription()+":"+new CondimentMilk(coffee).cost());
System.out.println(new CondimentSugar(new CondimentMilk(coffee)).getDiscription()+":"+new CondimentSugar( new CondimentMilk(coffee)).cost());
System.out.println(new CondimentSugar(new CondimentSugar(new CondimentMilk(coffee))).getDiscription()+":"+new CondimentSugar(new CondimentSugar( new CondimentMilk(coffee))).cost());
}
}
三:结构
四:实际应用
- JAVA IO
五:适用情形
Use the Adapter pattern when
- you want to use an existing class, and its interface does not match the one you need.
- you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces.
- (object adapter only) you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.
参考文献:
1:阎宏,《Java与模式》,电子工业出版社
2:Eric Freeman & Elisabeth Freeman,《Head First Design Pattern》,O'REILLY
分享到:
相关推荐
c++设计模式-结构型模式-适配器模式,其他工程,c++源码。适配器模式(Adapter)的定义如下:将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类能一起工作。
适配器模式(Adapter Pattern) 是作为两个不兼容接口之间的桥梁, 这种类型的设计模式属于结构型模式。 一些书籍也称适配器模式为缺省适配器模式(Default Adapter Pattern) 。 适配器模式主要分 为三类: 类...
在这里与各位分享本人从网络上下载的C#面向对象设计模式纵横谈系列视频,共有25节,除了第一节需要各位贡献一点资源分以作为对本人上传资源的回馈,后面的其他资源均不需要... 这是第7节:结构型模式Adapter 适配器模式
3、适配器模式(Adapter) 用意:把一个类的接口变换成客户所期待的另外一个接口。
C#面向对象设计模式纵横谈(7):Adapter 适配器模式(结构型模式) 体验课程
C#面向对象设计模式纵横谈(7):Adapter 适配器模式(结构型模式) (Level 300)
适配器模式是一种结构型设计模式,它允许接口不兼容的两个类可以协同工作。以下是该模式的要点: 1. **角色**: - **Target(目标接口)**:客户端期望调用的接口。 - **Adaptee(适配者)**:现有的、接口与目标...
创建型模式 (100%) 设计模式面面观(8):创建型模式总结 (100%) 设计模式面面观(9):适配器模式(Adapter Pattern)-结构型模式 (100%) 设计模式面面观(10):桥接模式(Bridge Pattern)-结构型模式 ...
NULL 博文链接:https://jacky-dai.iteye.com/blog/1929106
4.1 Adapter(适配器)—类对象结构型模式 92 4.2 Bridge(桥接)—对象结构型模式 100 4.3 Composite(组成)—对象结构型模式 107 4.4 Decorator(装饰)—对象结构型模式 115 4.5 FACADE(外观)—对象结构型模式...
4.1 ADAPTER(适配器)——对象结构型模式 4.2 BRIDGE(桥接)——对象结构型模式 4.3 COMPOSITE(组成)——对象结构型模式 4.4 DECORATOR(装饰)——对象结构型模式 4.5 FACADE(外观)——对象结构型模式 4.6 ...
这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。 这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口功能。举个真实的例子,读卡器是作为内存卡和笔记本之间的适配器。您将内存卡插入...
这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。 这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口功能。举个真实的例子,读卡器是作为内存卡和笔记本之间的适配器。您将内存卡插入...
主要介绍了PHP设计模式:适配器模式Adapter,结合实例形式详细分析了PHP适配器模式Adapter基本概念、功能、原理、用法及操作注意事项,需要的朋友可以参考下
适配器模式(Adapter Pattern) 7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. ...
结构型模式 Adapter-------->适配器模式 Bridge-------->桥接模式 Composite-------->组合模式 Decorator-------->装饰模式 Facade-------->外观模式 FlyWeight-------->享元模式 Proxy--------&...
C#面向对象设计模式纵横谈(7):Adapter 适配器模式(结构型模式) C#面向对象设计模式纵横谈(8):Bridge 桥接模式(结构型模式) C#面向对象设计模式纵横谈(9):Composite 组合模式(结构型模式) C#面向对象设计模式...
前几篇介绍了设计模式的特性并且详细讲解了4种创建型模式,创建型模式是负责如何产生对象实例的,接下来讲讲结构型模式。 一、什么是结构型模式? 结构型模式是解析类和对象的内部结构和外部组合,通过优化程序结构...