当前位置:首页 > PHP教程 > php面向对象 > 列表

PHP设计模式入门之状态模式原理与实现方法分析

发布:smiling 来源: PHP粉丝网  添加日期:2022-03-02 10:06:57 浏览: 评论:0 

本文实例讲述了PHP设计模式入门之状态模式原理与实现方法,分享给大家供大家参考,具体如下:

想必大家都用过自动售卖的自动饮料机吧,塞入硬币或纸币,选择想要的饮料,饮料就会在机器的下方滚出。大家有没有相关如果用程序去写一个饮料机要怎么样实现呢?

首先我们可以分享一下这部饮料机有几种状态

一、没有钱的状态

二、有钱的状态

三、售出的状态

四、销售一空的状态

好吧,知道了这些状态之后我们开始写代码了!

JuiceMachine.php

  1. <?php 
  2. /** 
  3.  * 饮料机 
  4.  * @author ben 
  5.  * 
  6.  */ 
  7. class JuiceMachine{ 
  8.  /** 
  9.  * 糖果机一共存在四种状态:没钱,有钱,成功售出以及销售一空 
  10.  *  
  11.  * 没钱的状态 
  12.  * @var INT 
  13.  */ 
  14.  const NOMONEY = 0; 
  15.    
  16.  /** 
  17.  * 有钱的状态 
  18.  * @var INT 
  19.  */ 
  20.  const HASMONEY = 1; 
  21.    
  22.  /** 
  23.  * 成功售出的状态 
  24.  * @var INT 
  25.  */ 
  26.  const SOLD = 2; 
  27.    
  28.  /** 
  29.  * 销售一空的状态 
  30.  * @var INT 
  31.  */ 
  32.  const SOLDOUT = 3; 
  33.    
  34.  /** 
  35.  * 记录糖果机当前的状态,初始化状态为售空 
  36.  * @var INT 
  37.  */ 
  38.  private $_state = JuiceMachine::SOLDOUT; 
  39.    
  40.  /** 
  41.  * 该变量用于记录饮料机中饮料的数量 
  42.  */ 
  43.  private $_count;  
  44.    
  45.  /** 
  46.  * 构造方法,最主要是用来初始化count和state属性的 
  47.  */ 
  48.  public function __construct($count){ 
  49.    $this->_count = $count; 
  50.    //当饮料机中的饮料数量大于零时,将饮料机的状态重置为没有钱的状态。 
  51.    if($this->_count > 0){ 
  52.      $this->_state = JuiceMachine::NOMONEY; 
  53.    } 
  54.  } 
  55.    
  56.  /** 
  57.  * 投入硬币 
  58.  */ 
  59.  public function insertCoin(){ 
  60.    if($this->_state == JuiceMachine::HASMONEY ){ 
  61.      echo "you can't insert another coin!<br />"; 
  62.    }elseif($this->_state == JuiceMachine::NOMONEY){ 
  63.      echo "you just insert a coin<br />"; 
  64.      $this->_state = JuiceMachine::HASMONEY; 
  65.    }elseif($this->_state == JuiceMachine::SOLD){ 
  66.      echo "wait a minute, we are giving you a bottle of juice<br />"; 
  67.    }elseif($this->_state == JuiceMachine::SOLDOUT){ 
  68.      echo "you can't insert coin, the machine is already soldout<br />"; 
  69.    } 
  70.  } 
  71.    
  72.  /** 
  73.  * 退回硬币 
  74.  */ 
  75.  public function retreatCoin(){ 
  76.    if($this->_state == JuiceMachine::HASMONEY ){ 
  77.      echo "coin return!<br />"; 
  78.      $this->_state = JuiceMachine::NOMONEY; 
  79.    }elseif($this->_state == JuiceMachine::NOMONEY){ 
  80.      echo "you have'nt inserted a coin yet<br />"; 
  81.    }elseif($this->_state == JuiceMachine::SOLD){ 
  82.      echo "sorry, you already clicked the botton<br />"; 
  83.    }elseif($this->_state == JuiceMachine::SOLDOUT){ 
  84.      echo "you have'nt inserted a coin yet<br />"; 
  85.    } 
  86.  } 
  87.    
  88.  /** 
  89.  * 点击饮料对应的按钮 
  90.  */ 
  91.  public function clickButton(){ 
  92.    if($this->_state == JuiceMachine::HASMONEY ){ 
  93.      echo "you clicked, we are giving you a bottle of juice...<br />"; 
  94.      $this->_state = JuiceMachine::SOLD;  //改变饮料机的状态为售出模式 
  95.      $this->dispend(); 
  96.    }elseif($this->_state == JuiceMachine::NOMONEY){ 
  97.      echo "you clicked,but you hav'nt inserted a coin yet<br />"; 
  98.    }elseif($this->_state == JuiceMachine::SOLD){ 
  99.      echo "click twice does'nt get you two bottle of juice<br />"; 
  100.    }elseif($this->_state == JuiceMachine::SOLDOUT){ 
  101.      echo "you clicked, but the machine is already soldout<br />"; 
  102.    } 
  103.  } 
  104.    
  105.  /** 
  106.  * 发放饮料 
  107.  */ 
  108.  public function dispend(){ 
  109.    if($this->_state == JuiceMachine::HASMONEY ){ 
  110.      echo "please click the button first<br />"; 
  111.    }elseif($this->_state == JuiceMachine::NOMONEY){ 
  112.      echo "you need to pay first<br />"; 
  113.    }elseif($this->_state == JuiceMachine::SOLD){ 
  114.      echo "now you get you juice<br />"; 
  115.      //饮料机中的饮料数量减一 
  116.      $this->_count--; 
  117.      if($this->_count <= 0){ 
  118.        echo "opps, runing out of juice<br />"; 
  119.        //如果这时饮料机中没有饮料了,将饮料机的状态重置为销售一空 
  120.        $this->_state = JuiceMachine::SOLDOUT; 
  121.      }else{ 
  122.        //将饮料机的状态重置为没有钱 
  123.        $this->_state = JuiceMachine::NOMONEY; 
  124.      } 
  125.    }elseif($this->_state == JuiceMachine::SOLDOUT){ 
  126.      //其实这种情况不应该出现 
  127.      echo "opps, it appears that we don't have any juice left<br />"; 
  128.    } 
  129.  } 
  130. } 

