设计模式 之 适配者模式

下载 23种设计模式源码 :http://download.csdn.net/download/knight_black_bob/8936043

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


创建型模式,共五种:
工厂方法模式 抽象工厂模式 单例模式 建造者模式 原型模式

结构型模式,共七种:
适配器模式 装饰器模式 代理模式 外观模式 桥接模式 组合模式 享元模式

行为型模式,共十一种:
策略模式 模板方法模式 观察者模式 迭代子模式 责任链模式 命令模式

备忘录模式 状态模式 访问者模式 中介者模式 解释器模式

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package 设计模式.适配器模式;

public interface Adapter {
	public String printLog();
}
package 设计模式.适配器模式;

public class People {

	
	private String username;
	private String age;
	public People(String username, String age) {
		super();
		this.username = username;
		this.age = age;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	@Override
	public String toString() {
		StringBuffer sb =new StringBuffer();
		sb.append(getClass().getName().substring(getClass().getName().lastIndexOf(".")+1,getClass().getName().length()));
		sb.append("[");
		sb.append("\"username\":\""+username+"\",");
		sb.append("\"age\":\""+age+"\""); 
		sb.append("]");
		return sb.toString();
	}
}
package 设计模式.适配器模式;

public class PeopleLogger implements Adapter{

	private People people;

	public PeopleLogger(People people) {
		this.people = people;
	}
	
	public String printLog(){
		return people.toString();
	}
}
package 设计模式.适配器模式;


//用电器做例子,笔记本电脑的插头一般都是三相的,即除了阳极、阴极外,还有一个地极。而有些地方的电源插座却只有两极,没有地极。电源插座与笔记本电脑的电源插头不匹配使得笔记本电脑无法使用。
//这时候一个三相到两相的转换器(适配器)就能解决此问题,而这正像是本模式所做的事情
public class AdapterTest {
	
	
	public static void main(String[] args) {
		 People p = new People("baoyou", "24");
		 Adapter adp = new PeopleLogger(p);
		 System.out.println(adp.printLog());
	}
	

}

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

package 设计模式.适配器模式;

public interface Adapter {
	public String printLog();
}
package 设计模式.适配器模式;

public class People {

	
	private String username;
	private String age;
	public People(String username, String age) {
		super();
		this.username = username;
		this.age = age;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	@Override
	public String toString() {
		StringBuffer sb =new StringBuffer();
		sb.append(getClass().getName().substring(getClass().getName().lastIndexOf(".")+1,getClass().getName().length()));
		sb.append("[");
		sb.append("\"username\":\""+username+"\",");
		sb.append("\"age\":\""+age+"\""); 
		sb.append("]");
		return sb.toString();
	}
}
package 设计模式.适配器模式;

public class PeopleLogger implements Adapter{

	private People people;

	public PeopleLogger(People people) {
		this.people = people;
	}
	
	public String printLog(){
		return people.toString();
	}
}
package 设计模式.适配器模式;


//用电器做例子,笔记本电脑的插头一般都是三相的,即除了阳极、阴极外,还有一个地极。而有些地方的电源插座却只有两极,没有地极。电源插座与笔记本电脑的电源插头不匹配使得笔记本电脑无法使用。
//这时候一个三相到两相的转换器(适配器)就能解决此问题,而这正像是本模式所做的事情
public class AdapterTest {
	
	
	public static void main(String[] args) {
		 People p = new People("baoyou", "24");
		 Adapter adp = new PeopleLogger(p);
		 System.out.println(adp.printLog());
	}
	

}

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

猜你喜欢

转载自knight-black-bob.iteye.com/blog/2216733
今日推荐