设计模式-适配器模式

适配器模式将一个类的接口,转换成客户期望的另一个接口。适配器让原本接口不兼容的类
可以合作无间。
public interface Target {
	public void request();
}

public class Adaptee {
	
	public void specificRequest() {
		
	}
}

public class Adapter implements Target {
	
	Adaptee adaptee;
	
	public Adapter(Adaptee adaptee) {
		this.adaptee = adaptee;
	}
	
	@Override
	public void request() {
		this.adaptee.specificRequest();
	}

}


public class EnumerationIterator<E> implements Iterator<E> {

	private Enumeration<E> enumeration;
	
	public EnumerationIterator(Enumeration<E> enu) {
		this.enumeration = enu;
	}
	
	@Override
	public boolean hasNext() {
		return this.enumeration.hasMoreElements();
	}

	@Override
	public E next() {
		return this.enumeration.nextElement();
	}

	@Override
	public void remove() {
		throw new UnsupportedOperationException();
	}

}

public class IteratorEnumation<E> implements Enumeration<E> {

	private Iterator<E> iterator;
	
	public IteratorEnumation(Iterator<E> iterator) {
		this.iterator = iterator;
	}
	
	@Override
	public boolean hasMoreElements() {
		return this.iterator.hasNext();
	}

	@Override
	public E nextElement() {
		return this.iterator.next();
	}

}
public class TestArrayList {

	public static void main(String[] args) {

		List<String> list = new ArrayList<String>();
		list.add("zhang");
		list.add("zhao");
		list.add("yu");
		IteratorEnumation<String> enumation = new IteratorEnumation<String>(list.iterator());
		
		while (enumation.hasMoreElements()) {
			System.out.println(enumation.nextElement());
		}
		
	}

}

你可能感兴趣的:(设计模式)