index.php

  1. <?php 
  2. require_once 'JuiceMachine.php'; 
  3.    
  4. $juiceMachine = new JuiceMachine(1); 
  5.    
  6. $juiceMachine->insertCoin(); 
  7. $juiceMachine->clickButton(); 

运行的结果是:

you just insert a coin

you clicked, we are giving you a bottle of juice...

now you get you juice

opps, runing out of juice

到目前为止我们的程序运行良好,没有出现什么问题,但是从这些多重的if判断中你是否嗅到了坏代码的味道呢?有一天问题终于出现了,老板希望当用户点击按钮时有10%的概率拿到两瓶饮料,我们需要为饮料机多加一个状态,这时去修改代码就成为了一种灾难,而且很可能会影响到之前的代码,带来新的bug,看看状态模式如何帮助我们度过难关吧!

状态模式的官方定义是:状态模式允许对象在内部状态改变是改变它的行为,对象看起来好像是修改了它的类

用uml类图表示如下:

在我们这个项目中的实际类图如下:

具体实现代码:

State.php

  1. <?php 
  2. interface State{ 
  3.     
  4.   /** 
  5.    * 插入硬币 
  6.    */ 
  7.   public function insertCoin(); 
  8.     
  9.   /** 
  10.    * 回退硬币 
  11.    */ 
  12.   public function retreatCoin(); 
  13.     
  14.   /** 
  15.    * 点击按钮 
  16.    */ 
  17.   public function clickButton(); 
  18.     
  19.   /** 
  20.    * 发放饮料 
  21.    */ 
  22.   public function dispend(); 
  23. } 

