티스토리 뷰

웹개발/Php

state 패턴 샘플

yaku 2017. 2. 6. 10:17

header('Content-Type: text/html; charset=UTF-8'); // state 패턴 // Capsule // SOLD 있음 , SOLD_OUT 동전 없음 , Capsule 매진, Capsule 판매 ( 상태 ) // 통전 투입, 통전 반환, 손잡이 돌림, Capsule 내보냄 ( 행동 ) class CapsuleMachine { static $SOLD_OUT = 0; //매진인 상태 static $COIN_IN = 1; //동전 넣기를 기다리는 상태 static $HAS_COIN = 2; //동전이 들어있는상대 static $SOLD = 3; //손잡이를 돌리고 난 후 알맹이를 받을수 있는상태 public $_state = 0; //상태를 관리하기 위한 변수 public $_count = 0; //기계에 들어있는 알맹이의 개수를 저장 public function __construct($cnt) { $this->_count = $cnt; //알맹이 갯수가 0 이 아니면 동전넣기를 기다리고 있는 상태로 변환 if ($cnt > 0) { $this->_state = self::$COIN_IN; } } //동전넣기 public function insertCoin() { if ($this->_state == self::$COIN_IN) { $this->_state = self::$HAS_COIN; echo "<br/>동전을 넣었습니다. --> "; } } //동전 반환 public function ejectCoin() { if ($this->_state == self::$HAS_COIN) { $this->_state = self::$COIN_IN; echo "동전을 반환합니다."; } } //손잡이 돌리기 public function turnHandle() { if ($this->_state == self::$HAS_COIN) { $this->_state = self::$SOLD; echo " 핸들을 돌립니다. --> "; $this->dispense(); } else { if ($this->_state == self::$COIN_IN) { echo " 동전을 넣어주세요."; } } } //캡슐 꺼내기 public function dispense() { if ($this->_state == self::$SOLD) { echo "휘리릭 휘리릭 ~ 대굴대굴대굴 캡슐이 나왔습니다. @.@ "; $this->_count--; if ($this->_count == 0) { echo " 캡슐이 매진 되었습니다."; $this->_state = self::$SOLD_OUT; } else { $this->_state = self::$COIN_IN; } } } } $capsule = new CapsuleMachine(2); $capsule->insertCoin(); $capsule->turnHandle(); $capsule->ejectCoin(); $capsule->insertCoin(); $capsule->turnHandle(); $capsule->ejectCoin(); echo "<br/>---------------------------------<br/><br/>"; echo "<br/> === State === <br/>"; interface IState { function insertCoin(); function ejectCoin(); function turnHandle(); function dispense(); } //상태를 클래스로 만든다. //매진 되었음을 알림 상태 class SoldOutState implements IState { public $_capsule; function __construct($capsule) { $this->_capsule = $capsule; } function insertCoin() { } function ejectCoin() { } function turnHandle() { } function dispense() { } } //캡슐을 내보냅니다. class SoldState implements IState { public $_capsule; function __construct($capsule) { $this->_capsule = $capsule; } function insertCoin() { } function ejectCoin() { } function turnHandle() { } function dispense() { echo " 캡슐이 나오고 잇습니다. @.@ "; if ($this->_capsule->_count > 0) { $this->_capsule->setState($this->_capsule->_coinHasState); } else { $this->_capsule->setState($this->_capsule->_soldOutState); echo " 매진 되었습니다."; } } } //동전을 넣어주세요 class CoinInState implements IState { public $_capsule; function __construct($capsule) { $this->_capsule = $capsule; } function insertCoin() { echo "<br/> 동전을 넣으셨습니다."; $this->_capsule->setState($this->_capsule->_coinHasState); } function ejectCoin() { echo "동전을 넣어주세요"; } function turnHandle() { echo "동전을 넣어주세요"; } function dispense() { echo "동전을 넣어주세요"; } } //동전이 들어가 있어용 class CoinHasState implements IState { public $_capsule; function __construct($capsule) { $this->_capsule = $capsule; } function insertCoin() { } function ejectCoin() { echo " 동전을 반환합니다."; $this->_capsule->setState($this->_capsule->_coinInState); } function turnHandle() { echo " <br/> 손잡이를 돌리셨습니다. "; $winNum = rand(0, 10); if ($winNum == 3) { $this->_capsule->setState($this->_capsule->_winState); } else { $this->_capsule->setState($this->_capsule->_soldState); } } function dispense() { } } //동전이 들어가 있어용 class WinState implements IState { public $_capsule; function __construct($capsule) { $this->_capsule = $capsule; } function insertCoin() { } function ejectCoin() { } function turnHandle() { } function dispense() { echo " 오호 추카추카 당첨 되었습니다. 캡슐을 1번 더 뽑을 수 있습니다. "; $this->_capsule->releaseBall(); if ($this->_capsule->_count > 0) { $this->_capsule->setState($this->_capsule->_coinHasState); } else { $this->_capsule->setState($this->_capsule->_soldOutState); echo " 매진 되었습니다."; } } } class CapSule { public $_soldOutState; public $_soldState; public $_coinInState; public $_coinHasState; public $_winState; public $_state; public $_count = 0; function __construct($count) { $this->_soldOutState = new SoldOutState($this); $this->_soldState = new SoldState($this); $this->_coinInState = new CoinInState($this); $this->_coinHasState = new CoinHasState($this); $this->_winState = new WinState($this); $this->_state = $this->_soldOutState; // 기본 상태 매진 상태 if ($count > 0) { $this->_count = $count; echo " <br/> 동전 넣기를 기다립니다. "; $this->_state = $this->_coinInState; // 코인을 기다리는 상태로 전환 } } function insertCoin() { $this->_state->insertCoin(); } function ejectCoin() { $this->_state->ejectCoin(); } function turnHandle() { $this->_state->turnHandle(); $this->_state->dispense(); } function dispense() { } function setState($state) { $this->_state = $state; } function releaseBall() { if ($this->_count != 0) { $this->_count = $this->_count - 1; } } } $m = new CapSule(1); $m->insertCoin(); //$m->ejectCoin(); $m->turnHandle(); $m->insertCoin(); //$m->ejectCoin(); $m->turnHandle(); $m->insertCoin(); //$m->ejectCoin(); $m->turnHandle(); $m->insertCoin(); //$m->ejectCoin(); $m->turnHandle(); $m->insertCoin(); //$m->ejectCoin(); $m->turnHandle();


'웹개발 > Php' 카테고리의 다른 글

PHP CLI 정리하기  (0) 2017.06.08
PHP - DateTime을 활용하여 날짜 계산하기  (0) 2017.04.04
데코레이터패턴 샘플  (0) 2017.02.06
옵저버패턴샘플  (0) 2017.02.06
Apater 패턴 샘플  (0) 2017.02.06
댓글
D-DAY
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함