Skip to content

Latest commit

 

History

History
70 lines (59 loc) · 1.18 KB

state.md

File metadata and controls

70 lines (59 loc) · 1.18 KB

State Design Pattern

The State pattern allows an object to change its behavior when its internal state changes.

How to apply

interface BookState
{
    public function handle(): void;
}

class AvailableState implements BookState
{
    public function handle(): void
    {
        echo "The book is available";
    }
}

class BorrowedState implements BookState
{
    public function handle(): void
    {
        echo "The book has been borrowed";
    }
}

class ReservedState implements BookState
{
    public function handle(): void
    {
        echo "The book has been reserved for reading";
    }
}
class Book
{
    public function __construct(private BookState $state) {}

    public function setState(BookState $state)
    {
        $this->state = $state;
    }

    public function getState(): void
    {
        $this->state->handle();
    }
}

Usage:

$book = new Book(new AvailableState());
echo $book->getState();

$book->setState(new BorrowedState());
echo $book->getState();

$book->setState(new ReservedState());
echo $book->getState();

Result:

The book is available
The book has been borrowed
The book has been reserved for reading