NomoneyState.php

  1. <?php 
  2. require_once 'State.php'; 
  3. class NomoneyState implements State{ 
  4.     
  5.   /** 
  6.    * 饮料机的实例 
  7.    *  
  8.    * @var object 
  9.    */ 
  10.   private $_juiceMachine; 
  11.     
  12.   /** 
  13.    * 构造方法,主要用于初始化饮料机实例 
  14.    *  
  15.    */ 
  16.   public function __construct($juiceMachine){ 
  17.     $this->_juiceMachine = $juiceMachine; 
  18.   } 
  19.     
  20.  /* (non-PHPdoc) 
  21.    * @see State::insertCoin() 
  22.    */ 
  23.   public function insertCoin() 
  24.   { 
  25.     // TODO Auto-generated method stub 
  26.     echo "you just insert a coin<br />"; 
  27.     //将饮料机的状态切换成有钱的状态 
  28.     $this->_juiceMachine->setState($this->_juiceMachine->getHasmoneyState()); 
  29.   } 
  30.    
  31.  /* (non-PHPdoc) 
  32.    * @see State::retreatCoin() 
  33.    */ 
  34.   public function retreatCoin() 
  35.   { 
  36.     // TODO Auto-generated method stub 
  37.     echo "you have'nt inserted a coin yet<br />"; 
  38.   } 
  39.    
  40.  /* (non-PHPdoc) 
  41.    * @see State::clickButton() 
  42.    */ 
  43.   public function clickButton() 
  44.   { 
  45.     // TODO Auto-generated method stub 
  46.     echo "you clicked,but you hav'nt inserted a coin yet<br />"; 
  47.   } 
  48.    
  49.  /* (non-PHPdoc) 
  50.    * @see State::dispend() 
  51.    */ 
  52.   public function dispend() 
  53.   { 
  54.     // TODO Auto-generated method stub 
  55.     echo "you need to pay first<br />"; 
  56.   } 
  57. } 

HasmoneyState.php

  1. <?php 
  2. require_once 'State.php'; 
  3.    
  4. class HasmoneyState implements State 
  5. { 
  6.    
  7.   /** 
  8.    * 饮料机的实例 
  9.    * 
  10.    * @var object 
  11.    */ 
  12.   private $_juiceMachine; 
  13.    
  14.   /** 
  15.    * 构造方法,主要用于初始化饮料机实例 
  16.    */ 
  17.   public function __construct($juiceMachine) 
  18.   { 
  19.     $this->_juiceMachine = $juiceMachine; 
  20.   } 
  21.     
  22.   /* 
  23.    * (non-PHPdoc) @see State::insertCoin() 
  24.    */ 
  25.   public function insertCoin() 
  26.   { 
  27.     // TODO Auto-generated method stub 
  28.     echo "you can't insert another coin!<br />"; 
  29.   } 
  30.     
  31.   /* 
  32.    * (non-PHPdoc) @see State::retreatCoin() 
  33.    */ 
  34.   public function retreatCoin() 
  35.   { 
  36.     // TODO Auto-generated method stub 
  37.     echo "coin return!<br />"; 
  38.     $this->_juiceMachine->setState($this->_juiceMachine->getNomoneyState()); 
  39.   } 
  40.     
  41.   /* 
  42.    * (non-PHPdoc) @see State::clickButton() 
  43.    */ 
  44.   public function clickButton() 
  45.   { 
  46.     // TODO Auto-generated method stub 
  47.     echo "you clicked, we are giving you a bottle of juice...<br />"; 
  48.     // 改变饮料机的状态为售出模式 
  49.     $rand = mt_rand(0, 0); 
  50.     // 当随机数为0(即1/10的概率)并且饮料机中还有1瓶以上的饮料时 
  51.     if ($rand == 0 && $this->_juiceMachine->getCount() > 1) { 
  52.       $this->_juiceMachine->setState($this->_juiceMachine->getWinnerState()); 
  53.     } else { 
  54.       $this->_juiceMachine->setState($this->_juiceMachine->getSoldState()); 
  55.     } 
  56.   } 
  57.     
  58.   /* 
  59.    * (non-PHPdoc) @see State::dispend() 
  60.    */ 
  61.   public function dispend() 
  62.   { 
  63.     // TODO Auto-generated method stub 
  64.     echo "please click the button first<br />"; 
  65.   } 
  66. } 

