1. Definition
The State Design Pattern allows an object to change its behavior when its internal state changes. The object will appear to change its class, facilitating a dynamic transition between states.
2. Problem Statement
Imagine you're building a music player application. The player has several states like "Playing", "Paused", "Stopped", etc. Implementing all behaviors for each state within a single class would lead to a lot of conditional statements and a convoluted codebase.
3. Solution
The State pattern suggests creating separate classes for each state, encapsulating state-specific behaviors within them. The context class then switches between these state objects instead of handling state transitions internally.
4. Real-World Use Cases
1. A vending machine that has states like "Idle", "Dispensing", "OutOfStock", and "WaitingForPayment".
2. Game characters having states like "Idle", "Running", "Attacking", and "Defending".
3. Workflow processes that move through stages like "Draft", "Review", "Approved", and "Rejected".
5. Implementation Steps
1. Define a State interface with methods representing user actions.
2. Implement concrete state classes for each possible state.
3. Create a Context class that contains a reference to one of the state objects and delegates user actions to that object.
6. Implementation in PHP
<?php
// State interface
interface State {
public function play();
public function pause();
public function stop();
}
// Concrete states
class PlayingState implements State {
public function play() {
echo "Already playing.\n";
}
public function pause() {
echo "Pausing music.\n";
}
public function stop() {
echo "Stopping music.\n";
}
}
class PausedState implements State {
public function play() {
echo "Resuming music.\n";
}
public function pause() {
echo "Already paused.\n";
}
public function stop() {
echo "Stopping music from paused state.\n";
}
}
// Context class
class MusicPlayer {
private $state;
public function __construct() {
$this->state = new PlayingState();
}
public function setState(State $state) {
$this->state = $state;
}
public function play() {
$this->state->play();
}
public function pause() {
$this->state->pause();
}
public function stop() {
$this->state->stop();
}
}
// Client Code
$player = new MusicPlayer();
$player->play();
$player->setState(new PausedState());
$player->pause();
$player->play();
?>
Output:
Already playing. Already paused. Resuming music.
Explanation:
1. The State interface defines methods that represent user actions, like play, pause, and stop.
2. The PlayingState and PausedState classes are concrete implementations of the State interface, and they handle behaviors specific to each state.
3. The MusicPlayer class (Context) maintains a reference to a state object representing its current state. It delegates user actions to the current state object.
4. In the client code, we create a MusicPlayer object, simulate a few user actions, and change its state.
7. When to use?
The State pattern is beneficial when:
1. An object's behavior depends on its state, and it must change its behavior at runtime depending on that state.
2. Operations have large, multi-way conditional statements that correspond to the object's state. This pattern puts each branch in a separate class.
3. A class is overly complex due to the presence of numerous independent states and transitions between those states.
Comments
Post a Comment