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

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();
	}
}

你可能感兴趣的:(java,设计模式,编程,算法,适配器)