策略模式和职责链模式实现坦克大战3

需求:坦克大战

创建两种坦克

坦克类型 射程 速度
b70 70米 时/70公里
b50 50米 时/70公里

类图

策略模式和职责链模式实现坦克大战3_第1张图片

代码

import java.util.HashMap;

class Function{
	public String mStr;
	public String mUnit;
	Function(String str,String unit){
		mStr = str;
		mUnit = unit;
	}
	public void exe(int specification) {
		System.out.println(mStr+specification+mUnit);
	}
};
// Strategy Pattern
interface IFun{
	void exe();
}

interface IHandler{
	void create(Tank t);
}
interface IStrategy{
	//algorithm
	void create();
	void update(Tank t);
}
interface ISpecificationOfTank{
	int getSpecification();
}

//Concrete
abstract class ConcreteFun implements IFun{
	public ConcreteFun(ISpecificationOfTank s) {
		mSpecificationOfTank = s;
	}
	protected ISpecificationOfTank mSpecificationOfTank;
}
class ConcreteSpecification implements ISpecificationOfTank{
	int mSpecification;
	public ConcreteSpecification(int value) {
		mSpecification = value;
	}
	public int getSpecification() {
		return mSpecification;
	}
}
class ShotFlyweight extends Function{
	public ShotFlyweight() {
		super("发射距离","米");
	}
}
class RunFlyweight extends Function{
	public RunFlyweight() {
		super("速度","公里");
	}
}
class FlyweightFactory{
	static FlyweightFactory mFlyweightFactory = new FlyweightFactory();
	static FlyweightFactory get() {
		return mFlyweightFactory;
	}
	HashMap mMaps = new HashMap();
	public Function GetFlyweitht(String key) {
		Function f = mMaps.get(key);
		if(f == null) {
			return createFlyweight(key);
		}else
		{
			return f;
		}
	} 
	public Function createFlyweight(String key) {
		Function f = null;
		if(key == "shot") {
			f = new ShotFlyweight();
		}else {
			f = new RunFlyweight();
		}
		mMaps.put(key, f);
		return f;
	}
}
class Shot extends ConcreteFun{
	public Shot(ISpecificationOfTank s) {
		super(s);
	}
	public void exe() {
		Function f = FlyweightFactory.get().GetFlyweitht("shot");
		f.exe(mSpecificationOfTank.getSpecification());
	}
}
class Run extends ConcreteFun{
	public Run(ISpecificationOfTank s) {
		super(s);
	}
	public void exe() {
		Function f = FlyweightFactory.get().GetFlyweitht("run");
		f.exe(mSpecificationOfTank.getSpecification());
	}
}
class Tank{
	Shot mShot;
	Run mRun;
	public void exe() {
		mShot.exe();
		mRun.exe();
	}
}

abstract class Handler implements IHandler{
	protected ISpecificationOfTank mSpecificationOfTank;
	public Handler(ISpecificationOfTank s) {
		mSpecificationOfTank = s;
	}
}

class HandlerRun extends Handler{
	IStrategy mStrategy;
	public HandlerRun(IStrategy stragegy,ISpecificationOfTank s) {
		super(s);
		mStrategy = stragegy;
	}
	public void create(Tank t) {
		t.mRun = new Run(mSpecificationOfTank);
		mStrategy.update(t);
	}
}

class HandlerSort extends Handler{
	HandlerRun mNextHandler;
	public HandlerSort(HandlerRun h,ISpecificationOfTank s){
		super(s);
		mNextHandler = h;
	}
	public void create(Tank t) {
		t.mShot = new Shot(mSpecificationOfTank);
		mNextHandler.create(t);
	}
}

class Strategy implements IStrategy{
	HandlerSort mStartChain;
	ISpecificationOfTank mSpecificationOfTank;
	Client mClient;
	public Strategy(Client c) {
		mClient = c;
	}
	protected void myinit() {
		HandlerRun endChain  = new HandlerRun(this,mSpecificationOfTank);
		mStartChain = new HandlerSort(endChain,mSpecificationOfTank);
	}
	public void create() {
		Tank t = new Tank();
		mStartChain.create(t);
	} 
	public void update(Tank t) {
		mClient.ceateTankFinish(t);
	}
}
class B70Strategy extends Strategy{
	public B70Strategy(Client c) {
		super(c);
		mSpecificationOfTank = new ConcreteSpecification(70);
		myinit();
	}
}

class B50Strategy extends Strategy{
	public B50Strategy(Client c) {
		super(c);
		mSpecificationOfTank = new ConcreteSpecification(70);
		myinit();
	}
}

public class Client {
	public static void main(String[] args) {
		System.out.println("hello world !");
		Client c = new Client();
		IStrategy strategy = new B70Strategy(c);
		strategy.create();
	}
	public void ceateTankFinish(Tank t) {
		System.out.println("ceateTankFinish");
		t.exe();
	}
}

运行结果

你可能感兴趣的:(设计模式-坦克大战-java)