SoldoutState.php

  1. <?php 
  2. require_once 'State.php'; 
  3. class SoldoutState implements State{ 
  4.     
  5.   /** 
  6.    * 饮料机的实例 
  7.    * 
  8.    * @var object 
  9.    */ 
  10.   private $_juiceMachine; 
  11.     
  12.   /** 
  13.    * 构造方法,主要用于初始化饮料机实例 
  14.    * 
  15.    */ 
  16.   public function __construct($juiceMachine){ 
  17.     $this->_juiceMachine = $juiceMachine; 
  18.   } 
  19.     
  20.  /* (non-PHPdoc) 
  21.    * @see State::insertCoin() 
  22.    */ 
  23.   public function insertCoin() 
  24.   { 
  25.     // TODO Auto-generated method stub 
  26.     echo "you can't insert coin, the machine is already soldout<br />"; 
  27.   } 
  28.    
  29.  /* (non-PHPdoc) 
  30.    * @see State::retreatCoin() 
  31.    */ 
  32.   public function retreatCoin() 
  33.   { 
  34.     // TODO Auto-generated method stub 
  35.     echo "you have'nt inserted a coin yet<br />"; 
  36.   } 
  37.    
  38.  /* (non-PHPdoc) 
  39.    * @see State::clickButton() 
  40.    */ 
  41.   public function clickButton() 
  42.   { 
  43.     // TODO Auto-generated method stub 
  44.     echo "you clicked, but the machine is already soldout<br />"; 
  45.   } 
  46.    
  47.  /* (non-PHPdoc) 
  48.    * @see State::dispend() 
  49.    */ 
  50.   public function dispend() 
  51.   { 
  52.     // TODO Auto-generated method stub 
  53.     echo "opps, it appears that we don't have any juice left<br />"; 
  54.   } 
  55. } 

SoldState.php

  1. <?php 
  2. require_once 'State.php'; 
  3. class SoldState implements State{ 
  4.     
  5.   /** 
  6.    * 饮料机的实例 
  7.    * 
  8.    * @var object 
  9.    */ 
  10.   private $_juiceMachine; 
  11.     
  12.   /** 
  13.    * 构造方法,主要用于初始化饮料机实例 
  14.    * 
  15.    */ 
  16.   public function __construct($juiceMachine){ 
  17.     $this->_juiceMachine = $juiceMachine; 
  18.   } 
  19.     
  20.  /* (non-PHPdoc) 
  21.    * @see State::insertCoin() 
  22.    */ 
  23.   public function insertCoin() 
  24.   { 
  25.     // TODO Auto-generated method stub 
  26.     echo "wait a minute, we are giving you a bottle of juice<br />"; 
  27.   } 
  28.    
  29.  /* (non-PHPdoc) 
  30.    * @see State::retreatCoin() 
  31.    */ 
  32.   public function retreatCoin() 
  33.   { 
  34.     // TODO Auto-generated method stub 
  35.     echo "sorry, you already clicked the botton<br />"; 
  36.   } 
  37.    
  38.  /* (non-PHPdoc) 
  39.    * @see State::clickButton() 
  40.    */ 
  41.   public function clickButton() 
  42.   { 
  43.     // TODO Auto-generated method stub 
  44.     echo "click twice does'nt get you two bottle of juice<br />"; 
  45.   } 
  46.    
  47.  /* (non-PHPdoc) 
  48.    * @see State::dispend() 
  49.    */ 
  50.   public function dispend() 
  51.   { 
  52.     $this->_juiceMachine->decJuice(); 
  53.     if($this->_juiceMachine->getCount() <= 0){ 
  54.       echo "opps, runing out of juice<br />"; 
  55.       //如果这时饮料机中没有饮料了,将饮料机的状态重置为销售一空 
  56.        $this->_juiceMachine->setState($this->_juiceMachine->getSoldoutState()); 
  57.     }else{ 
  58.       //将饮料机的状态重置为没有钱 
  59.        $this->_juiceMachine->setState($this->_juiceMachine->getNomoneyState()); 
  60.     } 
  61.   } 
  62.     
  63. } 

