设计模式(十)-策略模式(Strategy Pattern)——整体的替换算法

策略模式(Strategy Pattern)可以整体的替换算法的实现部分。

Hand.java

package com.test.dp.Strategy.Sample;

//算法需要计算的对象,这里代表“手势”
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

package com.test.dp.Strategy.Sample;

//策略抽象类
public interface Strategy {
    public abstract Hand nextHand();
    public abstract void study(boolean win);
}
WinningStrategy.java

package com.test.dp.Strategy.Sample;

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

package com.test.dp.Strategy.Sample;

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);
    }
    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);
    }
    private int getSum(int hv) {
        int sum = 0;
        for (int i = 0; i < 3; i++) {
            sum += history[hv][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.java

package com.test.dp.Strategy.Sample;

//使用策略的对象,这里表示“猜拳玩家”
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 com.test.dp.Strategy.Sample;

public class Main {
    public static void main(String[] args) {
    	args = new String[]{"5", "6"};
        if (args.length != 2) {
            System.out.println("Usage: java Main randomseed1 randomseed2");
            System.out.println("Example: java Main 314 15");
            System.exit(0);
        }
        int seed1 = Integer.parseInt(args[0]);
        int seed2 = Integer.parseInt(args[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 (nextHand2.isStrongerThan(nextHand1)) {
                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());
    }
}
输出结果:
Winner:[Hana:7679 games, 2645 win, 2432 lose]
Winner:[Hana:7680 games, 2646 win, 2432 lose]
Even...
Even...
Winner:[Taro:7683 games, 2432 win, 2647 lose]
Even...
Winner:[Taro:7685 games, 2433 win, 2647 lose]
Even...
Winner:[Taro:7687 games, 2434 win, 2647 lose]
Even...
Winner:[Taro:7689 games, 2435 win, 2647 lose]
……此处省略若干行
Winner:[Taro:9996 games, 3178 win, 3449 lose]
Winner:[Taro:9997 games, 3179 win, 3449 lose]
Even...
Winner:[Hana:9999 games, 3449 win, 3180 lose]
Total result:
[Taro:10000 games, 3180 win, 3450 lose]
[Hana:10000 games, 3450 win, 3180 lose]
总结:

应用实例:1、诸葛亮的锦囊妙计,每一个锦囊就是一个策略。2、旅行的出游方式,选择骑自行车、坐汽车,每一种旅行方式都是一个策略。3、JAVA AWT 中的 LayoutManager。

优点:1、算法可以自由切换。2、避免使用多重条件判断。3、扩展性良好。

缺点:1、策略类会增多。2、所有策略类都需要对外暴露。

使用场景:1、如果在一个系统里面有许多类,它们之间的区别仅在于它们的行为,那么使用策略模式可以动态地让一个对象在许多行为中选择一种行为。2、一个系统需要动态地在几种算法中选择一种。3、如果一个对象有很多的行为,如果不用恰当的模式,这些行为就只好使用多重的条件选择语句来实现。

注意事项:如果一个系统的策略多于四个,就需要考虑使用混合模式,解决策略类膨胀的问题。


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页