一个实例用全创建型模式

目录:《一个实例讲完23种设计模式》

当前:单件+抽象工厂+创建者+工厂方法

需求:坦克大战

创建两种坦克

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

设计说明

1.抽象工厂承担了创建不减的任务

2.创建者承担了讲部件组装的任务

3.工厂方法类相当于创建者模式的导演,但是他是并未给用户提供选择创建者的接口。

而是通过自己的多态来实现加载不同的创建者。

类图

一个实例用全创建型模式_第1张图片

代码

class Function{
	public String mOperation;
	public void exe(int type) {
		System.out.println(mOperation+type);
	}
};
class ShotFun extends Function{
	static ShotFun mFunction = new ShotFun();
	static ShotFun get() {
		return mFunction;
	}
	public ShotFun() {
		mOperation = "射击:";
	}
}
class RunFun extends Function{
	static RunFun mFunction = new RunFun();
	static RunFun get() {
		return mFunction;
	}
	public RunFun() {
		mOperation = "跑:";
	}
}
// interface ----------------
interface ITank{
	void shot();
	void run();
	void setmShot(IOperation mShot);
	void setmRun(IOperation mRun);
}
interface IOperation{
	void exe();
}
interface IAbstractFactory{
	IOperation createShot();
	IOperation createRun();
}
interface IBuilder{
	void createShout(ITank t);
	void createRun(ITank t);
}
interface IFactory{
	ITank createTank();
}
//
class Shot70 implements IOperation{
	public void exe() {
		ShotFun.get().exe(70);
	}
}
class Run70 implements IOperation{
	public void exe() {
		RunFun.get().exe(70);
	}
}
class Shot50 implements IOperation{
	public void exe() {
		ShotFun.get().exe(50);
	}
}
class Run50 implements IOperation{
	public void exe() {
		RunFun.get().exe(50);
	}
}
class Tank implements ITank{
	IOperation mShot;
	IOperation mRun;
	int mSubType;
	public void setmSubType(int mSubType) {
		this.mSubType = mSubType;
	}
	public void setmShot(IOperation mShot) {
		this.mShot = mShot;
	}
	public void setmRun(IOperation mRun) {
		this.mRun = mRun;
	}
	public void shot() {
		mShot.exe();
	}
	public void run() {
		mRun.exe();
	}
}
class AbstractFactory70 implements IAbstractFactory{
	public IOperation createShot() {
		return new Shot70();
	}
	public IOperation createRun() {
		return new Run70();
	}
}
class AbstractFactory50 implements IAbstractFactory{
	public IOperation createShot() {
		return new Shot50();
	}
	public IOperation createRun() {
		return new Run50();
	}
}
abstract class Builder implements IBuilder{
	IAbstractFactory mIAbstractFactory;
	public void createShout(ITank t) {
		t.setmShot(mIAbstractFactory.createShot());
	}
	public void createRun(ITank t) {
		t.setmRun(mIAbstractFactory.createRun());
	}
}
class Builder70 extends Builder{
	public Builder70() {
		mIAbstractFactory = new AbstractFactory70();
	}
}
class Builder50 extends Builder{
	public Builder50() {
		mIAbstractFactory = new AbstractFactory50();
	}
}
abstract class Factory implements IFactory{
	IBuilder mBuilder;
	public ITank createTank() {
		ITank t = new Tank();
		mBuilder.createRun(t);
		mBuilder.createShout(t);
		return t;
	}
}
class Factory70 extends Factory{
	public Factory70() {
		mBuilder = new Builder70();
	}	
}
class Factory50 extends Factory{
	public Factory50() {
		mBuilder = new Builder50();
	}
}

public class Client {
	public static void main(String[] args) {
		System.out.println("hello worldff !");
		Factory70 f7 = new Factory70();
		ITank t = f7.createTank();
		t.shot();
		t.run();
	}
}

运行结果

下一篇《一个实例用全创建型模式-优化(冗余消除)》

 

你可能感兴趣的:(设计模式-坦克大战-java,创建型模式,单件,工厂方法,抽象工厂,创建者)