¼ÓÈëÊÕ²Ø | ÉèΪÊ×Ò³ | »áÔ±ÖÐÐÄ | ÎÒҪͶ¸å ÁijÇÕ¾³¤Íø £¨https://www.0635zz.com/£©- ÖÇÄÜÓïÒô½»»¥¡¢ÐÐÒµÖÇÄÜ¡¢AIÓ¦Óá¢ÔƼÆËã¡¢5G!
µ±Ç°Î»Ö㺠Ê×Ò³ > Õ¾³¤Ñ§Ôº > PHP½Ì³Ì > ÕýÎÄ

PHP״̬ģʽʵÏÖ×Ô¶¯ÊÛÂô»ú³öÒûÁÏ»ú״̬ÊÇÕ¦Ñù

·¢²¼Ê±¼ä£º2023-05-11 13:50:43 ËùÊôÀ¸Ä¿£ºPHP½Ì³Ì À´Ô´£º
µ¼¶Á£ºÔÚʵ¼ÊÓ¦ÓÃÖУ¬ÎÒÃÇÓÐʱºò»áÓöµ½“PHP״̬ģʽʵÏÖ×Ô¶¯ÊÛÂô»ú³öÒûÁÏ»ú״̬ÊÇÔõÑù”ÕâÑùµÄÎÊÌ⣬ÎÒÃǸÃÔõÑùÀ´´¦ÀíÄØ£¿ÏÂÎĸø´ó¼Ò½éÉÜÁ˽â¾ö·½·¨£¬Ï£ÍûÕâÆª“PHP״̬ģʽʵÏÖ×Ô¶¯ÊÛÂô»ú³öÒûÁÏ»ú״̬ÊÇÔõ
ÔÚʵ¼ÊÓ¦ÓÃÖУ¬ÎÒÃÇÓÐʱºò»áÓöµ½“PHP״̬ģʽʵÏÖ×Ô¶¯ÊÛÂô»ú³öÒûÁÏ»ú״̬ÊÇÔõÑù”ÕâÑùµÄÎÊÌ⣬ÎÒÃǸÃÔõÑùÀ´´¦ÀíÄØ£¿ÏÂÎĸø´ó¼Ò½éÉÜÁ˽â¾ö·½·¨£¬Ï£ÍûÕâÆª“PHP״̬ģʽʵÏÖ×Ô¶¯ÊÛÂô»ú³öÒûÁÏ»ú״̬ÊÇÔõÑù”ÎÄÕÂÄܰïÖú´ó¼Ò½â¾öÎÊÌâ¡£
 
Ïë±Ø´ó¼Ò¶¼Óùý×Ô¶¯ÊÛÂôµÄ×Ô¶¯ÒûÁÏ»ú°É£¬ÈûÈëÓ²±Ò»òÖ½±Ò£¬Ñ¡ÔñÏëÒªµÄÒûÁÏ£¬ÒûÁϾͻáÔÚ»úÆ÷µÄÏ·½¹ö³ö¡£´ó¼ÒÓÐûÓÐÏà¹ØÈç¹ûÓóÌÐòȥдһ¸öÒûÁÏ»úÒªÔõôÑùʵÏÖÄØ£¿
 
Ê×ÏÈÎÒÃÇ¿ÉÒÔ·ÖÏíÒ»ÏÂÕⲿÒûÁÏ»úÓм¸ÖÖ״̬
 
Ò»¡¢Ã»ÓÐÇ®µÄ״̬
 
¶þ¡¢ÓÐÇ®µÄ״̬
 
Èý¡¢ÊÛ³öµÄ״̬
 
ËÄ¡¢ÏúÊÛÒ»¿ÕµÄ״̬
 
ºÃ°É£¬ÖªµÀÁËÕâЩ״̬֮ºóÎÒÃÇ¿ªÊ¼Ð´´úÂëÁË£¡
 
JuiceMachine.php
 
<?php
 
/**
 
 * ÒûÁÏ»ú
 
 * @author ben
 
 *
 
 */
 
