圖解設計模式-Strategy模式
阿新 • • 發佈:2018-09-07
關聯關系 部分 執行 關聯 stat urn tint 需要 static Strategy(算法)模式可以整體的替換算法的實現部分。
重點說明:
使用委托這種弱關聯關系可以很方便的整體替換算法。
角色:
Strategy策略:該角色負責決定實現策略所需要的接口api。
ConcreteStrategy具體策略:該角色負責實現Strategy角色接口api。即負責實現具體的策略。
Context上下文:負責使用Strategy角色,Context角色保存了ConcreteStrategy角色的實例,並使用ConcreteStrategy角色去實現需求。
代碼:
public class Hand { public static final intGUU = 0; public static final int CHO = 1; public static final int PAA = 2; public static final Hand[] hand = { new Hand(GUU), new Hand(CHO), new Hand(PAA), }; private int handValue; private Hand(int handValue) { this.handValue = handValue; }private static final String[] name = { "石頭","剪刀","布" }; 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; } } }
public interface Strategy { public abstract Hand nextHand(); public abstract void study(boolean win); }
public class ProbStrategy implements Strategy { private Random random; private int preHandValue = 0; private int currentHandValue =0; private int[][] histroy = { {1,1,1}, {1,1,1}, {1,1,1} }; public ProbStrategy(int seed) { this.random = new Random(seed); } private int getSum(int hv) { int sum = 0; for(int i=0;i<3;i++) { sum +=histroy[hv][i]; } return sum; } @Override public Hand nextHand() { int bet = random.nextInt(getSum(currentHandValue)); int handvalue = 0; if(bet<histroy[currentHandValue][0]) { handvalue=0; }else if(bet<histroy[currentHandValue][0] + histroy[currentHandValue][1]) { handvalue =1; }else { handvalue = 2; } preHandValue=currentHandValue; currentHandValue = handvalue; return Hand.getHand(handvalue); } @Override public void study(boolean win) { if(win){ histroy[preHandValue][currentHandValue]++; }else { histroy[preHandValue][(currentHandValue+1)%3]++; histroy[preHandValue][(currentHandValue+2)%3]++; } } }
public class WinningStrategy implements Strategy { private Random random; private boolean won =false; private Hand preHand; public WinningStrategy(int seed) { this.random = new Random(seed); } @Override public Hand nextHand() { if(!won) { preHand = Hand.getHand(random.nextInt(3)); } return preHand; } @Override public void study(boolean win) { won = win; } }
public class Main { public static void main(String[] args){ int seed0=1; int seed1=2; Play play1 = new Play("王五",new WinningStrategy(seed0)); Play play2 = new Play("王五",new ProbStrategy(seed1)); for(int i=0;i<1000;i++) { Hand hand1 = play1.nextHand(); Hand hand2 = play2.nextHand(); if(hand1.isStrongerThan(hand2)) { System.out.println("winner is "+play1); play1.win(); play2.lose(); }else if(hand2.isStrongerThan(hand1)) { System.out.println("winner is "+play2); play2.win(); play1.lose(); }else { System.out.println("even... "); play2.even(); play1.even(); } } } }
執行結果:
winner is [王五:0 ganmes,0 win, 0 lose ]
even...
even...
winner is [王五:3 ganmes,1 win, 0 lose ]
even...
even...
even...
winner is [王五:7 ganmes,2 win, 0 lose ]
winner is [王五:8 ganmes,3 win, 0 lose ]
winner is [王五:9 ganmes,0 win, 4 lose ]
winner is [王五:10 ganmes,4 win, 1 lose ]
winner is [王五:11 ganmes,1 win, 5 lose ]
winner is [王五:12 ganmes,5 win, 2 lose ]
winner is [王五:13 ganmes,2 win, 6 lose ]
even...
圖解設計模式-Strategy模式