大话设计模式_适配器模式

以姚明在NBA需要翻译为例子。

package com.wzs.design;

/**
 * 大话设计模式--page174 适配器模式
 * 
 * @author Administrator
 * 
 */
public class AdapterPattern {
	public static void main(String[] args) {
		Player player1 = new Forwards("巴蒂尔");
		player1.attack();

		Player player2 = new Center("超人");
		player2.attack();

		Player player3 = new Guards("麦克格雷迪");
		player3.attack();

		Player player4 = new Translator("姚明");
		player4.attack();

	}
}

/*
 * 球员
 */
class Player {
	public String name;

	public Player(String name) {
		this.name = name;
	}

	// 攻击
	public void attack() {

	}

	// 防守
	public void defense() {

	}
}

// 前锋
class Forwards extends Player {
	public Forwards(String name) {
		super(name);
	}

	@Override
	public void attack() {
		System.out.println("前锋 " + this.name + " 进攻.");
	}

	@Override
	public void defense() {
		System.out.println("前锋 " + this.name + " 防守.");
	}

}

// 中锋
class Center extends Player {
	public Center(String name) {
		super(name);
	}

	@Override
	public void attack() {
		System.out.println("中锋 " + this.name + " 进攻.");
	}

	@Override
	public void defense() {
		System.out.println("中锋 " + this.name + " 防守.");
	}

}

// 后卫
class Guards extends Player {
	public Guards(String name) {
		super(name);
	}

	@Override
	public void attack() {
		System.out.println("后卫 " + this.name + " 进攻.");
	}

	@Override
	public void defense() {
		System.out.println("后卫 " + this.name + " 防守.");
	}

}

// 外籍中锋
class ForeignCenter {
	public String name;

	public ForeignCenter(String name) {
		this.name = name;
	}

	// 攻击
	public void attack() {
		System.out.println("外籍中锋 " + this.name + " 进攻.");
	}

	// 防守
	public void defense() {
		System.out.println("外籍中锋 " + this.name + " 防守.");
	}
}

// 翻译者
class Translator extends Player {
	private ForeignCenter foreignCenter = new ForeignCenter(this.name);

	public Translator(String name) {
		super(name);
	}

	@Override
	public void attack() {
		foreignCenter.attack();
	}

	@Override
	public void defense() {
		foreignCenter.defense();
	}

}


你可能感兴趣的:(适配器模式,大话设计模式,姚明)