class JuiceMachine{
 
 /**
 
 * Ìǹû»úÒ»¹²´æÔÚËÄÖÖ״̬£ºÃ»Ç®£¬ÓÐÇ®£¬³É¹¦ÊÛ³öÒÔ¼°ÏúÊÛÒ»¿Õ
 
 *
 
 * ûǮµÄ״̬
 
 * @var INT
 
 */
 
 const NOMONEY = 0;
 
 /**
 
 * ÓÐÇ®µÄ״̬
 
 * @var INT
 
 */
 
 const HASMONEY = 1;
 
 /**
 
 * ³É¹¦ÊÛ³öµÄ״̬
 
 * @var INT
 
 */
 
 const SOLD = 2;

 /**
 
 * ÏúÊÛÒ»¿ÕµÄ״̬
 
 * @var INT
 
 */
 
 const SOLDOUT = 3;

 /**
 
 * ¼Ç¼Ìǹû»úµ±Ç°µÄ״̬£¬³õʼ»¯×´Ì¬ÎªÊÛ¿Õ
 
 * @var INT
 
 */
 
 private $_state = JuiceMachine::SOLDOUT;
 
 /**
 
 * ¸Ã±äÁ¿ÓÃÓڼǼÒûÁÏ»úÖÐÒûÁϵÄÊýÁ¿
 
 */
 
 private $_count;
 
 /**
 
 * ¹¹Ôì·½·¨£¬×îÖ÷ÒªÊÇÓÃÀ´³õʼ»¯countºÍstateÊôÐÔµÄ
 
 */
 
 public function __construct($count){
 
   $this->_count = $count;
 
   //µ±ÒûÁÏ»úÖеÄÒûÁÏÊýÁ¿´óÓÚÁãʱ£¬½«ÒûÁÏ»úµÄ×´Ì¬ÖØÖÃΪûÓÐÇ®µÄ״̬¡£
 
   if($this->_count > 0){
 
     $this->_state = JuiceMachine::NOMONEY;
 
   }
 
 }

 /**
 
 * ͶÈëÓ²±Ò
 
 */
 
 public function insertCoin(){
 
   if($this->_state == JuiceMachine::HASMONEY ){
 
     echo "you can't insert another coin!<br />";
 
   }elseif($this->_state == JuiceMachine::NOMONEY){
 
     echo "you just insert a coin<br />";
 
     $this->_state = JuiceMachine::HASMONEY;
 
   }elseif($this->_state == JuiceMachine::SOLD){
 
     echo "wait a minute, we are giving you a bottle of juice<br />";
 
   }elseif($this->_state == JuiceMachine::SOLDOUT){
 
     echo "you can't insert coin, the machine is already soldout<br />";
 
   }
 
 }
 
 /**
 
 * ÍË»ØÓ²±Ò
 
 */
 
 public function retreatCoin(){
 
   if($this->_state == JuiceMachine::HASMONEY ){
 
     echo "coin return!<br />";
 
     $this->_state = JuiceMachine::NOMONEY;
 
   }elseif($this->_state == JuiceMachine::NOMONEY){
 
     echo "you have'nt inserted a coin yet<br />";
 
   }elseif($this->_state == JuiceMachine::SOLD){
 
     echo "sorry, you already clicked the botton<br />";
 
   }elseif($this->_state == JuiceMachine::SOLDOUT){
 
     echo "you have'nt inserted a coin yet<br />";
 
   }
 
 }
 
 /**
 
 * µã»÷ÒûÁ϶ÔÓ¦µÄ°´Å¥
 
 */
 
 public function clickButton(){
 
   if($this->_state == JuiceMachine::HASMONEY ){
 
     echo "you clicked, we are giving you a bottle of juice...<br />";
 
     $this->_state = JuiceMachine::SOLD;  //¸Ä±äÒûÁÏ»úµÄ״̬ΪÊÛ³öģʽ
 
     $this->dispend();
 
   }elseif($this->_state == JuiceMachine::NOMONEY){
 
     echo "you clicked,but you hav'nt inserted a coin yet<br />";
 
   }elseif($this->_state == JuiceMachine::SOLD){
 
     echo "click twice does'nt get you two bottle of juice<br />";
 
   }elseif($this->_state == JuiceMachine::SOLDOUT){
 
     echo "you clicked, but the machine is already soldout<br />";
 
   }
 
 }

