10、Strategy 策略模式 整体地替换算法 行为型模式

1

  使

2

 使

3

  

Hand

package cn.design.behavior.strategy;
/** * @author lin * @version 1.0 * @date 2020-07-22 14:45 * @Description TODO */public class Hand { // public static final int HANDVALUE_ROCK = 0; // public static final int HANDVALUE_SCISSORS = 1; // public static final int HANDVALUE_PEPER = 2; private static final Hand[] hand = { new Hand(HANDVALUE_ROCK), new Hand(HANDVALUE_SCISSORS), new Hand(HANDVALUE_PEPER) }; private static final String[] name = { "", "", "" }; private final int handValue;
private Hand(int handValue) { this.handValue = handValue; }
public static Hand getHand(int handValue) { return hand[handValue]; }
public boolean isStrongerThan(Hand h) { return fight(h) == 1; }
public boolean isWeakerThan(Hand h) { return fight(h) == -1; }
private int fight(Hand h) { // if (this == h) { return 0; } else if ((this.handValue + 1) % 3 == h.handValue) { return 1; } else { return -1; } }
@Override public String toString() { return name[handValue]; }}

Strategy

package cn.design.behavior.strategy;
/** * @author lin * @version 1.0 * @date 2020-07-22 14:50 * @Description TODO */public interface Strategy { public abstract Hand nextHand(); public abstract void study(boolean win);}

ProbStrategy

package cn.design.behavior.strategy;
import java.util.Random;
/** * @author lin * @version 1.0 * @date 2020-07-22 14:51 * @Description TODO */public class ProbStrategy implements Strategy{ private Random random; private int prevHandValue = 0; private int currentHandValue = 0; //history[][] //studynextHandhistory 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][1]){ handValue = 1; }else{ handValue = 2; } prevHandValue = currentHandValue; currentHandValue = handValue; return Hand.getHand(handValue); } private int getSum(int hv){ int sum = 0; for (int i : history[hv] ) { sum += i; } return sum; } @Override public void study(boolean win){ if(win){ history[prevHandValue][currentHandValue]++; }else{ history[prevHandValue][(currentHandValue+1)%3]++; history[prevHandValue][(currentHandValue+2)%3]++; } }}

WinningStrategy

package cn.design.behavior.strategy;
import java.util.Random;
/** * @author lin * @version 1.0 * @date 2020-07-22 14:51 * @Description TODO */public class WinningStrategy implements Strategy { private Random random; private boolean won = false; // private Hand prevHand; //
public WinningStrategy(int seed) { random = new Random(seed); }
@Override public Hand nextHand() { if (!won) { prevHand = Hand.getHand(random.nextInt(3)); } return prevHand; }
@Override public void study(boolean win) { won = win; }}

Player

package cn.design.behavior.strategy;
/** * @author lin * @version 1.0 * @date 2020-07-22 14:52 * @Description TODO */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++; } @Override public String toString(){ return "[" + name + ":" + gamecount + " games, " + wincount + " win, " + losecount + " lose" + "]"; }}

main

package cn.design.behavior.strategy;
import java.util.Random;
/** * @author lin * @version 1.0 * @date 2020-07-22 14:52 * @Description TODO */public class Main { public static void main(String[] args) { int seed1 = ((new Random()).nextInt(500)) * (1 + (new Random()).nextInt(500)); int seed2 = seed1 * (new Random()).nextInt(500);
Player player1 = new Player("Taro", new WinningStrategy(seed1)); Player player2 = new Player("Hana", new ProbStrategy(seed2)); for (int i = 0; i < 100000; i++) { Hand nextHand1 = player1.nextHand(); Hand nextHand2 = player2.nextHand(); if (nextHand1.isStrongerThan(nextHand2)) { System.out.println("Winner: " + player1); player1.win(); player2.lose(); } else if (nextHand2.isStrongerThan(nextHand1)) { System.out.println("Winner: " + player2); player2.win(); player1.lose(); } else { System.out.println("Even..."); player1.even(); player2.even(); } } System.out.println("Total result:"); System.out.println(player1.toString()); System.out.println(player2.toString()); }}


Even...Winner: [Taro:99994 games, 37658 win, 27060 lose]Even...Even...Winner: [Taro:99997 games, 37659 win, 27060 lose]Winner: [Hana:99998 games, 27060 win, 37660 lose]Even...Total result:[Taro:100000 games, 37660 win, 27061 lose][Hana:100000 games, 27061 win, 37660 lose]

4Strategy

 

Strategy

ConcreteStrategyStrategyWinningStrategyProbStrategy

Context使Strategyplayer

5

Flyweight使ConcreteStrategy

Abstract Factory

Statestrategy

 

公众号:发哥讲

这是一个稍偏基础和技术的公众号,甚至其中包括一些可能阅读量很低的包含代码的技术文,不知道你是不是喜欢,期待你的关注。

● 扫码关注我们

据说看到好文章不推荐的人,服务器容易宕机!

本文版权归发哥讲博客园共有,原创文章,未经允许不得转载,否则保留追究法律责任的权利。

posted @ 2020-08-06 14:56  发哥讲Java  阅读(244)  评论(0编辑  收藏  举报