设计模式 - 适配器模式(adapter pattern) 枚举器和迭代器 具体解释

Posted yangykaifa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式 - 适配器模式(adapter pattern) 枚举器和迭代器 具体解释相关的知识,希望对你有一定的参考价值。

适配器模式(adapter pattern) 枚举器和迭代器 具体解释


本文地址: http://blog.csdn.net/caroline_wendy


參考适配器模式(adapter pattern): http://blog.csdn.net/caroline_wendy/article/category/2281679


Java早期版本号的枚举器(Enumeration)和如今的迭代器(Iterator) 能够使用适配器(adapter)进行转换.


适配器(adapter)代码:

/**
 * @time 2014年6月17日
 */
package adapter.enum_iter;

import java.util.*;

/**
 * @author C.L.Wang
 *
 */
public class EnumerationIterator implements Iterator {

	Enumeration enumeration;
	
	/**
	 * 
	 */
	public EnumerationIterator(Enumeration enumeration) {
		// TODO Auto-generated constructor stub
		this.enumeration = enumeration;
	}
	
	public boolean hasNext() {
		return enumeration.hasMoreElements();
	}
	
	public Object next() {
		return enumeration.nextElement();
	}
	
	public void remove() {
		throw new UnsupportedOperationException();
	}

}

測试代码:

/**
 * @time 2014年6月17日
 */
package adapter.enum_iter;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Vector;

/**
 * @author C.L.Wang
 *
 */
public class EnumerationIteratorTestDrive {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Vector v = new Vector(Arrays.asList(args));
		Iterator iterator = new EnumerationIterator(v.elements());
		while (iterator.hasNext()) {
			System.out.println(iterator.next());
		}
	}

}



技术分享














以上是关于设计模式 - 适配器模式(adapter pattern) 枚举器和迭代器 具体解释的主要内容,如果未能解决你的问题,请参考以下文章

Adapter适配器模式

设计模式:适配器模式(Adapter)

Adapter (适配器模式)

七适配器(Adapter)模式--结构模式(Structural Pattern)

Adapter适配器设计模式

设计模式--适配器模式(Adapter)详解