Java设计模式简述

设计模式就是解决一些类似问题的方法或者手段。这里所讲解设计模式都是为了所教学员的学习,所以讲解并不一定专业,旨在用通俗的语言来表述概念。如果学员能理解设计模式其中的道理就达到了我的初衷。

本人认为学习设计模式不但要理解其中的道理,更重要的在于在自己实际开发中的应用,希望学员能自己通过摸索和锻炼来达到实际应用的目的。好了,现在开始:

策略就是解决一些问题的方法,作战有策略,做事有策略。对于我们编写程序而言其实就是所采用的算法。比如排序,我们可以采用冒泡、插入、二分查找、基数,希尔、快速等多种方式,这些方式其实就是我们所采用的策略。但我们完成一个功能的时候采用一种算法(策略),有可能以后改成另外一种策略。当我们从一个策略切换到另外一个策略的时候,只需要关系策略的改变,对于使用策略的地方并不需要关心,这就是策略模式。说得通俗的点,就是把我们所采用的算法和使用算法的地方分离,这样可以在不留任何痕迹的情况下能很容易的做到算法的切换。

我们可以举一些实用些的例子来体会策略的用法。但是在此我采用我曾经看过的一本书上的例子来讲解,以为内我感觉这个例子虽然实用性并不是很强,但是对于策略模式的理解要比一些实用的例子容易一些。当我们真正理解了策略模式,自己通过摸索在自己的应用中进行采用。

这个例子就是模拟我们小时候经常玩得一个游戏“石头、剪子、布”。我们通过这个程序模拟我们在玩这个游戏的时候出招的方式,显然,出招就是一种策略。在这里我们有两种出招方式(也就是两种策略):

第一种策略就是当我们上一次赢得话,这次就采用上一次的出招方式,如果输得话,这次随机选择一个出招方式

第二种策略相对专业,就是根据以往出招的输赢概率算出这次应该如何出招

首先我们还是列出我们所需要创建的组件
Hand:表示我们出招的手势的类(石头、剪子或者布)
IGuessType:表示我们出招策略的接口
FirstType:第一种出招策略的类
SecondType:第二种出招策略的类
Player:玩游戏的人员的类,游戏者

Hand类就是表示游戏者出招方式的类,内部封装了出招的三种方式,我们可以用3个常量来表示。
同时类中有对于出招方式的胜负判断方法和其他方法
public class Hand{
 public static final int ONE_VALUE=0;//表示石头的常量值
 public static final int TWO_VALUE=1;//表示剪子的常量值
 public static final int THREE_VALUE=2;//表示布的常量值
 public static final Hand[] hands={//封装出招的方式的数组,以后可以从此数组中提取一个特定的出招方式对象
  new Hand(ONE_VALUE),
  new Hand(TWO_VALUE),
  new Hand(THREE_VALUE),
 };
 private static final String[] names={//封装出招方式的名称,对应于每一种出招方式对象
  "石头","剪子","布"
 };
 private int handValue;//当前出招方式的数值
 private Hand(int handValue){
  this.handValue=handValue;//私有构造函数,不允许从外部创建
 }
 public static Hand getHand(int handValue){
  return hands[handValue];//通过数值取得一个特定的出招方式对象
 }
 public boolean isWin(Hand target){//当前的出招方式和参数出招方式比较,判断是否胜出
  return (judgeHand(target)==1);
 }
 public boolean isLose(Hand target){//当前的出招方式和参数出招方式比较,判断是否失败
  return (judgeHand(target)==-1);
 }
 private int judgeHand(Hand target){
  if(this.handValue==target.handValue){
   return 0;
  }else if((this.handValue+1)%3==target.handValue){
   return 1;
  }else{
   return -1;
  }
 }
 public String toString(){
  return names[handValue];//改写toString方法
 }
}

IGuessType接口出招策略的接口。实现此接口的类即是一种出招策略,通过实现此接口中的方法可以具有出招操作中的各种操作方法。
在这里策略操作主要有两个方法(当然可以根据需要增加)。首先是下一次出招方法nextHand,第二个是判断前一次出招方式的输赢方法whetherWin(boolean),
如果前一次赢则调用whetherWin(true),否则调用whetherWin(false),通过调用此方法可以记录当前出招的情况,作为下一次出招的参考
public interface IGuessType{
 public abstract Hand nextHand();
 public abstract void whetherWin(boolean win);
}

