Adapter

Adapter Pattern:将一个类的接口,转换成客户所期望的另一个接口。

1).

Target.java

 

package com.flyingh.target;

public interface Target {
	void request();
}

 

 Adaptee.java

 

package com.flyingh.adaptee;

public class Adaptee {

	public void specificRequest() {
		// TODO Auto-generated method stub
		System.out.println("specific request!");
	}

}

 

Adapter.java

 package com.flyingh.adapter;

import com.flyingh.adaptee.Adaptee;
import com.flyingh.target.Target;

public class Adapter implements Target {
	private Adaptee adaptee;

	public Adapter(Adaptee adaptee) {
		super();
		this.adaptee = adaptee;
	}

	@Override
	public void request() {
		// TODO Auto-generated method stub
		adaptee.specificRequest();
	}

}

 Client.java

 

package com.flyingh.client;

import com.flyingh.adaptee.Adaptee;
import com.flyingh.adapter.Adapter;
import com.flyingh.target.Target;

public class Client {
	public static void main(String[] args) {
		Adaptee adaptee = new Adaptee();
		Target adapter = new Adapter(adaptee);
		adapter.request();
	}
}

 

 程序运行结果如下:

 

specific request!

 

2).将Iterator适配成Enumeration,并用Set测试:

IteratorAdapter.java

 

package com.flyingh.adapter;

import java.util.Enumeration;
import java.util.Iterator;

public class IteratorAdapter<E> implements Enumeration<E> {
	private Iterator<E> iterator;

	public IteratorAdapter(Iterator<E> iterator) {
		super();
		this.iterator = iterator;
	}

	@Override
	public boolean hasMoreElements() {
		// TODO Auto-generated method stub
		return iterator.hasNext();
	}

	@Override
	public E nextElement() {
		// TODO Auto-generated method stub
		return iterator.next();
	}

}

 

 Client.java

 

package com.flyingh.client;

import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.flyingh.adapter.IteratorAdapter;

public class Client {
	public static void main(String[] args) {
		Set<String> set = new HashSet<String>();
		set.add("zhangsan");
		set.add("lisi");
		set.add("wangwu");
		set.add("zhaoliu");
		set.add("sunqi");
		set.add("qianba");
		System.out.println("**********directly*********");
		System.out.println(set);
		System.out.println("**********foreach(iterator)**********");
		for (String string : set) {
			System.out.print(string + " ");
		}
		System.out.println("\n********iterator***********");
		Iterator<String> iterator = set.iterator();
		while (iterator.hasNext()) {
			System.out.print(iterator.next() + " ");
		}
		System.out.println("\n********enumeration********");
		iterator = set.iterator();
		Enumeration<String> enumeration = new IteratorAdapter<String>(iterator);
		while (enumeration.hasMoreElements()) {
			System.out.print(enumeration.nextElement() + " ");
		}
	}
}

 

 程序运行结果如下:

 

**********directly*********
[zhaoliu, sunqi, wangwu, qianba, lisi, zhangsan]
**********foreach(iterator)**********
zhaoliu sunqi wangwu qianba lisi zhangsan 
********iterator***********
zhaoliu sunqi wangwu qianba lisi zhangsan 
********enumeration********
zhaoliu sunqi wangwu qianba lisi zhangsan 

 

 3).Default Adapter:

Target.java

 

package com.flyingh.target;

public interface Target {
	void fun1();

	void fun2();

	void fun3();

	void fun4();

	void fun5();
}

 

 Adapter.java

 

package com.flyingh.adapter;

import com.flyingh.target.Target;

public abstract class Adapter implements Target {

	@Override
	public void fun1() {
		// TODO Auto-generated method stub

	}

	@Override
	public void fun2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void fun3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void fun4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void fun5() {
		// TODO Auto-generated method stub

	}

}

 

 Adaptee.java

 

package com.flyingh.adaptee;

import com.flyingh.adapter.Adapter;

public class Adaptee extends Adapter {

	@Override
	public void fun2() {
		// TODO Auto-generated method stub
		System.out.println("fun2()");
	}

	@Override
	public void fun3() {
		// TODO Auto-generated method stub
		System.out.println("fun3()");
	}

}

 

 Client.java

 

package com.flyingh.client;

import com.flyingh.adaptee.Adaptee;
import com.flyingh.target.Target;

public class Client {
	public static void main(String[] args) {
		Target adaptee = new Adaptee();
		adaptee.fun2();
		adaptee.fun3();
	}
}

 

 程序运行结果如下:

 

fun2()
fun3()
 

 

你可能感兴趣的:(Adapter)