 /**
 
 * ·¢·ÅÒûÁÏ
 
 */
 
 public function dispend(){
 
   if($this->_state == JuiceMachine::HASMONEY ){
 
     echo "please click the button first<br />";
 
   }elseif($this->_state == JuiceMachine::NOMONEY){
 
     echo "you need to pay first<br />";
 
   }elseif($this->_state == JuiceMachine::SOLD){
 
     echo "now you get you juice<br />";
 
     //ÒûÁÏ»úÖеÄÒûÁÏÊýÁ¿¼õÒ»
 
     $this->_count--;
 
     if($this->_count <= 0){
 
       echo "opps, runing out of juice<br />";
 
       //Èç¹ûÕâʱÒûÁÏ»úÖÐûÓÐÒûÁÏÁË£¬½«ÒûÁÏ»úµÄ×´Ì¬ÖØÖÃΪÏúÊÛÒ»¿Õ
 
       $this->_state = JuiceMachine::SOLDOUT;
 
     }else{
 
       //½«ÒûÁÏ»úµÄ×´Ì¬ÖØÖÃΪûÓÐÇ®
 
       $this->_state = JuiceMachine::NOMONEY;
 
     }
 
   }elseif($this->_state == JuiceMachine::SOLDOUT){
 
     //ÆäʵÕâÖÖÇé¿ö²»Ó¦¸Ã³öÏÖ
 
     echo "opps, it appears that we don't have any juice left<br />";
 
   }
 
 }
 
}
 
index.php
 
<?php
 
require_once 'JuiceMachine.php';
 
$juiceMachine = new JuiceMachine(1);
 
$juiceMachine->insertCoin();
 
$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
 
µ½Ä¿Ç°ÎªÖ¹ÎÒÃǵijÌÐòÔËÐÐÁ¼ºÃ£¬Ã»ÓгöÏÖʲôÎÊÌ⣬µ«ÊÇ´ÓÕâЩ¶àÖØµÄifÅжÏÖÐÄãÊÇ·ñÐáµ½ÁË»µ´úÂëµÄζµÀÄØ£¿ÓÐÒ»ÌìÎÊÌâÖÕÓÚ³öÏÖÁË£¬ÀϰåÏ£Íûµ±Óû§µã»÷°´Å¥Ê±ÓÐ10%µÄ¸ÅÂÊÄõ½Á½Æ¿ÒûÁÏ£¬ÎÒÃÇÐèҪΪÒûÁÏ»ú¶à¼ÓÒ»¸ö״̬£¬ÕâʱȥÐ޸ĴúÂë¾Í³ÉΪÁËÒ»ÖÖÔÖÄÑ£¬¶øÇҺܿÉÄÜ»áÓ°Ï쵽֮ǰµÄ´úÂ룬´øÀ´ÐµÄbug£¬¿´¿´×´Ì¬Ä£Ê½ÈçºÎ°ïÖúÎÒÃǶȹýÄѹذɣ¡
 
״̬ģʽµÄ¹Ù·½¶¨ÒåÊÇ£º×´Ì¬Ä£Ê½ÔÊÐí¶ÔÏóÔÚÄÚ²¿×´Ì¬¸Ä±äÊǸıäËüµÄÐÐΪ£¬¶ÔÏó¿´ÆðÀ´ºÃÏñÊÇÐÞ¸ÄÁËËüµÄÀà
 
¾ßÌåʵÏÖ´úÂ룺
 
State.php
 
<?php
 
interface State{
 
  /**
 
   * ²åÈëÓ²±Ò
 
   */
 
  public function insertCoin();
 
