Skip to content

Latest commit

 

History

History
75 lines (62 loc) · 1.26 KB

command.md

File metadata and controls

75 lines (62 loc) · 1.26 KB

Command Design Pattern

The Command pattern turns a request into a stand-alone object that contains all the information about the request. This allows to parameterize methods with different requests, delay execution, or queue operations.

How to apply

// Receiver

class Library
{
    public function add(): void
    {
        echo "Book added";
    }

    public function remove(): void
    {
        echo "Book removed";
    }
}
interface Command
{
    public function execute(): void;
}

class AddCommand implements Command
{
    public function __construct(protected Library $library) {}

    public function execute(): void
    {
        $this->library->add();
    }
}

class RemoveCommand implements Command
{
    public function __construct(protected Library $library) {}

    public function execute(): void
    {
        $this->library->remove();
    }
}
// Sender

class LibraryAssistant
{
    public function process(Command $command) {
        $command->execute();
    }
}

Usage:

$library = new Library();

$add = new AddCommand($library);
$remove = new RemoveCommand($library);

$assistant = new LibraryAssistant();
$assistant->process($add);
$assistant->process($remove);

Output:

Book added
Book removed