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

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

当前:策略模式 职责链 观察者

上一篇 《命令模式坦克大战简单实现(java实现)

需求:坦克大战

创建两种坦克

坦克类型射程速度
b7070米时/70公里
b5050米时/70公里

简单说明一下

这任然用坦克大战的需求,实现了如下3种模式

  1. 策略
  2. 职责链
  3. 观察者(这里的观察者就一个,没有体现被观察者和观察者1对多的关系,但是观察者的价值在于被观察者发现自己有变化的时候,对观察者自动的通知,至于多少个观察者个人认为不是重点)
  4. 其实也用到了命令模式(因为将机能都封装成了对象,但我再类图中没画)
  5. 这篇文章的主要意图:策略+职责链的运用
  6. 我尽量都用坦克大战实现所有的模式就是希望对需求的理解别影响对模式的学习。(理解需求消耗的时间尽可能的少)

类图

代码

js实现

c++实现

jav实现

class Function{
	public String mStr;
	Function(String str){
		mStr = str;
		exe();
	}
	public void exe() {
		System.out.println(mStr);
	}
};
// Strategy Pattern
interface IShot{
	void exe();
}
interface IRun{
	void exe();
}

class Tank{
	IShot mShot;
	IRun mRun;
	public void exe() {
		mShot.exe();
		mRun.exe();
	}
}

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

//Concrete
class B50Shot implements IShot{
	public void exe() {
		Function f = new Function("发射距离50米");
	}
}
class B70Shot implements IShot{
	public void exe() {
		Function f = new Function("发射距离70米");
	}
}
class B50Run implements IRun{
	public void exe() {
		Function f = new Function("速度50公里");
	}
}
class B70Run implements IRun{
	public void exe() {
		Function f = new Function("速度70公里");
	}
}

class HandlerRunB70 implements IHandler{
	IStrategy mStrategy;
	public HandlerRunB70(IStrategy stragegy) {
		mStrategy = stragegy;
	}
	public void create(Tank t) {
		t.mRun = new B70Run();
		mStrategy.update(t);
	}
}
class HandlerSortB70 implements IHandler{
	HandlerRunB70 mNextHandler;
	public HandlerSortB70(HandlerRunB70 h){
		mNextHandler = h;
	}
	public void create(Tank t) {
		t.mShot = new B70Shot();
		mNextHandler.create(t);
	}
}

class HandlerRunB50 implements IHandler{
	IStrategy mStrategy;
	public HandlerRunB50(IStrategy stragegy) {
		mStrategy = stragegy;
	}
	public void create(Tank t) {
		t.mRun = new B70Run();
		mStrategy.update(t);
	}
}
class HandlerSortB50 implements IHandler{
	HandlerRunB50 mNextHandler;
	public HandlerSortB50(HandlerRunB50 h){
		mNextHandler = h;
	}
	public void create(Tank t) {
		t.mShot = new B70Shot();
		mNextHandler.create(t);
	}
}

class B70Strategy implements IStrategy{
	HandlerSortB70 mStartChain;
	Client mClient;
	public B70Strategy(Client c) {
		HandlerRunB70 endChain  = new HandlerRunB70(this);
		mStartChain = new HandlerSortB70(endChain);
		mClient = c;
	}
	public void create() {
		Tank t = new Tank();
		mStartChain.create(t);
	} 
	public void update(Tank t) {
		mClient.ceateTankFinish(t);
	}
}

class B50Strategy implements IStrategy{
	HandlerSortB50 mStartChain;
	Client mClient;
	public B50Strategy(Client c) {
		HandlerRunB50 endChain  = new HandlerRunB50(this);
		mStartChain = new HandlerSortB50(endChain);
		mClient = c;
	}
	public void create() {
		Tank t = new Tank();
		mStartChain.create(t);
	} 
	public void update(Tank t) {
		mClient.ceateTankFinish(t);
	}
}

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

运行结果

后记:

其实细心的读者能发现,这里对坦克型号和机能的结合方式有点混乱,这里是明显需要用桥接的方式来处理的。

我稍后会在做些优化,这里主要体现“策略+职责链”的运用。

设计模式这东西,本质还是面向对象的特性。

无论用什么样的需求解释设计模式,最本质的东西和是面向对象的特性在模式里的体现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值