  /**
 
   * »ØÍËÓ²±Ò
 
   */
 
  public function retreatCoin();

  /**
 
   * µã»÷°´Å¥
 
   */
 
  public function clickButton();
 
  
 
  /**
 
   * ·¢·ÅÒûÁÏ
 
   */
 
  public function dispend();
 
}
 
NomoneyState.php
 
<?php
 
require_once 'State.php';
 
class NomoneyState implements State{

  /**
 
   * ÒûÁÏ»úµÄʵÀý
 
   *
 
   * @var object
 
   */
 
  private $_juiceMachine;
 
  /**
 
   * ¹¹Ôì·½·¨£¬Ö÷ÒªÓÃÓÚ³õʼ»¯ÒûÁÏ»úʵÀý
 
   *
 
   */
 
  public function __construct($juiceMachine){
 
    $this->_juiceMachine = $juiceMachine;
 
  }

 /* (non-PHPdoc)
 
   * @see State::insertCoin()
 
   */
 
  public function insertCoin()
 
  {
 
    // TODO Auto-generated method stub
 
    echo "you just insert a coin<br />";
 
    //½«ÒûÁÏ»úµÄ״̬Çл»³ÉÓÐÇ®µÄ״̬
 
    $this->_juiceMachine->setState($this->_juiceMachine->getHasmoneyState());
 
  }
 
 
 
 /* (non-PHPdoc)
 
   * @see State::retreatCoin()
 
   */
 
  public function retreatCoin()
 
  {
 
    // TODO Auto-generated method stub
 
    echo "you have'nt inserted a coin yet<br />";
 
  }

 /* (non-PHPdoc)
 
   * @see State::clickButton()
 
   */
 
  public function clickButton()
 
  {
 
    // TODO Auto-generated method stub
 
    echo "you clicked,but you hav'nt inserted a coin yet<br />";
 
  }

 /* (non-PHPdoc)
 
   * @see State::dispend()
 
   */
 
  public function dispend()
 
  {
 
    // TODO Auto-generated method stub
 
    echo "you need to pay first<br />";
 
  }
 
}
 
HasmoneyState.php
 
<?php
 
require_once 'State.php';

class HasmoneyState implements State
 
{

  /**
 
   * ÒûÁÏ»úµÄʵÀý
 
   *
 
   * @var object
 
   */
 
  private $_juiceMachine;

  /**
 
   * ¹¹Ôì·½·¨£¬Ö÷ÒªÓÃÓÚ³õʼ»¯ÒûÁÏ»úʵÀý
 
   */
 
  public function __construct($juiceMachine)
 
  {
 
    $this->_juiceMachine = $juiceMachine;
 
  }

  /*
 
   * (non-PHPdoc) @see State::insertCoin()
 
   */
 
  public function insertCoin()
 
  {
 
    // TODO Auto-generated method stub
 
    echo "you can't insert another coin!<br />";
 
  }
 
  
 
  /*
 
   * (non-PHPdoc) @see State::retreatCoin()
 
   */
 
  public function retreatCoin()
 
  {
 
    // TODO Auto-generated method stub
 
    echo "coin return!<br />";
 
    $this->_juiceMachine->setState($this->_juiceMachine->getNomoneyState());
 
  }

  /*
 
   * (non-PHPdoc) @see State::clickButton()
 
   */
 
  public function clickButton()
 
  {
 
    // TODO Auto-generated method stub
 
    echo "you clicked, we are giving you a bottle of juice...<br />";
 
    // ¸Ä±äÒûÁÏ»úµÄ״̬ΪÊÛ³öģʽ
 
    $rand = mt_rand(0, 0);
 
    // µ±Ëæ»úÊýΪ0£¨¼´1/10µÄ¸ÅÂÊ£©²¢ÇÒÒûÁÏ»úÖл¹ÓÐ1Æ¿ÒÔÉϵÄÒûÁÏʱ
 
    if ($rand == 0 && $this->_juiceMachine->getCount() > 1) {
 
      $this->_juiceMachine->setState($this->_juiceMachine->getWinnerState());
 
    } else {
 
      $this->_juiceMachine->setState($this->_juiceMachine->getSoldState());
 
    }
 
  }

