当前位置:首页 > 资讯 > info6 > 正文

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

发表于: 2014-10-11   作者:chaoyong918   来源:转载   浏览:
摘要: packagecn.jinzhaow.www; interfacePowerA{ voidinsert(); } interfacePowerB{ voidset(); } classPowerAImplimplementsPowerA{ publicvoidinsert(){ System.out.println("插头开始连接中。。。"); } } classPowerBImplimp
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适配器设计模式源码示例

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号