FirstType类
此类就是一个策略,它当然要实现IGuessType接口。实现nextHand和whetherWin方法。具体的出招方式要看它的nextHand实现代码。
前面已经说过,此策略就是根据上一次的输赢来决定本次的出招方式,如果上次赢,则本次按照同样的方式出招,如上次输则随机
选择一种出招方式。因为要用到随技术,所以要用到java.util.Random类,此类的实例能产生一定范围的随机数。在这里要产生0-2
的随机整数,用于在招式数组中(hands)中随机选取一个招式(Hand对象)。
import java.util.Random;
public class FirstType implements IGuessType{
 private Random random;
 private boolean won=false;//存储出招的输赢状态
 private Hand preHand;//记录前次出招的招式
 public FirstType(int seed){
  random=new Random(see);//根据随机数种子等到一个范围的随机数
 }
 public Hand nextHand(){
  if(!won){
   preHand=Hand.getHand(random.nextInt(3));//如果上次输,则随机选取一个招式
  }
  return preHand;
 }
 public void whetherWin(boolean won){
  this.won=won;
 }
}

SecondType类
此类是另外一个出招策略,同样需要实现IGuessType接口。实现其中的nextHand和whetherWin方法。其中的方法决定此种策略的具体实现。
在此策略中不会考虑前次的出招情况,而是根据一定的分析来决定下一次的出招方式。我们可根据我们的需要决定具体的分析策略,比如
通过概率的分析。
我们需要维护一个整型二维数组来存贮历史的出招获胜记录,比如用history[][]来表示,注意此二维数组的意义
history[前一次的出招][本次出招]
例如
history[0][0]=10;代表上次出石头,本次出石头获胜10次
history[0][1]=20;代表上次出石头,本次出剪刀获胜20次
history[0][2]=15;代表上次出石头,本次出布获胜15次
将三个数相加得到的结果就是45,我们可以通过随机数得到一个大于0,小于45的随机数,
如果随机数
大于0,小于10,本次出石头

大于10,小于30,本次出剪刀

大于30,小于45,本次出布
你明白其中的道理了吗?
依此类推可以得到其他的获胜次数。当然没出招一次需要重新维护history数组的数值
public class SecondType implements IGuessType{
 private Random random;
 private int preHand=0;
 private int currentHand=0;
 private int[][] history={
  {1,1,1},
  {1,1,1},
  {1,1,1},
 };
 public SecondType(int seed){
  this.random=new Random(seed);
 }
 public Hand nextHand(){
  int nowRandom=random.nextInt(getTotal(currentHand));
  int handValue=0;
  if(nwoRandom<history[currentHand][0]{
   handValue=0;
  }else if(nowRandom<history[currentHand][1]+history[currentHand][0]){
   handValue=1;
  }else{
   handValue=2;
  }
  preHand=currentHand;
  currentHand=handValue;
  return Hand.getHand(handValue);
 }
 public int getTotalWin(int preHand){//得到上次出某种招式,本次获胜的的所有招式的次数总和
  int total=0;
  for(int i=0;i<3;i++){
   total=total+history[preHand][i];
  }
  return total;
 }
 public void whetherWin(boolean won){
  if(won){
   history[preHand][currentHand]++;
  }else{
   history[preHand][(currentHand+1)%3]++;//如果当前招式输,那么两外两种招式在本次不会输
   history[preHand][(currentHand+2)%3]++;
  }
 }
}
Player类是游戏者的类,此类需要选取某种策略。
public class Player{
 private String palyerName;//游戏者名称
 private IGuessType currentType;//当前所选取的策略
 private int winCount;//记录应的次数
 private int losecount;//记录输的次数
 private int playCount;//记录玩的次数
 public Player(String playerName,IGuessType currentType){
  this.playerName=playerName;
  this.currentType=currentType;
 }
 public Hand nextHand(){
  return currentType.nextHand();//通过策略得到招式
 }
 public void win(){
  currentType.whetherWin(ture);//出招赢了
  winCount++;
  playCount++;
 }
 publc void lose(){
  currentType.whetherWin(false);//出招输了
  loseCount++;
  playCount++;
 }
 public void draw(){
  playCount++;//出招平了
 }
 public String toString(){
  return "["+playerName+":"+playCount+" games, "+winCount+" win, " + loseCount + " lose]";
 }
}

该测试了
public class MyTest{
 public static void main(String[] args[]){
  int seed1=Integer.parseInt(args[0]);
  int seed2=Integer.parseInt(args[1]);
  Player one=new Player("OnePlayer",new FirstType(seed1));
  Player two=new Player("TwoPlayer",new SecondType(seed2));
  for(int i=0;i<1000;i++){
   Hand nextHand1=one.nextHand();
   Hand nextHand2=two.nextHand();
   if(nextHand1.isWin(nextHand2)){
    System.out.println("Winner"+one);
    one.win();
    two.lose();
   }else if(nextHand1.isLose(nextHand2)){
    System.out.println("Winner"+two);
    one.lose();
    two.win();
   }else{
    System.out.println("Draw......");
    one.draw();
    two.draw();
   }
  }
  System.out.println("Total Result:");
  System.out.println(""+one);
  System.out.println(""+two);
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值