  /*
 
   * (non-PHPdoc) @see State::dispend()
 
   */
 
  public function dispend()
 
  {
 
    // TODO Auto-generated method stub
 
    echo "please click the button first<br />";
 
  }
 
}
 
SoldoutState.php
 
<?php
 
require_once 'State.php';
 
class SoldoutState implements State{
 
  /**
 
   * ÒûÁÏ»úµÄʵÀý
 
   *
 
   * @var object
 
   */
 
  private $_juiceMachine;

  /**
 
   * ¹¹Ôì·½·¨£¬Ö÷ÒªÓÃÓÚ³õʼ»¯ÒûÁÏ»úʵÀý
 
   *
 
   */
 
  public function __construct($juiceMachine){
 
    $this->_juiceMachine = $juiceMachine;
 
  }
 
 /* (non-PHPdoc)
 
   * @see State::insertCoin()
 
   */
 
  public function insertCoin()
 
  {
 
    // TODO Auto-generated method stub
 
    echo "you can't insert coin, the machine is already soldout<br />";
 
  }
 
 /* (non-PHPdoc)
 
   * @see State::retreatCoin()
 
   */
 
  public function retreatCoin()
 
  {
 
    // TODO Auto-generated method stub
 
    echo "you have'nt inserted a coin yet<br />";
 
  }
 
 /* (non-PHPdoc)
 
   * @see State::clickButton()
 
   */
 
  public function clickButton()
 
  {
 
    // TODO Auto-generated method stub
 
    echo "you clicked, but the machine is already soldout<br />";
 
  }

 /* (non-PHPdoc)
 
   * @see State::dispend()
 
   */
 
  public function dispend()
 
  {
 
    // TODO Auto-generated method stub
 
    echo "opps, it appears that we don't have any juice left<br />";
 
  }
 
}
 
SoldState.php
 
<?php
 
require_once 'State.php';
 
class SoldState implements State{
 
  /**
 
   * ÒûÁÏ»úµÄʵÀý
 
   *
 
   * @var object
 
   */
 
  private $_juiceMachine;

  /**
 
   * ¹¹Ôì·½·¨£¬Ö÷ÒªÓÃÓÚ³õʼ»¯ÒûÁÏ»úʵÀý
 
   *
 
   */
 
  public function __construct($juiceMachine){
 
    $this->_juiceMachine = $juiceMachine;
 
  }

 /* (non-PHPdoc)
 
   * @see State::insertCoin()
 
   */
 
  public function insertCoin()
 
  {
 
    // TODO Auto-generated method stub
 
    echo "wait a minute, we are giving you a bottle of juice<br />";
 
  }
 
 /* (non-PHPdoc)
 
   * @see State::retreatCoin()
 
   */
 
  public function retreatCoin()
 
  {
 
    // TODO Auto-generated method stub
 
    echo "sorry, you already clicked the botton<br />";
 
  }
 
 /* (non-PHPdoc)
 
   * @see State::clickButton()
 
   */
 
  public function clickButton()
 
  {
 
    // TODO Auto-generated method stub
 
    echo "click twice does'nt get you two bottle of juice<br />";
 
  }
 
 /* (non-PHPdoc)
 
   * @see State::dispend()
 
   */
 
  public function dispend()
 
  {
 
    $this->_juiceMachine->decJuice();
 
    if($this->_juiceMachine->getCount() <= 0){
 
      echo "opps, runing out of juice<br />";
 
      //Èç¹ûÕâʱÒûÁÏ»úÖÐûÓÐÒûÁÏÁË£¬½«ÒûÁÏ»úµÄ×´Ì¬ÖØÖÃΪÏúÊÛÒ»¿Õ
 
       $this->_juiceMachine->setState($this->_juiceMachine->getSoldoutState());
 
    }else{
 
      //½«ÒûÁÏ»úµÄ×´Ì¬ÖØÖÃΪûÓÐÇ®
 
       $this->_juiceMachine->setState($this->_juiceMachine->getNomoneyState());
 
    }
 
  }

}
 
