适配器模式 :将一个类的接口,转换成客户期望的另一个接口.适配器上原本接口不兼容的类可以合作无间.
项目比较旧的代码,迭代采用Enumeration
Java代码
public class EnumerationTest { public Enumeration getEnum(Vector list){ Enumeration e = list.elements(); return e; } }
项目比较旧的代码,迭代采用Iterator
Java代码
public class IteratorTest { public Iterator getIter(Collection list) { Iterator iterator = list.iterator(); return iterator; } }
Enumeration 转换成 Iterator 的适配器
Java代码
public class EnumerationIterator implements Iterator { public Enumeration enumeration; public EnumerationIterator(Enumeration enumeration) { this.enumeration = enumeration; } @Override public boolean hasNext() { // TODO Auto-generated method stub return enumeration.hasMoreElements(); } @Override public Object next() { // TODO Auto-generated method stub return enumeration.nextElement(); } //由于Enumeration没有相关删除方法,所以执行时抛出异常 @Deprecated public void remove() { // TODO Auto-generated method stub throw new RuntimeException(); } }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索iterator
, enumeration
, 代码
, 适配器
, public
enumerate
适配器设计模式、java适配器设计模式、ios 适配器设计模式、head first 设计模式、headfirst设计模式pdf,以便于您获取更多的相关知识。
时间: 2024-08-08 12:18:41