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

适配器模式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();
	}

}

猜你喜欢

转载自injavawetrust.iteye.com/blog/2310615