WinnerState.php
 
<?php
 
require_once 'State.php';
 
class WinnerState implements State
 
{
 
  /**
 
   * ÒûÁÏ»úµÄʵÀý
 
   *
 
   * @var object
 
   */
 
  private $_juiceMachine;

  /**
 
   * ¹¹Ôì·½·¨£¬Ö÷ÒªÓÃÓÚ³õʼ»¯ÒûÁÏ»úʵÀý
 
   */
 
  public function __construct($juiceMachine)
 
  {
 
    $this->_juiceMachine = $juiceMachine;
 
  }

  /*
 
   * (non-PHPdoc) @see State::insertCoin()
 
   */
 
  public function insertCoin()
 
  {
 
    // TODO Auto-generated method stub
 
    echo "wait a minute, we are giving you a bottle of juice<br />";
 
  }
 
  /*
 
   * (non-PHPdoc) @see State::retreatCoin()
 
   */
 
  public function retreatCoin()
 
  {
 
    // TODO Auto-generated method stub
 
    echo "sorry, you already clicked the botton<br />";
 
  }

  /*
 
   * (non-PHPdoc) @see State::clickButton()
 
   */
 
  public function clickButton()
 
  {
 
    // TODO Auto-generated method stub
 
    echo "click twice does'nt get you two bottle of juice<br />";
 
  }
 
  /*
 
   * (non-PHPdoc) @see State::dispend()
 
   */
 
  public function dispend()
 
  {
 
    echo "you are a winner! you get two bottle of juice!<br />";
 
    $this->_juiceMachine->decJuice();
 
    if ($this->_juiceMachine->getCount() > 0) {
 
      $this->_juiceMachine->decJuice();
 
      if ($this->_juiceMachine->getCount() <= 0) {
 
        echo "opps, runing out of juice<br />";
 
        // Èç¹ûÕâʱÒûÁÏ»úÖÐûÓÐÒûÁÏÁË£¬½«ÒûÁÏ»úµÄ×´Ì¬ÖØÖÃΪÏúÊÛÒ»¿Õ
 
        $this->_juiceMachine->setState($this->_juiceMachine->getSoldoutState());
 
      } else {
 
        // ½«ÒûÁÏ»úµÄ×´Ì¬ÖØÖÃΪûÓÐÇ®
 
        $this->_juiceMachine->setState($this->_juiceMachine->getSoldoutState());
 
      }
 
    } else {
 
      echo "opps, runing out of juice<br />";
 
      // Èç¹ûÕâʱÒûÁÏ»úÖÐûÓÐÒûÁÏÁË£¬½«ÒûÁÏ»úµÄ×´Ì¬ÖØÖÃΪÏúÊÛÒ»¿Õ
 
      $this->_juiceMachine->setState($this->_juiceMachine->getSoldoutState());
 
    }
 
  }
 
}
 
JuiceMachine.php
 
<?php
 
require_once './state/NomoneyState.php';
 
require_once './state/HasmoneyState.php';
 
require_once './state/SoldState.php';
 
require_once './state/SoldoutState.php';
 
require_once './state/WinnerState.php';
 
class JuiceMachine
 
{

  /**
 
   * ¼Ç¼Ìǹû»úµ±Ç°µÄ״̬£¬³õʼ»¯×´Ì¬ÎªÊÛ¿Õ
 
   *
 
   * @var object
 
   */
 
  private $_state;
 
  /**
 
   * ¸Ã±äÁ¿ÓÃÓڼǼÒûÁÏ»úÖÐÒûÁϵÄÊýÁ¿
 
   */
 
  private $_count;
 
  /**
 
   * ¹¹Ôì·½·¨£¬×îÖ÷ÒªÊÇÓÃÀ´³õʼ»¯countºÍstateÊôÐÔµÄ
 
   */
 