WinnerState.php

  1. <?php 
  2. require_once 'State.php'; 
  3.    
  4. class WinnerState implements State 
  5. { 
  6.    
  7.   /** 
  8.    * 饮料机的实例 
  9.    * 
  10.    * @var object 
  11.    */ 
  12.   private $_juiceMachine; 
  13.    
  14.   /** 
  15.    * 构造方法,主要用于初始化饮料机实例 
  16.    */ 
  17.   public function __construct($juiceMachine) 
  18.   { 
  19.     $this->_juiceMachine = $juiceMachine; 
  20.   } 
  21.     
  22.   /* 
  23.    * (non-PHPdoc) @see State::insertCoin() 
  24.    */ 
  25.   public function insertCoin() 
  26.   { 
  27.     // TODO Auto-generated method stub 
  28.     echo "wait a minute, we are giving you a bottle of juice<br />"; 
  29.   } 
  30.     
  31.   /* 
  32.    * (non-PHPdoc) @see State::retreatCoin() 
  33.    */ 
  34.   public function retreatCoin() 
  35.   { 
  36.     // TODO Auto-generated method stub 
  37.     echo "sorry, you already clicked the botton<br />"; 
  38.   } 
  39.     
  40.   /* 
  41.    * (non-PHPdoc) @see State::clickButton() 
  42.    */ 
  43.   public function clickButton() 
  44.   { 
  45.     // TODO Auto-generated method stub 
  46.     echo "click twice does'nt get you two bottle of juice<br />"; 
  47.   } 
  48.     
  49.   /* 
  50.    * (non-PHPdoc) @see State::dispend() 
  51.    */ 
  52.   public function dispend() 
  53.   { 
  54.     echo "you are a winner! you get two bottle of juice!<br />"; 
  55.     $this->_juiceMachine->decJuice(); 
  56.     if ($this->_juiceMachine->getCount() > 0) { 
  57.       $this->_juiceMachine->decJuice(); 
  58.       if ($this->_juiceMachine->getCount() <= 0) { 
  59.         echo "opps, runing out of juice<br />"; 
  60.         // 如果这时饮料机中没有饮料了,将饮料机的状态重置为销售一空 
  61.         $this->_juiceMachine->setState($this->_juiceMachine->getSoldoutState()); 
  62.       } else { 
  63.         // 将饮料机的状态重置为没有钱 
  64.         $this->_juiceMachine->setState($this->_juiceMachine->getSoldoutState()); 
  65.       } 
  66.     } else { 
  67.       echo "opps, runing out of juice<br />"; 
  68.       // 如果这时饮料机中没有饮料了,将饮料机的状态重置为销售一空 
  69.       $this->_juiceMachine->setState($this->_juiceMachine->getSoldoutState()); 
  70.     } 
  71.   } 
  72. } 

