博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
图解设计模式-Strategy模式
阅读量:6634 次
发布时间:2019-06-25

本文共 3534 字,大约阅读时间需要 11 分钟。

Strategy(算法)模式可以整体的替换算法的实现部分。
 
重点说明:
使用委托这种弱关联关系可以很方便的整体替换算法。
 
角色:
Strategy策略:该角色负责决定实现策略所需要的接口api。
ConcreteStrategy具体策略:该角色负责实现Strategy角色接口api。即负责实现具体的策略。
Context上下文:负责使用Strategy角色,Context角色保存了ConcreteStrategy角色的实例,并使用ConcreteStrategy角色去实现需求。
 
代码:
public class Hand {    public static final int GUU = 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
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...

转载于:https://www.cnblogs.com/use-D/p/9601980.html

你可能感兴趣的文章
小项目中建立列表页时间需要注意的
查看>>
SQL Server 索引列的顺序——真的没关系吗
查看>>
Eclipse中Svn插件配置
查看>>
POI 设置EXCEL单元格格式(日期数字文本等)
查看>>
cocos2d-x lua 学习笔记(1) -- 环境搭建
查看>>
java.lang 类String
查看>>
去掉iPhone、iPad的默认按钮样式
查看>>
U盘去保护方法
查看>>
kmalloc/kfree,vmalloc/vfree函数用法和区别
查看>>
【转】 Android开发之EditText属性详解
查看>>
tomcat https 未测试成功的版本
查看>>
各大门户网站全局CSS样式定义
查看>>
Overfitting & Regularization
查看>>
Bootstrap看厌了?试试Metro UI CSS吧
查看>>
通过Java发送邮件和接收邮件的工具类
查看>>
理解SQL Server中的权限体系(上)----主体
查看>>
Android Map新用法:MapFragment应用
查看>>
强人工智能基本问题:全局控制与自组织
查看>>
音乐网站之技术总结
查看>>
[.net 面向对象编程基础] (12) 面向对象三大特性——继承
查看>>