图解设计模式--Strategy(策略)模式
2017-08-21 本文已影响33人
Ginger12
整体地替换算法
Strategy 模式
整体替换算法的实现部分
示例程序
名字 | 说明 | 角色 |
---|---|---|
Hand | 表示猜拳游戏中的“手势”的类 | |
Strategy | 表示猜拳游戏中的策略的类 | Strategy |
WinningStrategy | 表示“如果这局猜拳获胜,那么下一局也出一样的手势”这一策略的类 | ConcreteStrategy |
ProbStrategy | 表示“根据上一局的手势从概率上计算出下一句的手势从之前的猜拳结果计算下一局出各种拳的概率”这一策略的类 | ConcreteStrategy |
Player | 表示进行猜拳游戏的选手的类 | Context |
Main | 测试程序行为的类 |
Hand.java
package strategy;
public class Hand {
public static final int HANDVALUE_GUU = 0; // 表示石头的值
public static final int HANDVALUE_CHO = 1; // 表示剪刀的值
public static final int HANDVALUE_PAA = 2; // 表示布的值
public static final Hand[] hand = { // 表示猜拳中3种手势的实例
new Hand(HANDVALUE_GUU),
new Hand(HANDVALUE_CHO),
new Hand(HANDVALUE_PAA)
};
private static final String[] name = { // 表示猜拳中手势所对应的字符串
"石头", "剪刀", "布"
};
private int handvalue; // 猜拳中出的手势的值
private Hand(int handvalue) {
this.handvalue = handvalue;
}
public static Hand getHand(int handvalue) { // 根据手势的值获取其对应的实例
return hand[handvalue];
}
public boolean isStrongerThan(Hand h) { // 如果 this 胜了 h 则返回 true
return fight(h) == 1;
}
public boolean isWeakerThan(Hand h) { // 如果 this 输给了 h 则返回 true
return fight(h) == -1;
}
private int fight(Hand h) { // 计分:平0,胜1,负-1
if (this == h) {
return 0;
} else if ((this.handvalue + 1) % 3 == h.handvalue) {
return 1;
} else {
return -1;
}
}
public String toString() { // 转换为手势值所对应的字符串
return name[handvalue];
}
}
Strategy.java(Strategy)
package strategy;
public interface Strategy {
public abstract Hand nextHand();
public abstract void study(boolean win);
}
WinningStrategy.java(ConcreteStrategy)
package strategy;
import java.util.Random;
public class WinningStrategy implements Strategy {
private Random random;
private boolean won = false;
private Hand prevHand;
public WinningStrategy(int seed) {
random = new Random(seed);
}
public Hand nextHand() {
if (!won) {
prevHand = Hand.getHand(random.nextInt(3));
}
return prevHand;
}
public void study(boolean win) {
won = win;
}
}
ProbStrategy.java(ConcreteStrategy)
package 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);
}
@Override
public Hand nextHand() {
int bet = random.nextInt(getSum(currentHandValue));
int handvalue = 0;
if (bet < history[currentHandValue][0]) {
handvalue = 0;
} else if (bet < history[currentHandValue][0] + history[currentHandValue][1]) {
handvalue = 1;
} else {
handvalue = 2;
}
prevHandValue = currentHandValue;
currentHandValue = handvalue;
return Hand.getHand(handvalue);
}
@Override
public void study(boolean win) {
if (win) {
history[prevHandValue][currentHandValue] ++;
} else {
history[prevHandValue][(currentHandValue + 1) % 3]++;
history[prevHandValue][(currentHandValue + 2) % 3]++;
}
}
private int getSum(int hv) {
int sum = 0;
for (int i = 0; i < 3; i++) {
sum += history[hv][i];
}
return sum;
}
}
Player.java(Context)
package strategy;
public class Player {
private String name;
private Strategy strategy;
private int wincount;
private int losecount;
private int gamecount;
public Player(String name, Strategy strategy) {
this.name = name;
this.strategy = strategy;
}
public Hand nextHand() {
return strategy.nextHand();
}
public void win() {
strategy.study(true);
wincount++;
gamecount++;
}
public void lose() {
strategy.study(false);
losecount++;
gamecount++;
}
public void even() {
gamecount++;
}
public String toString() {
return "[" + name + ":" + gamecount + " games, " + wincount + " win, " +
losecount + " lose]";
}
}
Main.java
package strategy;
public class Main {
public static void main(String[] args) {
int seed1 = 0;
int seed2 = 1;
Player player1 = new Player("Taro", new WinningStrategy(seed1));
Player player2 = new Player("Hana", new ProbStrategy(seed2));
for (int i = 0; i < 10000; i++) {
Hand nextHand1 = player1.nextHand();
Hand nextHand2 = player2.nextHand();
if (nextHand1.isStrongerThan(nextHand2)) {
System.out.println("Winner:" + player1);
player1.win();
player2.lose();
} else if (nextHand1.isWeakerThan(nextHand2)) {
System.out.println("Winner:" + player2);
player1.lose();
player2.win();
} else {
System.out.println("Even...");
player1.even();
player2.even();
}
}
System.out.println("Total result:");
System.out.println(player1.toString());
System.out.println(player2.toString());
}
}
Strategy 模式中的角色
-
Strategy(策略)
Strategy 角色负责决定实现策略所必须的接口。
-
ConcreteStrategy(具体的策略)
ConcreteStrategy 角色负责实现 Strategy 角色的接口,即负责实现具体的策略。
-
Context(上下文)
负责使用 Strategy 角色。Context 角色保存了 ConcreteStrategy 角色的实例。