  public function __construct($count)
 
  {
 
    $this->_state = new SoldoutState($this);
 
    $this->_count = $count;
 
    // µ±ÒûÁÏ»úÖеÄÒûÁÏÊýÁ¿´óÓÚÁãʱ£¬½«ÒûÁÏ»úµÄ×´Ì¬ÖØÖÃΪûÓÐÇ®µÄ״̬¡£
 
    if ($this->_count > 0) {
 
      $this->_state = new NomoneyState($this);
 
    }
 
  }

  /*
 
   * (non-PHPdoc) @see State::insertCoin()
 
   */
 
  public function insertCoin()
 
  {
 
    // TODO Auto-generated method stub
 
    $this->_state->insertCoin();
 
  }
 
  /*
 
   * (non-PHPdoc) @see State::retreatCoin()
 
   */
 
  public function retreatCoin()
 
  {
 
    // TODO Auto-generated method stub
 
    $this->_state->retreatCoin();
 
  }
 
  /*
 
   * (non-PHPdoc) @see State::clickButton()
 
   */
 
  public function clickButton()
 
  {
 
    $this->_state->clickButton();
 
    //Æäʵ·¢·ÅÌǹûÊÇÔÚÓû§µã»÷Íê°´Å¥ºó»úÆ÷ÄÚ²¿½øÐеÄËùÓÐûÓбØÒªÔÙдһ¸ödispend·½·¨
 
    $this->_state->dispend();
 
  }
 
  /**
 
   * ÉèÖÃÌǹû»úµÄ״̬
 
   *
 
   * @param State $state
 
   */
 
  public function setState(State $state)
 
  {
 
    $this->_state = $state;
 
  }
 
  /**
 
   * »ñȡûÓÐÇ®µÄ״̬
 
   */
 
  public function getNomoneyState(){
 
    return new NomoneyState($this);
 
  }
 
  /**
 
   * »ñÈ¡ÓÐÇ®µÄ״̬
 
   */
 
  public function getHasmoneyState(){
 
    return new HasmoneyState($this);
 
  }
 
  /**
 
   * »ñÈ¡ÊÛ³öµÄ״̬
 
   */
 
  public function getSoldState(){
 
    return new SoldState($this);
 
  }
 
  /**
 
   * »ñÈ¡ÏúÊÛÒ»¿ÕµÄ״̬
 
   */
 
  public function getSoldoutState(){
 
    return new SoldoutState($this);
 
  }
 
  /**
 
   * »ñÈ¡ÐÒÔËÕßµÄ״̬
 
   */
 
  public function getWinnerState(){
 
    return new WinnerState($this);
 
  }
 
  /**
 
   * »ñÈ¡ÒûÁÏ»úÖÐÒûÁϵÄÊýÁ¿
 
   */
 
  public function getCount(){
 
    return $this->_count;
 
  }
 
  /**
 
   * ½«ÒûÁÏÊýÁ¿¼õÒ»
 
   */
 
  public function decJuice(){
 
    echo "now you get you juice<br />";
 
    //ÒûÁÏ»úÖеÄÒûÁÏÊýÁ¿¼õÒ»
 
    $this->_count--;
 
  }

}
 
index.php
 
<?php
 
require_once 'JuiceMachine.php';

$juiceMachine = new JuiceMachine(2);
 
$juiceMachine->insertCoin();
 
$juiceMachine->clickButton();
 
 

£¨±à¼­£ºÁijÇÕ¾³¤Íø£©

¡¾ÉùÃ÷¡¿±¾Õ¾ÄÚÈݾùÀ´×ÔÍøÂ磬ÆäÏà¹ØÑÔÂÛ½ö´ú±í×÷Õ߸öÈ˹۵㣬²»´ú±í±¾Õ¾Á¢³¡¡£ÈôÎÞÒâÇÖ·¸µ½ÄúµÄȨÀû£¬Ç뼰ʱÓëÁªÏµÕ¾³¤É¾³ýÏà¹ØÄÚÈÝ!