1、模式说明java
策略模式比较好理解,就是将程序中用到的算法总体的拿出来,并有多个不一样版本的算法实现,在程序运行阶段,动态的决定使用哪一个算法来解决问题。算法
举个实际的例子:排序算法的问题,假如咱们的程序中须要对数据进行排序,咱们知道,不一样的算法具备不一样的时间复杂度和空间复杂度,所以须要在程序运行时,根据可用内存和数据特征,选用不一样的算法(排序策略),这就是策略模式的使用场景之一。再举个例子,负载均衡算法:若是某个服务部署了多个冗余的实例,客户端在向服务端发送请求时,根据负载均衡算法策略,请求可能会被转发到不一样的服务提供者实例来处理,如何决定某个请求转发给哪一个服务实例呢,最简单的作法就是轮询,顺次将请求转发给每一个服务实例进行处理。也能够采用随机方式,或者根据实际硬件环境和业务场景设置特定算法。设计模式
2、模式类图负载均衡
3、程序示例dom
在下面的演示策略模式的代码示例中,咱们模拟猜拳游戏——剪刀石头布,猜拳的策略有两种:若是此次猜拳赢了,则下次还出一样的手势。另外一种策略就是根据之前的猜拳结果,选择胜率最高的一种手势。ide
一、Hand类:表示猜拳游戏中的手势,并不是Strategy策略模式中的角色。this
package com.designpattern.cn.strategypattern; 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 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; } } public String toString(){ return name[handValue]; } }
二、Strategy接口:spa
package com.designpattern.cn.strategypattern; public interface Strategy { public abstract Hand nextHand(); public abstract void study(boolean win); }
三、WinningStrategy类:设计
package com.designpattern.cn.strategypattern; 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类:3d
package com.designpattern.cn.strategypattern; import java.util.Random; public class ProbStrategy implements Strategy { private Random random; private int prevHandValue = 0; private int currentHandValue = 0; //history[上一局的手势][这一局的手势] 表达式的值越高表示过去的胜率越高 //study方法会根据nextHand方法返回的手势胜负结果更新history字段中的值 private int[][] history = { {1, 1, 1}, {1, 1, 1}, {1, 1, 1} }; public ProbStrategy(int seed) { random = new Random(seed); } 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; } public void study(boolean win){ if(win){ history[prevHandValue][currentHandValue]++; }else{ history[prevHandValue][(currentHandValue+1)%3]++; history[prevHandValue][(currentHandValue+2)%3]++; } } }
五、Player类:
package com.designpattern.cn.strategypattern; 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类与运行结果:
package com.designpattern.cn.strategypattern; import java.util.Random; 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()); } }
4、Strategy策略模式中的角色
5、相关的设计模式