策略模式可以更换实现算法的部分而且不留痕迹,切换整个算法,简化改为采用其他方法来解决同样的问题,在设计程序时,比较习惯把实现算法这部分结合到方法里面,但是策略模式则故意把算法的部分跟其他部分分开,只规定跟算法有关的接口部分,然后再从程序这边以委托的方式来利用运算法则,因为有了委托关系,所以才能切换算法,尤其是机动性的切换动作
package com.tools.pattern.strategy;
public class Hand {
// 石头的值
private static final int HANDVALUE_GUU = 0;
// 剪刀的值
private static final int HANDVALUE_CHO = 1;
// 布的值
private static final int HANDVALUE_PAA = 2;
private static final Hand[] hands = { new Hand(HANDVALUE_GUU),
new Hand(HANDVALUE_CHO), new Hand(HANDVALUE_PAA) };
private static final String[] names = { "石头", "剪刀", "布" };
private int handValue;
private Hand(int handValue) {
this.handValue = handValue;
}
public static Hand getHand(int handValue) {
return hands[handValue];
}
public boolean isStrongerThan(Hand h) {
return 1 == this.fight(h);
}
public boolean isWeakerThan(Hand h) {
return -1 == this.fight(h);
}
private int fight(Hand h) {
if (this == h) {
return 0;
} else if ((this.handValue + 1) % 3 == h.handValue) {
return 1;
} else {
return -1;
}
}
public String toString() {
return names[this.handValue];
}
}
package com.tools.pattern.strategy;
public interface Strategy {
Hand nexHand();
void study(boolean isWin);
}
package com.tools.pattern.strategy;
import java.util.Random;
public class WinningStrategy implements Strategy {
private Random random;
private Hand preHand;
private boolean isWon = false;
public WinningStrategy(int seed) {
random = new Random(seed);
}
public Hand nexHand() {
if (!this.isWon) {
preHand = Hand.getHand(random.nextInt(3));
}
return preHand;
}
public void study(boolean isWin) {
this.isWon = isWin;
}
}
package com.tools.pattern.strategy;
import java.util.Random;
public class ProbStrategy implements Strategy {
private Random random;
private int prevHandValue = 0;
private int currentHandValue = 0;
private int[][] history = { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } };
public ProbStrategy(int seed) {
random = new Random(seed);
}
private int getSum(int hv) {
int sum = 0;
for (int i = 0; i < 3; i++) {
sum += history[hv][i];
}
return sum;
}
public Hand nexHand() {
int bet = random.nextInt(this.getSum(this.currentHandValue));
int handValue = 0;
if (bet < history[this.currentHandValue][0]) {
handValue = 0;
} else if (bet < history[this.currentHandValue][0]
+ history[this.currentHandValue][1]) {
handValue = 1;
} else {
handValue = 2;
}
this.prevHandValue = this.currentHandValue;
this.currentHandValue = handValue;
return Hand.getHand(handValue);
}
public void study(boolean isWin) {
if (isWin) {
history[this.prevHandValue][this.currentHandValue]++;
} else {
history[this.prevHandValue][(this.currentHandValue + 1) % 3]++;
history[this.prevHandValue][(this.currentHandValue + 2) % 3]++;
}
}
}
分享到:
相关推荐
策略模式结合模板方法模式
策略模式的 C++ 代码实现, ide :XCode
策略模式定义了方法族,分别封装起来,让它们之间可以相互替换,此模式让算法的变化独立于使用算法的客户。 还可以那家咖啡屋,记忆里很温馨,很sugar,或许很多温馨美好的回忆都是于咖啡有关的。 我们常常说设计...
所有模式都可分为类模式和对象模式两种,类模式是继承,对象模式是委托,而桥接模式和策略模式都是将任务委托给另外一个接口去实现,那么两者的区别什么呢?
主要介绍了详解SpringBoot结合策略模式实战套路,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
技术: 1. spring事件驱动(ApplicationEventPublisher) 2. 策略模式处理事件 目的: 1. 通过event,代码逻辑异步处理 2. 通过策略模式,构建具体监听实现 3. 解耦 4. 容错(降低代码块错误风险)
这是策略模式中的一个经典实例,通过鸭子问题,能让学习者更好的了解设计模式,这也是headfirst 设计模式中用的经典实例
深入浅出设计模式之抽象工厂模式+工厂方法模式+策略模式实现手机加工厂(加类图)
策略模式实例策略模式实例策略模式实例策略模式实例策略模式实例
策略模式代码,两种方式实现,第一种,通过map存储方式,第二种,通过配置文件加反射方式
策略模式的简单例子,根据《Head First设计模式》中第一章中的Duck编写
策略模式例子,纯代码,copy后即可运行; 1、如果在一个系统里面有许多类,它们之间的区别仅在于它们的行为,那么使用策略模式可以动态地让一个对象在许多行为中选择一种行为。 2、一个系统需要动态地在几种算法中...
策略模式在实际项目中的应用二,该测试demo为普通项目,导入build path一下lib目录下的jar包,然后运行测试类即可在控制台看到测试结果
处理层的设计采用了设计模式中的策略模式、模板方法模式和工厂模式。 Server端和Client端的实现也采用了分层的设计方式,包含自定义的模型层、视图层和控制层。 说明:程序采用配置文件的方式进行初始化,运行时时请...
55-Java设计模式之策略模式与状态模式1
设计模式中的策略模式,同时兼容简单工厂模式,商场收银模式,易懂可用。
策略模式策略模式策略模式
javascript的策略模式和代理模式简介,附代码
在网络上看到几个加密解密算法,本着"取之于民用之于民"的方针测试一下,并用策略模式将这些算法封装共享。
设计模式中的策略模式,template模式详细讲解。