啦啦啦,又懒惰了几天,希望毕业前可以整理完所有设计模式。
策略模式,使用比较官方的话来介绍:定义一系列算法类,将每一个算法封装起来,并让它们可以相互替换,策略模式让算法独立于使用它的客户而变化,也称为政策模式(Policy)。策略模式是一种对象行为型模式。
大白话理解一下就是策略模式是为了在业务场景中整体替换算法的实现部分,以处理不同的实际情况,使用起来也比较简单,就是在上下文中保留一个抽象的策略的引用,在根据实际情况的改变,去定义新的具体策略类。还是直接看代码(书上的例子比较复杂,我自己写的比较简单,不影响理解)
这段示例程序是让计算机进行猜拳游戏,定义两种出拳策略:
- 如果本局获胜,下局出一样的。失败则随机出一个
- 无论本局获胜与否,下局随机出一个。
首先是手势工具类HandUtil用来判断输赢和生成出拳状态,我们约定1-石头,2-剪刀,3-布
import java.util.Random;
public class HandUtil {
public static final int EVEN = 0;
public static final int LEFT_WIN = 1;
public static final int RIGHT_WIN = 2;
public static Random random;
/**
* 1-石头 2-剪刀 3-布
*/
//判断规则
public static int compareHandValue(int handValue1, int handValue2) {
if(handValue1 == handValue2) {
return EVEN;
}
if((handValue1 + 1) % 3 == handValue2) {
return LEFT_WIN;
}
return RIGHT_WIN;
}
//生成随机出拳状态
public static int randomHandValue() {
random = new Random();
return random.nextInt(3) + 1;
}
}
然后是策略的抽象接口,有两个方法需要具体策略实现:1.根据输赢结果进行学习;2.出拳
public interface Strategy {
public abstract int nextHand();
public abstract void study(boolean isWin);
}
接着是两个具体策略类的实现:
- 如果本局获胜,下局出一样的。失败则随机出一个
public class WinningStrategy implements Strategy {
private boolean isWin;
private int preHandValue;
@Override
public int nextHand() {
if(!this.isWin) {
this.preHandValue = HandUtil.randomHandValue();
}
return preHandValue;
}
@Override
public void study(boolean isWin) {
this.isWin = isWin;
}
}
- 无论本局获胜与否,下局随机出一个。
public class RandomStrategy implements Strategy{
@Override
public int nextHand() {
return HandUtil.randomHandValue();
}
//对于无论输赢都随机出拳的策略,其实该方法没有什么用处
@Override
public void study(boolean isWin) {
}
}
然后就是玩家类Player:
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;
this.winCount = 0;
this.loseCount = 0;
this.gameCount = 0;
}
//玩家的出拳行为,其实就是根据策略来出拳
public int nextHand(){
return this.strategy.nextHand();
}
//获胜后的学习行为
public void win() {
this.strategy.study(true);
++this.gameCount;
++this.winCount;
}
//失败后的学习行为
public void lose() {
this.strategy.study(false);
++this.gameCount;
++this.loseCount;
}
//平局
public void even() {
++this.gameCount;
}
public String toString() {
return name + ": 总局数 " + this.gameCount + " ; 获胜局数 " + this.winCount
+ " ; 失败局数 " + this.loseCount;
}
}
玩家类保留策略的引用,出拳,输赢后的行为都和策略类有紧密的关系,还有一个对玩家游戏记录的输出
直接通过Main测试一下:
public class Main {
public static void main(String[] args) {
Player leftPlayer = new Player("left", new WinningStrategy());
Player rightPlayer = new Player("right", new RandomStrategy());
int leftHandValue, rightHandValue, compareResult;
for(int i=0; i<1000; ++i) {
leftHandValue = leftPlayer.nextHand();
rightHandValue = rightPlayer.nextHand();
compareResult = HandUtil.compareHandValue(leftHandValue, rightHandValue);
if(compareResult == 0) {
System.out.println("even--------------------");
leftPlayer.even();
rightPlayer.even();
} else if(compareResult == 1) {
System.out.println("leftPlayer win----------");
leftPlayer.win();
rightPlayer.lose();
} else {
System.out.println("rightPlayer win---------");
leftPlayer.lose();
rightPlayer.win();
}
}
System.out.println("game over! \n Total result:");
System.out.println(leftPlayer.toString());
System.out.println(rightPlayer.toString());
}
}
定义两个玩家,进行1000次对战,最后打印对战结果:
rightPlayer win---------
even--------------------
even--------------------
rightPlayer win---------
even--------------------
rightPlayer win---------
......
......
leftPlayer win----------
even--------------------
game over!
Total result:
left: 总局数 1000 ; 获胜局数 242 ; 失败局数 401
right: 总局数 1000 ; 获胜局数 401 ; 失败局数 242
多运行了几次,虽然每次结果稍有不同,但都是rightPlayer获胜的局数比较多。
好,简单来说就这样,周六了,打球打球