设计模式学习-----适配器模式
简单地说,适配器模式就是 将一个类的接口,转换成客户需要的口味。
一个接口--->另一个接口-----以符合客户。
适配器模式的类图(见底):
[img]C:\Documents and Settings\rudy\桌面\Main.jpg[/img]
这里描述由 枚举适配到迭代器。
当然,这里枚举这个类就是adaptee被适配者,而目标接口为iterator,需要设计一个适配器,讲现有的枚举类转变为我们现在所使用的Iterator接口。
其类图如下
下面为适配器代码
public class EmToIterator implement Iterator{ private Enumeration e ; //has a Enumeration object public EmTorIterator(){ this.e = new Enumeration() ; } public boolean hasnext(){ e.hasMoreElement() ; } public Object next(){ e.nextElement() ; } public void remove(){ //为实现 }}