首页 > 其他 > 详细

适配器模式Adapter(对象的适配器模式)

时间:2016-07-13 02:10:12      阅读:180      评论:0      收藏:0      [点我收藏+]

适配器模式Adapter(对象的适配器模式)

?

?

/**
 * 目标角色
 * @author InJavaWeTrust
 *
 */
public interface Japanese {
	
	public void speakJapanese();

}

?

/**
 * 源角色
 * @author InJavaWeTrust
 *
 */
public interface Chinese {
	
	public void speakChinese();

}

?

/**
 * 适配器角色
 * @author InJavaWeTrust
 *
 */
public class Translate implements Japanese{
	
	private Chinese chinese;
	
	public Translate(Chinese chinese) {
		this.chinese = chinese;
	}

	@Override
	public void speakJapanese() {
		chinese.speakChinese();
		System.out.println("translate.");
		System.out.println("speak Japanese.");
	}

}

?

/**
 * 测试类
 * @author InJavaWeTrust
 *
 */
public class TestAdapter {
	
	public static void main(String[] args) {
		Chinese chinese = new Chinese() {
			public void speakChinese() {
				System.out.println("speak Chinese.");
			}
		};
		
		Translate translate = new Translate(chinese);
		translate.speakJapanese();
	}

}

?

适配器模式Adapter(对象的适配器模式)

原文:http://injavawetrust.iteye.com/blog/2310615

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!