java适配器设计模式源码示例

发布于: 2014-21-11   作者:chaoyong918   来源:转载   浏览:
package cn.jinzhaow.www;

interface PowerA{
	void insert();
}

interface PowerB{
	void set();
}

class PowerAImpl implements PowerA{
	public void insert(){
		System.out.println("插头开始连接中。。。");
	}
}

class PowerBImpl implements PowerB{
	public void set(){
		System.out.println("插座已被连接。。。");
	}
}

class PowerAdapter implements PowerA{
	private PowerB b;
	public PowerAdapter(PowerB b){
		this.b = b;
	}
	public void insert(){
		b.set();
	}
}

public class ATest {
	public static void main(String[] args) {
		PowerA a = new PowerAImpl();
		insert(a);
		
		PowerB b = new PowerBImpl();
		PowerAdapter pa = new PowerAdapter(b);
		insert(pa);
	}
	
	public static void insert(PowerA a){
		a.insert();
	}
}

line
IT610手机版