JuiceMachine.php

  1. <?php 
  2. require_once './state/NomoneyState.php'; 
  3. require_once './state/HasmoneyState.php'; 
  4. require_once './state/SoldState.php'; 
  5. require_once './state/SoldoutState.php'; 
  6. require_once './state/WinnerState.php'; 
  7.    
  8. class JuiceMachine 
  9. { 
  10.    
  11.   /** 
  12.    * 记录糖果机当前的状态,初始化状态为售空 
  13.    *  
  14.    * @var object 
  15.    */ 
  16.   private $_state; 
  17.    
  18.   /** 
  19.    * 该变量用于记录饮料机中饮料的数量 
  20.    */ 
  21.   private $_count; 
  22.    
  23.   /** 
  24.    * 构造方法,最主要是用来初始化count和state属性的 
  25.    */ 
  26.   public function __construct($count) 
  27.   { 
  28.     $this->_state = new SoldoutState($this); 
  29.     $this->_count = $count; 
  30.     // 当饮料机中的饮料数量大于零时,将饮料机的状态重置为没有钱的状态。 
  31.     if ($this->_count > 0) { 
  32.       $this->_state = new NomoneyState($this); 
  33.     } 
  34.   } 
  35.     
  36.   /* 
  37.    * (non-PHPdoc) @see State::insertCoin() 
  38.    */ 
  39.   public function insertCoin() 
  40.   { 
  41.     // TODO Auto-generated method stub 
  42.     $this->_state->insertCoin(); 
  43.   } 
  44.     
  45.   /* 
  46.    * (non-PHPdoc) @see State::retreatCoin() 
  47.    */ 
  48.   public function retreatCoin() 
  49.   { 
  50.     // TODO Auto-generated method stub 
  51.     $this->_state->retreatCoin(); 
  52.   } 
  53.     
  54.   /* 
  55.    * (non-PHPdoc) @see State::clickButton() 
  56.    */ 
  57.   public function clickButton() 
  58.   { 
  59.     $this->_state->clickButton(); 
  60.     //其实发放糖果是在用户点击完按钮后机器内部进行的所有没有必要再写一个dispend方法 
  61.     $this->_state->dispend(); 
  62.   } 
  63.     
  64.   /** 
  65.    * 设置糖果机的状态 
  66.    *  
  67.    * @param State $state 
  68.    */ 
  69.   public function setState(State $state) 
  70.   { 
  71.     $this->_state = $state; 
  72.   } 
  73.     
  74.   /** 
  75.    * 获取没有钱的状态 
  76.    */ 
  77.   public function getNomoneyState(){ 
  78.     return new NomoneyState($this); 
  79.   } 
  80.     
  81.   /** 
  82.    * 获取有钱的状态 
  83.    */ 
  84.   public function getHasmoneyState(){ 
  85.     return new HasmoneyState($this); 
  86.   } 
  87.     
  88.   /** 
  89.    * 获取售出的状态 
  90.    */ 
  91.   public function getSoldState(){ 
  92.     return new SoldState($this); 
  93.   } 
  94.     
  95.   /** 
  96.    * 获取销售一空的状态 
  97.    */ 
  98.   public function getSoldoutState(){ 
  99.     return new SoldoutState($this); 
  100.   } 
  101.     
  102.   /** 
  103.    * 获取幸运者的状态 
  104.    */ 
  105.   public function getWinnerState(){ 
  106.     return new WinnerState($this); 
  107.   } 
  108.     
  109.   /** 
  110.    * 获取饮料机中饮料的数量 
  111.    */ 
  112.   public function getCount(){ 
  113.     return $this->_count; 
  114.   } 
  115.     
  116.   /** 
  117.    * 将饮料数量减一 
  118.    */ 
  119.   public function decJuice(){ 
  120.     echo "now you get you juice<br />"; 
  121.     //饮料机中的饮料数量减一 
  122.     $this->_count--; 
  123.   } 
  124.     
  125. } 

index.php

  1. <?php 
  2. require_once 'JuiceMachine.php'; 
  3.    
  4. $juiceMachine = new JuiceMachine(2); 
  5.    
  6. $juiceMachine->insertCoin(); 
  7. $juiceMachine->clickButton();

Tags: PHP设计模式 PHP状态模式

分享到: