Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Fear Not the Machine of State!

Fear Not the Machine of State!

Do your application’s objects have properties like ‘status’ or ‘state’, boolean properties like ‘active’, ‘paid’ or ‘published’, nullable timestamp properties like ‘paid_on’ or ‘published_at’? These are all good indicators that your project may benefit from the introduction of a State Machine. Proper use of a State Machine can mean less bugs, less undefined behavior, and a more clearly defined API to your object’s internal state, yet many developers are reluctant to use them in their projects, fearing unnecessary complexity, difficult integration or a steep learning curve. The good news is that these concerns are largely unfounded! Join us as we explore the State Machine’s theoretical underpinnings, examine its practical application including examples of its use in popular open source projects, and review available PHP resources for easily leveraging the power of the State Machine in your own projects.

willroth

July 23, 2015
Tweet

More Decks by willroth

Other Decks in Programming

Transcript

  1. Fear Not
    the Machine
    of State!

    View Slide

  2. View Slide

  3. Yitzchok
    Willroth

    View Slide

  4. Yitzchok
    Willroth

    View Slide

  5. Yitzchok
    Willroth
    @coderabbi

    View Slide

  6. Yitzchok
    Willroth
    @coderabbi

    View Slide

  7. Yitzchok
    Willroth
    @coderabbi

    View Slide

  8. View Slide

  9. View Slide

  10. View Slide

  11. View Slide

  12. View Slide

  13. View Slide

  14. View Slide

  15. View Slide

  16. View Slide

  17. View Slide

  18. View Slide

  19. State Pattern

    View Slide

  20. State Pattern
    • Behavioral Pattern

    View Slide

  21. State Pattern
    • Behavioral Pattern
    • Allows an object to alter its behavior when its
    internal state changes, giving the appearance of
    changing class.

    View Slide

  22. View Slide

  23. View Slide

  24. View Slide

  25. View Slide

  26. View Slide

  27. View Slide

  28. View Slide

  29. View Slide

  30. View Slide

  31. Context: maintains an instance of the concrete
    state subclass that defines current
    behavior.
    Abstract State: defines an interface for
    encapsulating all state behavior.
    Concrete State: implements the behavior
    associated with a state of the
    context.

    View Slide

  32. View Slide

  33. View Slide

  34. View Slide

  35. IDENTIFY
    STATES

    View Slide

  36. View Slide

  37. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked

    View Slide

  38. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked

    View Slide

  39. View Slide

  40. IDENTIFY
    TRANSITIONS

    View Slide

  41. View Slide

  42. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked

    View Slide

  43. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked

    View Slide

  44. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked

    View Slide

  45. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked

    View Slide

  46. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked

    View Slide

  47. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked

    View Slide

  48. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked

    View Slide

  49. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked

    View Slide

  50. View Slide

  51. View Slide

  52. class Turnstyle
    {
    const LOCKED_STATE = 'locked';
    const UNLOCKED_STATE = 'unlocked';
    private $status;
    public function __construct($status = null)
    {
    $this->status = is_null($status) ? self::LOCKED_STATE : $status;
    }
    public function getStatus()
    {
    return $this->status;
    }
    public function pass()
    {
    $this->status = self::LOCKED_STATE;
    }
    public function coin()
    {
    $this->status = self::UNLOCKED_STATE;
    }
    }

    View Slide

  53. class Turnstyle
    {
    const LOCKED_STATE = 'locked';
    const UNLOCKED_STATE = 'unlocked';
    private $status;
    public function __construct($status = null)
    {
    $this->status = is_null($status) ? self::LOCKED_STATE : $status;
    }
    public function getStatus()
    {
    return $this->status;
    }
    public function pass()
    {
    $this->status = self::LOCKED_STATE;
    }
    public function coin()
    {
    $this->status = self::UNLOCKED_STATE;
    }
    }

    View Slide

  54. class Turnstyle
    {
    const LOCKED_STATE = 'locked';
    const UNLOCKED_STATE = 'unlocked';
    private $status;
    public function __construct($status = null)
    {
    $this->status = is_null($status) ? self::LOCKED_STATE : $status;
    }
    public function getStatus()
    {
    return $this->status;
    }
    public function pass()
    {
    $this->status = self::LOCKED_STATE;
    }
    public function coin()
    {
    $this->status = self::UNLOCKED_STATE;
    }
    }

    View Slide

  55. class Turnstyle
    {
    const LOCKED_STATE = 'locked';
    const UNLOCKED_STATE = 'unlocked';
    private $status;
    public function __construct($status = null)
    {
    $this->status = is_null($status) ? self::LOCKED_STATE : $status;
    }
    public function getStatus()
    {
    return $this->status;
    }
    public function pass()
    {
    $this->status = self::LOCKED_STATE;
    }
    public function coin()
    {
    $this->status = self::UNLOCKED_STATE;
    }
    }

    View Slide

  56. class Turnstyle
    {
    const LOCKED_STATE = 'locked';
    const UNLOCKED_STATE = 'unlocked';
    private $status;
    public function __construct($status = null)
    {
    $this->status = is_null($status) ? self::LOCKED_STATE : $status;
    }
    public function getStatus()
    {
    return $this->status;
    }
    public function pass()
    {
    $this->status = self::LOCKED_STATE;
    }
    public function coin()
    {
    $this->status = self::UNLOCKED_STATE;
    }
    }

    View Slide

  57. class Turnstyle
    {
    const LOCKED_STATE = 'locked';
    const UNLOCKED_STATE = 'unlocked';
    private $status;
    public function __construct($status = null)
    {
    $this->status = is_null($status) ? self::LOCKED_STATE : $status;
    }
    public function getStatus()
    {
    return $this->status;
    }
    public function pass()
    {
    $this->status = self::LOCKED_STATE;
    }
    public function coin()
    {
    $this->status = self::UNLOCKED_STATE;
    }
    }

    View Slide

  58. class Turnstyle
    {
    const LOCKED_STATE = 'locked';
    const UNLOCKED_STATE = 'unlocked';
    private $status;
    public function __construct($status = null)
    {
    $this->status = is_null($status) ? self::LOCKED_STATE : $status;
    }
    public function getStatus()
    {
    return $this->status;
    }
    public function pass()
    {
    $this->status = self::LOCKED_STATE;
    }
    public function coin()
    {
    $this->status = self::UNLOCKED_STATE;
    }
    }

    View Slide

  59. class Turnstyle
    {
    const LOCKED_STATE = 'locked';
    const UNLOCKED_STATE = 'unlocked';
    private $status;
    public function __construct($status = null)
    {
    $this->status = is_null($status) ? self::LOCKED_STATE : $status;
    }
    public function getStatus()
    {
    return $this->status;
    }
    public function pass()
    {
    $this->status = self::LOCKED_STATE;
    }
    public function coin()
    {
    $this->status = self::UNLOCKED_STATE;
    }
    }

    View Slide

  60. class Turnstyle
    {
    const LOCKED_STATE = 'locked';
    const UNLOCKED_STATE = 'unlocked';
    private $status;
    public function __construct($status = null)
    {
    $this->status = is_null($status) ? self::LOCKED_STATE : $status;
    }
    public function getStatus()
    {
    return $this->status;
    }
    public function pass()
    {
    $this->status = self::LOCKED_STATE;
    }
    public function coin()
    {
    $this->status = self::UNLOCKED_STATE;
    }
    }

    View Slide

  61. class Turnstyle
    {
    const LOCKED_STATE = 'locked';
    const UNLOCKED_STATE = 'unlocked';
    private $status;
    public function __construct($status = null)
    {
    $this->status = is_null($status) ? self::LOCKED_STATE : $status;
    }
    public function getStatus()
    {
    return $this->status;
    }
    public function pass()
    {
    $this->status = self::LOCKED_STATE;
    }
    public function coin()
    {
    $this->status = self::UNLOCKED_STATE;
    }
    }

    View Slide

  62. View Slide

  63. View Slide

  64. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked

    View Slide

  65. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass

    View Slide

  66. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm

    View Slide

  67. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm

    View Slide

  68. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin

    View Slide

  69. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund

    View Slide

  70. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund

    View Slide

  71. View Slide

  72. View Slide

  73. // truncated ...
    public function pass()
    {
    if ($this->status == self::LOCKED_STATE)
    {
    $this->alarm->sound();
    }
    $this->status = self::LOCKED_STATE;
    }
    public function coin()
    {
    if ($this->status == self::UNLOCKED_STATE)
    {
    $this->coinVendor->refund();
    }
    $this->status = self::UNLOCKED_STATE;
    }
    // truncated ...

    View Slide

  74. // truncated ...
    public function pass()
    {
    if ($this->status == self::LOCKED_STATE)
    {
    $this->alarm->sound();
    }
    $this->status = self::LOCKED_STATE;
    }
    public function coin()
    {
    if ($this->status == self::UNLOCKED_STATE)
    {
    $this->coinVendor->refund();
    }
    $this->status = self::UNLOCKED_STATE;
    }
    // truncated ...

    View Slide

  75. // truncated ...
    public function pass()
    {
    if ($this->status == self::LOCKED_STATE)
    {
    $this->alarm->sound();
    }
    $this->status = self::LOCKED_STATE;
    }
    public function coin()
    {
    if ($this->status == self::UNLOCKED_STATE)
    {
    $this->coinVendor->refund();
    }
    $this->status = self::UNLOCKED_STATE;
    }
    // truncated ...

    View Slide

  76. // truncated ...
    public function pass()
    {
    if ($this->status == self::LOCKED_STATE)
    {
    $this->alarm->sound();
    }
    $this->status = self::LOCKED_STATE;
    }
    public function coin()
    {
    if ($this->status == self::UNLOCKED_STATE)
    {
    $this->coinVendor->refund();
    }
    $this->status = self::UNLOCKED_STATE;
    }
    // truncated ...

    View Slide

  77. // truncated ...
    public function pass()
    {
    if ($this->status == self::LOCKED_STATE)
    {
    $this->alarm->sound();
    }
    $this->status = self::LOCKED_STATE;
    }
    public function coin()
    {
    if ($this->status == self::UNLOCKED_STATE)
    {
    $this->coinVendor->refund();
    }
    $this->status = self::UNLOCKED_STATE;
    }
    // truncated ...

    View Slide

  78. View Slide

  79. View Slide

  80. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Coin
    Refund

    View Slide

  81. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Coin
    Refund
    Violation

    View Slide

  82. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass Coin
    Refund
    Violation

    View Slide

  83. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation

    View Slide

  84. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation

    View Slide

  85. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Ready

    View Slide

  86. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Ready
    ResetAlarm

    View Slide

  87. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Ready
    ResetAlarm
    Lock

    View Slide

  88. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Ready
    ResetAlarm
    Lock

    View Slide

  89. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Ready
    ResetAlarm
    Lock
    Coin

    View Slide

  90. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Ready
    ResetAlarm
    Lock
    Coin
    Refund

    View Slide

  91. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Ready
    ResetAlarm
    Lock
    Coin
    Refund

    View Slide

  92. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Ready
    ResetAlarm
    Lock
    Coin
    Refund
    Pass

    View Slide

  93. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Ready
    ResetAlarm
    Lock
    Coin
    Refund
    Pass

    View Slide

  94. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Reset
    Ready
    ResetAlarm
    Lock
    Coin
    Refund
    Pass

    View Slide

  95. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Reset
    ResetAlarm
    Ready
    ResetAlarm
    Lock
    Coin
    Refund
    Pass

    View Slide

  96. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Reset
    ResetAlarm
    Ready
    ResetAlarm
    Lock
    Coin
    Refund
    Pass

    View Slide

  97. View Slide

  98. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Reset
    ResetAlarm
    Ready
    ResetAlarm
    Lock
    Coin
    Refund
    Pass

    View Slide

  99. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Reset
    ResetAlarm
    Ready
    ResetAlarm
    Lock
    Coin
    Refund
    Pass

    View Slide

  100. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Reset
    ResetAlarm
    Ready
    ResetAlarm
    Lock
    NORMAL MODE
    Coin
    Refund
    Pass

    View Slide

  101. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Reset
    ResetAlarm
    Ready
    ResetAlarm
    Lock
    NORMAL MODE
    Coin
    Refund
    Pass

    View Slide

  102. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Reset
    ResetAlarm
    Ready
    ResetAlarm
    Lock
    NORMAL MODE
    DIAGNOSTIC MODE
    Coin
    Refund
    Pass

    View Slide

  103. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Reset
    ResetAlarm
    Ready
    ResetAlarm
    Lock
    NORMAL MODE
    DIAGNOSTIC MODE
    H
    Coin
    Refund
    Pass

    View Slide

  104. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Reset
    ResetAlarm
    Ready
    ResetAlarm
    Lock
    NORMAL MODE
    DIAGNOSTIC MODE
    H
    Coin
    Refund
    Pass

    View Slide

  105. View Slide

  106. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund

    View Slide

  107. View Slide

  108. View Slide

  109. class Turnstile
    {
    private $state;
    public $lockedState;
    public $unlockedState;
    public function __construct(TurnstileState $state = null)
    {
    $this->lockedState = new LockedTurnstileState();
    $this->unlockedState = new UnlockedTurnstileState();
    $this->state = is_null($state) ? $this->lockedState : $state;
    }
    public function getState()
    {
    return $this->state;
    }
    public function setState(TurnstileState $state)
    {
    $this->state = $state;
    }
    public function pass()
    {
    $this->state->pass($this);
    }
    public function coin()
    {
    $this->state->coin($this);
    }
    }

    View Slide

  110. class Turnstile
    {
    private $state;
    public $lockedState;
    public $unlockedState;
    public function __construct(TurnstileState $state = null)
    {
    $this->lockedState = new LockedTurnstileState();
    $this->unlockedState = new UnlockedTurnstileState();
    $this->state = is_null($state) ? $this->lockedState : $state;
    }
    public function getState()
    {
    return $this->state;
    }
    public function setState(TurnstileState $state)
    {
    $this->state = $state;
    }
    public function pass()
    {
    $this->state->pass($this);
    }
    public function coin()
    {
    $this->state->coin($this);
    }
    }

    View Slide

  111. class Turnstile
    {
    private $state;
    public $lockedState;
    public $unlockedState;
    public function __construct(TurnstileState $state = null)
    {
    $this->lockedState = new LockedTurnstileState();
    $this->unlockedState = new UnlockedTurnstileState();
    $this->state = is_null($state) ? $this->lockedState : $state;
    }
    public function getState()
    {
    return $this->state;
    }
    public function setState(TurnstileState $state)
    {
    $this->state = $state;
    }
    public function pass()
    {
    $this->state->pass($this);
    }
    public function coin()
    {
    $this->state->coin($this);
    }
    }

    View Slide

  112. class Turnstile
    {
    private $state;
    public $lockedState;
    public $unlockedState;
    public function __construct(TurnstileState $state = null)
    {
    $this->lockedState = new LockedTurnstileState();
    $this->unlockedState = new UnlockedTurnstileState();
    $this->state = is_null($state) ? $this->lockedState : $state;
    }
    public function getState()
    {
    return $this->state;
    }
    public function setState(TurnstileState $state)
    {
    $this->state = $state;
    }
    public function pass()
    {
    $this->state->pass($this);
    }
    public function coin()
    {
    $this->state->coin($this);
    }
    }

    View Slide

  113. class Turnstile
    {
    private $state;
    public $lockedState;
    public $unlockedState;
    public function __construct(TurnstileState $state = null)
    {
    $this->lockedState = new LockedTurnstileState();
    $this->unlockedState = new UnlockedTurnstileState();
    $this->state = is_null($state) ? $this->lockedState : $state;
    }
    public function getState()
    {
    return $this->state;
    }
    public function setState(TurnstileState $state)
    {
    $this->state = $state;
    }
    public function pass()
    {
    $this->state->pass($this);
    }
    public function coin()
    {
    $this->state->coin($this);
    }
    }

    View Slide

  114. class Turnstile
    {
    private $state;
    public $lockedState;
    public $unlockedState;
    public function __construct(TurnstileState $state = null)
    {
    $this->lockedState = new LockedTurnstileState();
    $this->unlockedState = new UnlockedTurnstileState();
    $this->state = is_null($state) ? $this->lockedState : $state;
    }
    public function getState()
    {
    return $this->state;
    }
    public function setState(TurnstileState $state)
    {
    $this->state = $state;
    }
    public function pass()
    {
    $this->state->pass($this);
    }
    public function coin()
    {
    $this->state->coin($this);
    }
    }

    View Slide

  115. class Turnstile
    {
    private $state;
    public $lockedState;
    public $unlockedState;
    public function __construct(TurnstileState $state = null)
    {
    $this->lockedState = new LockedTurnstileState();
    $this->unlockedState = new UnlockedTurnstileState();
    $this->state = is_null($state) ? $this->lockedState : $state;
    }
    public function getState()
    {
    return $this->state;
    }
    public function setState(TurnstileState $state)
    {
    $this->state = $state;
    }
    public function pass()
    {
    $this->state->pass($this);
    }
    public function coin()
    {
    $this->state->coin($this);
    }
    }

    View Slide

  116. class Turnstile
    {
    private $state;
    public $lockedState;
    public $unlockedState;
    public function __construct(TurnstileState $state = null)
    {
    $this->lockedState = new LockedTurnstileState();
    $this->unlockedState = new UnlockedTurnstileState();
    $this->state = is_null($state) ? $this->lockedState : $state;
    }
    public function getState()
    {
    return $this->state;
    }
    public function setState(TurnstileState $state)
    {
    $this->state = $state;
    }
    public function pass()
    {
    $this->state->pass($this);
    }
    public function coin()
    {
    $this->state->coin($this);
    }
    }

    View Slide

  117. class Turnstile
    {
    private $state;
    public $lockedState;
    public $unlockedState;
    public function __construct(TurnstileState $state = null)
    {
    $this->lockedState = new LockedTurnstileState();
    $this->unlockedState = new UnlockedTurnstileState();
    $this->state = is_null($state) ? $this->lockedState : $state;
    }
    public function getState()
    {
    return $this->state;
    }
    public function setState(TurnstileState $state)
    {
    $this->state = $state;
    }
    public function pass()
    {
    $this->state->pass($this);
    }
    public function coin()
    {
    $this->state->coin($this);
    }
    }

    View Slide

  118. class Turnstile
    {
    private $state;
    public $lockedState;
    public $unlockedState;
    public function __construct(TurnstileState $state = null)
    {
    $this->lockedState = new LockedTurnstileState();
    $this->unlockedState = new UnlockedTurnstileState();
    $this->state = is_null($state) ? $this->lockedState : $state;
    }
    public function getState()
    {
    return $this->state;
    }
    public function setState(TurnstileState $state)
    {
    $this->state = $state;
    }
    public function pass()
    {
    $this->state->pass($this);
    }
    public function coin()
    {
    $this->state->coin($this);
    }
    }

    View Slide

  119. class Turnstile
    {
    private $state;
    public $lockedState;
    public $unlockedState;
    public function __construct(TurnstileState $state = null)
    {
    $this->lockedState = new LockedTurnstileState();
    $this->unlockedState = new UnlockedTurnstileState();
    $this->state = is_null($state) ? $this->lockedState : $state;
    }
    public function getState()
    {
    return $this->state;
    }
    public function setState(TurnstileState $state)
    {
    $this->state = $state;
    }
    public function pass()
    {
    $this->state->pass($this);
    }
    public function coin()
    {
    $this->state->coin($this);
    }
    }

    View Slide

  120. class Turnstile
    {
    private $state;
    public $lockedState;
    public $unlockedState;
    public function __construct(TurnstileState $state = null)
    {
    $this->lockedState = new LockedTurnstileState();
    $this->unlockedState = new UnlockedTurnstileState();
    $this->state = is_null($state) ? $this->lockedState : $state;
    }
    public function getState()
    {
    return $this->state;
    }
    public function setState(TurnstileState $state)
    {
    $this->state = $state;
    }
    public function pass()
    {
    $this->state->pass($this);
    }
    public function coin()
    {
    $this->state->coin($this);
    }
    }

    View Slide

  121. class Turnstile
    {
    private $state;
    public $lockedState;
    public $unlockedState;
    public function __construct(TurnstileState $state = null)
    {
    $this->lockedState = new LockedTurnstileState();
    $this->unlockedState = new UnlockedTurnstileState();
    $this->state = is_null($state) ? $this->lockedState : $state;
    }
    public function getState()
    {
    return $this->state;
    }
    public function setState(TurnstileState $state)
    {
    $this->state = $state;
    }
    public function pass()
    {
    $this->state->pass($this);
    }
    public function coin()
    {
    $this->state->coin($this);
    }
    }

    View Slide

  122. View Slide

  123. abstract class TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    //empty implementation
    }
    public function coin(Turnstile $turnstile)
    {
    //empty implementation
    }
    }

    View Slide

  124. abstract class TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    //empty implementation
    }
    public function coin(Turnstile $turnstile)
    {
    //empty implementation
    }
    }

    View Slide

  125. abstract class TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    //empty implementation
    }
    public function coin(Turnstile $turnstile)
    {
    //empty implementation
    }
    }

    View Slide

  126. abstract class TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    //empty implementation
    }
    public function coin(Turnstile $turnstile)
    {
    //empty implementation
    }
    }

    View Slide

  127. abstract class TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    //empty implementation
    }
    public function coin(Turnstile $turnstile)
    {
    //empty implementation
    }
    }

    View Slide

  128. View Slide

  129. class LockedTurnstileState extends TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    $turnstile->alarm->sound();
    }
    public function coin(Turnstile $turnstile)
    {
    $turnstile->setState($turnstile->unlockedState);
    }
    }

    View Slide

  130. class LockedTurnstileState extends TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    $turnstile->alarm->sound();
    }
    public function coin(Turnstile $turnstile)
    {
    $turnstile->setState($turnstile->unlockedState);
    }
    }

    View Slide

  131. class LockedTurnstileState extends TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    $turnstile->alarm->sound();
    }
    public function coin(Turnstile $turnstile)
    {
    $turnstile->setState($turnstile->unlockedState);
    }
    }

    View Slide

  132. class LockedTurnstileState extends TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    $turnstile->alarm->sound();
    }
    public function coin(Turnstile $turnstile)
    {
    $turnstile->setState($turnstile->unlockedState);
    }
    }

    View Slide

  133. class LockedTurnstileState extends TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    $turnstile->alarm->sound();
    }
    public function coin(Turnstile $turnstile)
    {
    $turnstile->setState($turnstile->unlockedState);
    }
    }

    View Slide

  134. class LockedTurnstileState extends TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    $turnstile->alarm->sound();
    }
    public function coin(Turnstile $turnstile)
    {
    $turnstile->setState($turnstile->unlockedState);
    }
    }

    View Slide

  135. class LockedTurnstileState extends TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    $turnstile->alarm->sound();
    }
    public function coin(Turnstile $turnstile)
    {
    $turnstile->setState($turnstile->unlockedState);
    }
    }

    View Slide

  136. class LockedTurnstileState extends TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    $turnstile->alarm->sound();
    }
    public function coin(Turnstile $turnstile)
    {
    $turnstile->setState($turnstile->unlockedState);
    }
    }

    View Slide

  137. View Slide

  138. class unlockedTurnstileState extends TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    $turnstile->setState($turnstile->lockedState);
    }
    public function coin(Turnstile $turnstile)
    {
    $turnstile->coinVendor->refund();
    }
    }

    View Slide

  139. class unlockedTurnstileState extends TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    $turnstile->setState($turnstile->lockedState);
    }
    public function coin(Turnstile $turnstile)
    {
    $turnstile->coinVendor->refund();
    }
    }

    View Slide

  140. class unlockedTurnstileState extends TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    $turnstile->setState($turnstile->lockedState);
    }
    public function coin(Turnstile $turnstile)
    {
    $turnstile->coinVendor->refund();
    }
    }

    View Slide

  141. class unlockedTurnstileState extends TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    $turnstile->setState($turnstile->lockedState);
    }
    public function coin(Turnstile $turnstile)
    {
    $turnstile->coinVendor->refund();
    }
    }

    View Slide

  142. class unlockedTurnstileState extends TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    $turnstile->setState($turnstile->lockedState);
    }
    public function coin(Turnstile $turnstile)
    {
    $turnstile->coinVendor->refund();
    }
    }

    View Slide

  143. class unlockedTurnstileState extends TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    $turnstile->setState($turnstile->lockedState);
    }
    public function coin(Turnstile $turnstile)
    {
    $turnstile->coinVendor->refund();
    }
    }

    View Slide

  144. class unlockedTurnstileState extends TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    $turnstile->setState($turnstile->lockedState);
    }
    public function coin(Turnstile $turnstile)
    {
    $turnstile->coinVendor->refund();
    }
    }

    View Slide

  145. class unlockedTurnstileState extends TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    $turnstile->setState($turnstile->lockedState);
    }
    public function coin(Turnstile $turnstile)
    {
    $turnstile->coinVendor->refund();
    }
    }

    View Slide

  146. View Slide

  147. View Slide

  148. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund

    View Slide

  149. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Reset
    ResetAlarm
    Ready
    ResetAlarm
    Lock
    Coin
    Refund
    Pass

    View Slide

  150. View Slide

  151. View Slide

  152. class Turnstile
    {
    private $state;
    public $lockedState;
    public $unlockedState;
    public $violationState;
    public function __construct(TurnstileState $state = null)
    {
    $this->lockedState = new LockedTurnstileState();
    $this->unlockedState = new UnlockedTurnstileState();
    $this->violationState = new ViolationTurnstileState();
    $this->state = is_null($state) ? $this->lockedState : $state;
    }
    public function getState()
    {
    return $this->state;
    }
    public function setState(TurnstileState $state)
    {
    $this->state = $state;
    }
    public function pass()
    {
    $this->state->pass($this);
    }
    public function coin()
    {
    $this->state->coin($this);
    }
    public function reset()
    {
    $this->state->reset($this);
    }
    public function ready()
    {
    $this->state->ready($this);
    }
    }

    View Slide

  153. class Turnstile
    {
    private $state;
    public $lockedState;
    public $unlockedState;
    public $violationState;
    public function __construct(TurnstileState $state = null)
    {
    $this->lockedState = new LockedTurnstileState();
    $this->unlockedState = new UnlockedTurnstileState();
    $this->violationState = new ViolationTurnstileState();
    $this->state = is_null($state) ? $this->lockedState : $state;
    }
    public function getState()
    {
    return $this->state;
    }
    public function setState(TurnstileState $state)
    {
    $this->state = $state;
    }
    public function pass()
    {
    $this->state->pass($this);
    }
    public function coin()
    {
    $this->state->coin($this);
    }
    public function reset()
    {
    $this->state->reset($this);
    }
    public function ready()
    {
    $this->state->ready($this);
    }
    }

    View Slide

  154. class Turnstile
    {
    private $state;
    public $lockedState;
    public $unlockedState;
    public $violationState;
    public function __construct(TurnstileState $state = null)
    {
    $this->lockedState = new LockedTurnstileState();
    $this->unlockedState = new UnlockedTurnstileState();
    $this->violationState = new ViolationTurnstileState();
    $this->state = is_null($state) ? $this->lockedState : $state;
    }
    public function getState()
    {
    return $this->state;
    }
    public function setState(TurnstileState $state)
    {
    $this->state = $state;
    }
    public function pass()
    {
    $this->state->pass($this);
    }
    public function coin()
    {
    $this->state->coin($this);
    }
    public function reset()
    {
    $this->state->reset($this);
    }
    public function ready()
    {
    $this->state->ready($this);
    }
    }

    View Slide

  155. class Turnstile
    {
    private $state;
    public $lockedState;
    public $unlockedState;
    public $violationState;
    public function __construct(TurnstileState $state = null)
    {
    $this->lockedState = new LockedTurnstileState();
    $this->unlockedState = new UnlockedTurnstileState();
    $this->violationState = new ViolationTurnstileState();
    $this->state = is_null($state) ? $this->lockedState : $state;
    }
    public function getState()
    {
    return $this->state;
    }
    public function setState(TurnstileState $state)
    {
    $this->state = $state;
    }
    public function pass()
    {
    $this->state->pass($this);
    }
    public function coin()
    {
    $this->state->coin($this);
    }
    public function reset()
    {
    $this->state->reset($this);
    }
    public function ready()
    {
    $this->state->ready($this);
    }
    }

    View Slide

  156. class Turnstile
    {
    private $state;
    public $lockedState;
    public $unlockedState;
    public $violationState;
    public function __construct(TurnstileState $state = null)
    {
    $this->lockedState = new LockedTurnstileState();
    $this->unlockedState = new UnlockedTurnstileState();
    $this->violationState = new ViolationTurnstileState();
    $this->state = is_null($state) ? $this->lockedState : $state;
    }
    public function getState()
    {
    return $this->state;
    }
    public function setState(TurnstileState $state)
    {
    $this->state = $state;
    }
    public function pass()
    {
    $this->state->pass($this);
    }
    public function coin()
    {
    $this->state->coin($this);
    }
    public function reset()
    {
    $this->state->reset($this);
    }
    public function ready()
    {
    $this->state->ready($this);
    }
    }

    View Slide

  157. class Turnstile
    {
    private $state;
    public $lockedState;
    public $unlockedState;
    public $violationState;
    public function __construct(TurnstileState $state = null)
    {
    $this->lockedState = new LockedTurnstileState();
    $this->unlockedState = new UnlockedTurnstileState();
    $this->violationState = new ViolationTurnstileState();
    $this->state = is_null($state) ? $this->lockedState : $state;
    }
    public function getState()
    {
    return $this->state;
    }
    public function setState(TurnstileState $state)
    {
    $this->state = $state;
    }
    public function pass()
    {
    $this->state->pass($this);
    }
    public function coin()
    {
    $this->state->coin($this);
    }
    public function reset()
    {
    $this->state->reset($this);
    }
    public function ready()
    {
    $this->state->ready($this);
    }
    }

    View Slide

  158. class Turnstile
    {
    private $state;
    public $lockedState;
    public $unlockedState;
    public $violationState;
    public function __construct(TurnstileState $state = null)
    {
    $this->lockedState = new LockedTurnstileState();
    $this->unlockedState = new UnlockedTurnstileState();
    $this->violationState = new ViolationTurnstileState();
    $this->state = is_null($state) ? $this->lockedState : $state;
    }
    public function getState()
    {
    return $this->state;
    }
    public function setState(TurnstileState $state)
    {
    $this->state = $state;
    }
    public function pass()
    {
    $this->state->pass($this);
    }
    public function coin()
    {
    $this->state->coin($this);
    }
    public function reset()
    {
    $this->state->reset($this);
    }
    public function ready()
    {
    $this->state->ready($this);
    }
    }

    View Slide

  159. View Slide

  160. abstract class TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    //empty implementation
    }
    public function coin(Turnstile $turnstile)
    {
    //empty implementation
    }
    public function reset(Turnstile $turnstile)
    {
    //empty implementation
    }
    public function ready(Turnstile $turnstile)
    {
    //empty implementation
    }
    }

    View Slide

  161. abstract class TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    //empty implementation
    }
    public function coin(Turnstile $turnstile)
    {
    //empty implementation
    }
    public function reset(Turnstile $turnstile)
    {
    //empty implementation
    }
    public function ready(Turnstile $turnstile)
    {
    //empty implementation
    }
    }

    View Slide

  162. abstract class TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    //empty implementation
    }
    public function coin(Turnstile $turnstile)
    {
    //empty implementation
    }
    public function reset(Turnstile $turnstile)
    {
    //empty implementation
    }
    public function ready(Turnstile $turnstile)
    {
    //empty implementation
    }
    }

    View Slide

  163. abstract class TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    //empty implementation
    }
    public function coin(Turnstile $turnstile)
    {
    //empty implementation
    }
    public function reset(Turnstile $turnstile)
    {
    //empty implementation
    }
    public function ready(Turnstile $turnstile)
    {
    //empty implementation
    }
    }

    View Slide

  164. View Slide

  165. class violationTurnstileState implements TurnstileState
    {
    public function coin(Turnstile $turnstile)
    {
    turnstile->coinVendor->refund();
    }
    public function reset(Turnstile $turnstile)
    {
    $turnstile->alarm->reset();
    }
    public function ready(Turnstile $turnstile)
    {
    $turnstile->alarm->reset();
    $turnstile->setState($turnstile->unlockedTurnstileState);
    }
    }

    View Slide

  166. class violationTurnstileState implements TurnstileState
    {
    public function coin(Turnstile $turnstile)
    {
    turnstile->coinVendor->refund();
    }
    public function reset(Turnstile $turnstile)
    {
    $turnstile->alarm->reset();
    }
    public function ready(Turnstile $turnstile)
    {
    $turnstile->alarm->reset();
    $turnstile->setState($turnstile->unlockedTurnstileState);
    }
    }

    View Slide

  167. class violationTurnstileState implements TurnstileState
    {
    public function coin(Turnstile $turnstile)
    {
    turnstile->coinVendor->refund();
    }
    public function reset(Turnstile $turnstile)
    {
    $turnstile->alarm->reset();
    }
    public function ready(Turnstile $turnstile)
    {
    $turnstile->alarm->reset();
    $turnstile->setState($turnstile->unlockedTurnstileState);
    }
    }

    View Slide

  168. class violationTurnstileState implements TurnstileState
    {
    public function coin(Turnstile $turnstile)
    {
    turnstile->coinVendor->refund();
    }
    public function reset(Turnstile $turnstile)
    {
    $turnstile->alarm->reset();
    }
    public function ready(Turnstile $turnstile)
    {
    $turnstile->alarm->reset();
    $turnstile->setState($turnstile->unlockedTurnstileState);
    }
    }

    View Slide

  169. class violationTurnstileState implements TurnstileState
    {
    public function coin(Turnstile $turnstile)
    {
    turnstile->coinVendor->refund();
    }
    public function reset(Turnstile $turnstile)
    {
    $turnstile->alarm->reset();
    }
    public function ready(Turnstile $turnstile)
    {
    $turnstile->alarm->reset();
    $turnstile->setState($turnstile->unlockedTurnstileState);
    }
    }

    View Slide

  170. class violationTurnstileState implements TurnstileState
    {
    public function coin(Turnstile $turnstile)
    {
    turnstile->coinVendor->refund();
    }
    public function reset(Turnstile $turnstile)
    {
    $turnstile->alarm->reset();
    }
    public function ready(Turnstile $turnstile)
    {
    $turnstile->alarm->reset();
    $turnstile->setState($turnstile->unlockedTurnstileState);
    }
    }

    View Slide

  171. View Slide

  172. class lockedTurnstileState extends TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    $turnstile->alarm->sound();
    $turnstile->setState($turnstile->violationState);
    }
    public function coin(Turnstile $turnstile)
    {
    $turnstile->setState($turnstile->unlockedState);
    }
    }

    View Slide

  173. class lockedTurnstileState extends TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    $turnstile->alarm->sound();
    $turnstile->setState($turnstile->violationState);
    }
    public function coin(Turnstile $turnstile)
    {
    $turnstile->setState($turnstile->unlockedState);
    }
    }

    View Slide

  174. class lockedTurnstileState extends TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    $turnstile->alarm->sound();
    $turnstile->setState($turnstile->violationState);
    }
    public function coin(Turnstile $turnstile)
    {
    $turnstile->setState($turnstile->unlockedState);
    }
    }

    View Slide

  175. class lockedTurnstileState extends TurnstileState
    {
    public function pass(Turnstile $turnstile)
    {
    $turnstile->alarm->sound();
    $turnstile->setState($turnstile->violationState);
    }
    public function coin(Turnstile $turnstile)
    {
    $turnstile->setState($turnstile->unlockedState);
    }
    }

    View Slide

  176. View Slide

  177. View Slide

  178. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Reset
    ResetAlarm
    Ready
    ResetAlarm
    Lock
    Coin
    Refund
    Pass

    View Slide

  179. Locked
    Coin
    Unlock
    Pass
    Lock
    Unlocked
    Pass
    Alarm
    Coin
    Refund
    Violation
    Reset
    ResetAlarm
    Ready
    ResetAlarm
    Lock
    NORMAL MODE
    DIAGNOSTIC MODE
    H
    Coin
    Refund
    Pass

    View Slide

  180. View Slide

  181. Extend the public API of the
    “context” & Abstract “state” classes
    To include new event triggers

    View Slide

  182. Extend the public API of the
    “context” & Abstract “state” classes
    To include new event triggers
    Create a concrete subclass of
    the abstract “state class” to
    encapsulate the new state logic

    View Slide

  183. Extend the public API of the
    “context” & Abstract “state” classes
    To include new event triggers
    Create a concrete subclass of
    the abstract “state class” to
    encapsulate the new state logic
    ADD an instance of the new
    concrete “state” class
    as an instance variable
    on the “context” object

    View Slide

  184. View Slide

  185. Flexibility

    View Slide

  186. CONTROL

    View Slide

  187. VISIBILITY

    View Slide

  188. View Slide

  189. View Slide

  190. View Slide

  191. View Slide

  192. View Slide

  193. View Slide

  194. • State Variables

    View Slide

  195. • State Variables
    • $status

    View Slide

  196. • State Variables
    • $status
    • $state

    View Slide

  197. • State Variables
    • $status
    • $state
    • Boolean Driven Development

    View Slide

  198. • State Variables
    • $status
    • $state
    • Boolean Driven Development
    • $active, $published

    View Slide

  199. • State Variables
    • $status
    • $state
    • Boolean Driven Development
    • $active, $published
    • isActive(), isPublished()

    View Slide

  200. • State Variables
    • $status
    • $state
    • Boolean Driven Development
    • $active, $published
    • isActive(), isPublished()
    • Nullable Timestamps

    View Slide

  201. • State Variables
    • $status
    • $state
    • Boolean Driven Development
    • $active, $published
    • isActive(), isPublished()
    • Nullable Timestamps
    • Multipart Conditionals or Switches

    View Slide

  202. View Slide

  203. View Slide

  204. View Slide

  205. View Slide

  206. View Slide

  207. View Slide

  208. View Slide

  209. View Slide

  210. View Slide

  211. View Slide

  212. View Slide

  213. View Slide

  214. View Slide

  215. View Slide

  216. View Slide

  217. github.com/sebastianbergmann/state

    View Slide

  218. View Slide

  219. View Slide


























  220. View Slide

  221. View Slide

  222. OpenDoor
    [x] Can be closed
    [x] Cannot be opened
    [x] Cannot be locked
    [x] Cannot be unlocked
    ClosedDoor
    [x] Cannot be closed
    [x] Can be opened
    [x] Can be locked
    [x] Cannot be unlocked
    LockedDoor
    [x] Cannot be closed
    [x] Cannot be opened
    [x] Cannot be locked
    [x] Can be unlocked

    View Slide

  223. View Slide

  224. class Door
    {
    private $state;
    public function __construct(DoorState $state)
    {
    $this->setState($state);
    }
    public function open()
    {
    $this->setState($this->state->open());
    }
    public function close()
    {
    $this->setState($this->state->close());
    }
    public function lock()
    {
    $this->setState($this->state->lock());
    }
    public function unlock()
    {
    $this->setState($this->state->unlock());
    }
    private function setState(DoorState $state)
    {
    $this->state = $state;
    }
    }

    View Slide

  225. class Door
    {
    private $state;
    public function __construct(DoorState $state)
    {
    $this->setState($state);
    }
    public function open()
    {
    $this->setState($this->state->open());
    }
    public function close()
    {
    $this->setState($this->state->close());
    }
    public function lock()
    {
    $this->setState($this->state->lock());
    }
    public function unlock()
    {
    $this->setState($this->state->unlock());
    }
    private function setState(DoorState $state)
    {
    $this->state = $state;
    }
    }

    View Slide

  226. class Door
    {
    private $state;
    public function __construct(DoorState $state)
    {
    $this->setState($state);
    }
    public function open()
    {
    $this->setState($this->state->open());
    }
    public function close()
    {
    $this->setState($this->state->close());
    }
    public function lock()
    {
    $this->setState($this->state->lock());
    }
    public function unlock()
    {
    $this->setState($this->state->unlock());
    }
    private function setState(DoorState $state)
    {
    $this->state = $state;
    }
    }

    View Slide

  227. class Door
    {
    private $state;
    public function __construct(DoorState $state)
    {
    $this->setState($state);
    }
    public function open()
    {
    $this->setState($this->state->open());
    }
    public function close()
    {
    $this->setState($this->state->close());
    }
    public function lock()
    {
    $this->setState($this->state->lock());
    }
    public function unlock()
    {
    $this->setState($this->state->unlock());
    }
    private function setState(DoorState $state)
    {
    $this->state = $state;
    }
    }

    View Slide

  228. class Door
    {
    private $state;
    public function __construct(DoorState $state)
    {
    $this->setState($state);
    }
    public function open()
    {
    $this->setState($this->state->open());
    }
    public function close()
    {
    $this->setState($this->state->close());
    }
    public function lock()
    {
    $this->setState($this->state->lock());
    }
    public function unlock()
    {
    $this->setState($this->state->unlock());
    }
    private function setState(DoorState $state)
    {
    $this->state = $state;
    }
    }

    View Slide

  229. class Door
    {
    private $state;
    public function __construct(DoorState $state)
    {
    $this->setState($state);
    }
    public function open()
    {
    $this->setState($this->state->open());
    }
    public function close()
    {
    $this->setState($this->state->close());
    }
    public function lock()
    {
    $this->setState($this->state->lock());
    }
    public function unlock()
    {
    $this->setState($this->state->unlock());
    }
    private function setState(DoorState $state)
    {
    $this->state = $state;
    }
    }

    View Slide

  230. class Door
    {
    private $state;
    public function __construct(DoorState $state)
    {
    $this->setState($state);
    }
    public function open()
    {
    $this->setState($this->state->open());
    }
    public function close()
    {
    $this->setState($this->state->close());
    }
    public function lock()
    {
    $this->setState($this->state->lock());
    }
    public function unlock()
    {
    $this->setState($this->state->unlock());
    }
    private function setState(DoorState $state)
    {
    $this->state = $state;
    }
    }

    View Slide

  231. class Door
    {
    private $state;
    public function __construct(DoorState $state)
    {
    $this->setState($state);
    }
    public function open()
    {
    $this->setState($this->state->open());
    }
    public function close()
    {
    $this->setState($this->state->close());
    }
    public function lock()
    {
    $this->setState($this->state->lock());
    }
    public function unlock()
    {
    $this->setState($this->state->unlock());
    }
    private function setState(DoorState $state)
    {
    $this->state = $state;
    }
    }

    View Slide

  232. View Slide

  233. class ClosedDoorState extends AbstractDoorState
    {
    public function open()
    {
    return new OpenDoorState;
    }
    public function lock()
    {
    return new LockedDoorState;
    }
    }

    View Slide

  234. class ClosedDoorState extends AbstractDoorState
    {
    public function open()
    {
    return new OpenDoorState;
    }
    public function lock()
    {
    return new LockedDoorState;
    }
    }

    View Slide

  235. class ClosedDoorState extends AbstractDoorState
    {
    public function open()
    {
    return new OpenDoorState;
    }
    public function lock()
    {
    return new LockedDoorState;
    }
    }

    View Slide

  236. class ClosedDoorState extends AbstractDoorState
    {
    public function open()
    {
    return new OpenDoorState;
    }
    public function lock()
    {
    return new LockedDoorState;
    }
    }

    View Slide

  237. class ClosedDoorState extends AbstractDoorState
    {
    public function open()
    {
    return new OpenDoorState;
    }
    public function lock()
    {
    return new LockedDoorState;
    }
    }

    View Slide

  238. class ClosedDoorState extends AbstractDoorState
    {
    public function open()
    {
    return new OpenDoorState;
    }
    public function lock()
    {
    return new LockedDoorState;
    }
    }

    View Slide

  239. View Slide

  240. View Slide

  241. • Context Object maintains a reference to the Stateful Object.

    View Slide

  242. • Context Object maintains a reference to the Stateful Object.
    • States & Transitions are mapped and maintained by the
    Context Object.

    View Slide

  243. • Context Object maintains a reference to the Stateful Object.
    • States & Transitions are mapped and maintained by the
    Context Object.
    • State Transitions are introduced externally rather than in
    response to Triggers.

    View Slide

  244. • Context Object maintains a reference to the Stateful Object.
    • States & Transitions are mapped and maintained by the
    Context Object.
    • State Transitions are introduced externally rather than in
    response to Triggers.
    • The Context Object determines if Transitions are allowed.

    View Slide

  245. View Slide

  246. github.com/yohang/finite

    View Slide

  247. View Slide

  248. View Slide

  249. class Document implements Finite\StatefulInterface
    {
    private $state;
    public function getFiniteState()
    {
    return $this->state;
    }
    public function setFiniteState($state)
    {
    $this->state = $state;
    }
    }

    View Slide

  250. class Document implements Finite\StatefulInterface
    {
    private $state;
    public function getFiniteState()
    {
    return $this->state;
    }
    public function setFiniteState($state)
    {
    $this->state = $state;
    }
    }

    View Slide

  251. class Document implements Finite\StatefulInterface
    {
    private $state;
    public function getFiniteState()
    {
    return $this->state;
    }
    public function setFiniteState($state)
    {
    $this->state = $state;
    }
    }

    View Slide

  252. class Document implements Finite\StatefulInterface
    {
    private $state;
    public function getFiniteState()
    {
    return $this->state;
    }
    public function setFiniteState($state)
    {
    $this->state = $state;
    }
    }

    View Slide

  253. class Document implements Finite\StatefulInterface
    {
    private $state;
    public function getFiniteState()
    {
    return $this->state;
    }
    public function setFiniteState($state)
    {
    $this->state = $state;
    }
    }

    View Slide

  254. class Document implements Finite\StatefulInterface
    {
    private $state;
    public function getFiniteState()
    {
    return $this->state;
    }
    public function setFiniteState($state)
    {
    $this->state = $state;
    }
    }

    View Slide

  255. View Slide

  256. $stateMap = [
    'class' => 'Document',
    'states' => [
    'draft' => ['type' => 'initial', 'properties' => []],
    'proposed' => ['type' => 'normal', 'properties' => []],
    'accepted' => ['type' => 'final', 'properties' => []],
    'refused' => ['type' => 'final', 'properties' => []],
    ],
    'transitions' => [
    'propose' => ['from' => ['draft'], 'to' => 'proposed'],
    'accept' => ['from' => ['proposed'], 'to' => 'accepted'],
    'refuse' => ['from' => ['proposed'], 'to' => 'refused'],
    ]
    ];
    $stateMachine = new Finite\StateMachine\StateMachine;
    $loader->load(new Finite\Loader\ArrayLoader($stateMap));
    $stateMachine->setObject(new Document);
    $stateMachine->initialize();

    View Slide

  257. $stateMap = [
    'class' => 'Document',
    'states' => [
    'draft' => ['type' => 'initial', 'properties' => []],
    'proposed' => ['type' => 'normal', 'properties' => []],
    'accepted' => ['type' => 'final', 'properties' => []],
    'refused' => ['type' => 'final', 'properties' => []],
    ],
    'transitions' => [
    'propose' => ['from' => ['draft'], 'to' => 'proposed'],
    'accept' => ['from' => ['proposed'], 'to' => 'accepted'],
    'refuse' => ['from' => ['proposed'], 'to' => 'refused'],
    ]
    ];
    $stateMachine = new Finite\StateMachine\StateMachine;
    $loader->load(new Finite\Loader\ArrayLoader($stateMap));
    $stateMachine->setObject(new Document);
    $stateMachine->initialize();

    View Slide

  258. $stateMap = [
    'class' => 'Document',
    'states' => [
    'draft' => ['type' => 'initial', 'properties' => []],
    'proposed' => ['type' => 'normal', 'properties' => []],
    'accepted' => ['type' => 'final', 'properties' => []],
    'refused' => ['type' => 'final', 'properties' => []],
    ],
    'transitions' => [
    'propose' => ['from' => ['draft'], 'to' => 'proposed'],
    'accept' => ['from' => ['proposed'], 'to' => 'accepted'],
    'refuse' => ['from' => ['proposed'], 'to' => 'refused'],
    ]
    ];
    $stateMachine = new Finite\StateMachine\StateMachine;
    $loader->load(new Finite\Loader\ArrayLoader($stateMap));
    $stateMachine->setObject(new Document);
    $stateMachine->initialize();

    View Slide

  259. $stateMap = [
    'class' => 'Document',
    'states' => [
    'draft' => ['type' => 'initial', 'properties' => []],
    'proposed' => ['type' => 'normal', 'properties' => []],
    'accepted' => ['type' => 'final', 'properties' => []],
    'refused' => ['type' => 'final', 'properties' => []],
    ],
    'transitions' => [
    'propose' => ['from' => ['draft'], 'to' => 'proposed'],
    'accept' => ['from' => ['proposed'], 'to' => 'accepted'],
    'refuse' => ['from' => ['proposed'], 'to' => 'refused'],
    ]
    ];
    $stateMachine = new Finite\StateMachine\StateMachine;
    $loader->load(new Finite\Loader\ArrayLoader($stateMap));
    $stateMachine->setObject(new Document);
    $stateMachine->initialize();

    View Slide

  260. $stateMap = [
    'class' => 'Document',
    'states' => [
    'draft' => ['type' => 'initial', 'properties' => []],
    'proposed' => ['type' => 'normal', 'properties' => []],
    'accepted' => ['type' => 'final', 'properties' => []],
    'refused' => ['type' => 'final', 'properties' => []],
    ],
    'transitions' => [
    'propose' => ['from' => ['draft'], 'to' => 'proposed'],
    'accept' => ['from' => ['proposed'], 'to' => 'accepted'],
    'refuse' => ['from' => ['proposed'], 'to' => 'refused'],
    ]
    ];
    $stateMachine = new Finite\StateMachine\StateMachine;
    $loader->load(new Finite\Loader\ArrayLoader($stateMap));
    $stateMachine->setObject(new Document);
    $stateMachine->initialize();

    View Slide

  261. $stateMap = [
    'class' => 'Document',
    'states' => [
    'draft' => ['type' => 'initial', 'properties' => []],
    'proposed' => ['type' => 'normal', 'properties' => []],
    'accepted' => ['type' => 'final', 'properties' => []],
    'refused' => ['type' => 'final', 'properties' => []],
    ],
    'transitions' => [
    'propose' => ['from' => ['draft'], 'to' => 'proposed'],
    'accept' => ['from' => ['proposed'], 'to' => 'accepted'],
    'refuse' => ['from' => ['proposed'], 'to' => 'refused'],
    ]
    ];
    $stateMachine = new Finite\StateMachine\StateMachine;
    $loader->load(new Finite\Loader\ArrayLoader($stateMap));
    $stateMachine->setObject(new Document);
    $stateMachine->initialize();

    View Slide

  262. $stateMap = [
    'class' => 'Document',
    'states' => [
    'draft' => ['type' => 'initial', 'properties' => []],
    'proposed' => ['type' => 'normal', 'properties' => []],
    'accepted' => ['type' => 'final', 'properties' => []],
    'refused' => ['type' => 'final', 'properties' => []],
    ],
    'transitions' => [
    'propose' => ['from' => ['draft'], 'to' => 'proposed'],
    'accept' => ['from' => ['proposed'], 'to' => 'accepted'],
    'refuse' => ['from' => ['proposed'], 'to' => 'refused'],
    ]
    ];
    $stateMachine = new Finite\StateMachine\StateMachine;
    $loader->load(new Finite\Loader\ArrayLoader($stateMap));
    $stateMachine->setObject(new Document);
    $stateMachine->initialize();

    View Slide

  263. View Slide

  264. echo $stateMachine->getCurrentState();
    // => "draft“
    var_dump($stateMachine->can('accept'));
    // => bool(false)
    var_dump($stateMachine->can('propose'));
    // => bool(true)
    $stateMachine->apply('propose');
    echo $stateMachine->getCurrentState();
    // => "proposed"

    View Slide

  265. echo $stateMachine->getCurrentState();
    // => "draft“
    var_dump($stateMachine->can('accept'));
    // => bool(false)
    var_dump($stateMachine->can('propose'));
    // => bool(true)
    $stateMachine->apply('propose');
    echo $stateMachine->getCurrentState();
    // => "proposed"

    View Slide

  266. echo $stateMachine->getCurrentState();
    // => "draft“
    var_dump($stateMachine->can('accept'));
    // => bool(false)
    var_dump($stateMachine->can('propose'));
    // => bool(true)
    $stateMachine->apply('propose');
    echo $stateMachine->getCurrentState();
    // => "proposed"

    View Slide

  267. echo $stateMachine->getCurrentState();
    // => "draft“
    var_dump($stateMachine->can('accept'));
    // => bool(false)
    var_dump($stateMachine->can('propose'));
    // => bool(true)
    $stateMachine->apply('propose');
    echo $stateMachine->getCurrentState();
    // => "proposed"

    View Slide

  268. echo $stateMachine->getCurrentState();
    // => "draft“
    var_dump($stateMachine->can('accept'));
    // => bool(false)
    var_dump($stateMachine->can('propose'));
    // => bool(true)
    $stateMachine->apply('propose');
    echo $stateMachine->getCurrentState();
    // => "proposed"

    View Slide

  269. View Slide

  270. View Slide

  271. Is Your State & Transition Graph

    View Slide

  272. • exceptionally complex?
    Is Your State & Transition Graph

    View Slide

  273. • exceptionally complex?
    • subject to frequent change?
    Is Your State & Transition Graph

    View Slide

  274. • exceptionally complex?
    • subject to frequent change?
    • externally configurable?
    Is Your State & Transition Graph

    View Slide

  275. View Slide

  276. Follow Roughly the Same Design

    View Slide

  277. Follow Roughly the Same Design
    • Wrapper

    View Slide

  278. Follow Roughly the Same Design
    • Wrapper
    • Polymorphic “Wrappee”

    View Slide

  279. Follow Roughly the Same Design
    • Wrapper
    • Polymorphic “Wrappee”
    • Delegation

    View Slide

  280. Follow Roughly the Same Design
    • Wrapper
    • Polymorphic “Wrappee”
    • Delegation
    Solve the Same Problems

    View Slide

  281. Follow Roughly the Same Design
    • Wrapper
    • Polymorphic “Wrappee”
    • Delegation
    Solve the Same Problems
    • Allow object to alter behavior when internal state changes

    View Slide

  282. Follow Roughly the Same Design
    • Wrapper
    • Polymorphic “Wrappee”
    • Delegation
    Solve the Same Problems
    • Allow object to alter behavior when internal state changes
    Provide the Same Benefits

    View Slide

  283. Follow Roughly the Same Design
    • Wrapper
    • Polymorphic “Wrappee”
    • Delegation
    Solve the Same Problems
    • Allow object to alter behavior when internal state changes
    Provide the Same Benefits
    • Increased Flexibility

    View Slide

  284. Follow Roughly the Same Design
    • Wrapper
    • Polymorphic “Wrappee”
    • Delegation
    Solve the Same Problems
    • Allow object to alter behavior when internal state changes
    Provide the Same Benefits
    • Increased Flexibility
    • Increased Control

    View Slide

  285. Follow Roughly the Same Design
    • Wrapper
    • Polymorphic “Wrappee”
    • Delegation
    Solve the Same Problems
    • Allow object to alter behavior when internal state changes
    Provide the Same Benefits
    • Increased Flexibility
    • Increased Control
    • Increased Visibility

    View Slide

  286. View Slide

  287. Fear Not
    the Machine
    of State!

    View Slide