Tự học Design Pattern | Code ví dụ Command design pattern trong PHP » Cafedev.vn

Qua các series tự học về Design Pattern, Hôm nay cafedevn chia sẻ cho ace ví dụ và code cụ thể về cách sử dụng Command design pattern với ngôn ngữ lập trình PHP. Nhằm giúp ace hiểu rõ cách sử Pattern này với PHP một cách nhanh nhất và áp dụng nó vào thực tế.

Mô tả code

Trong Command Pattern, một đối tượng đóng gói mọi thứ cần thiết để thực thi một phương thức trong một đối tượng khác.

Trong ví dụ này, một đối tượng BookStarsOnCommand được khởi tạo với một thể hiện của lớp BookComandee. Đối tượng BookStarsOnCommand sẽ gọi hàm bookStarsOn() của đối tượng BookComandee đó khi hàm execute() của nó được gọi.

Phần code

<?php

class BookCommandee {
    private $author;
    private $title;
    function __construct($title_in, $author_in) {
        $this->setAuthor($author_in);
        $this->setTitle($title_in);
    }
    function getAuthor() {
        return $this->author;
    }
    function setAuthor($author_in) {
        $this->author = $author_in;
    }
    function getTitle() {
        return $this->title;
    }
    function setTitle($title_in) {
        $this->title = $title_in;
    }
    function setStarsOn() {
        $this->setAuthor(Str_replace(' ','*',$this->getAuthor()));
        $this->setTitle(Str_replace(' ','*',$this->getTitle()));
    }
    function setStarsOff() {
        $this->setAuthor(Str_replace('*',' ',$this->getAuthor()));
        $this->setTitle(Str_replace('*',' ',$this->getTitle()));
    }
    function getAuthorAndTitle() {
        return $this->getTitle().' by '.$this->getAuthor();
    }
}

abstract class BookCommand {
    protected $bookCommandee;
    function __construct($bookCommandee_in) {
        $this->bookCommandee = $bookCommandee_in;
    }
    abstract function execute();
}

class BookStarsOnCommand extends BookCommand {
    function execute() {
        $this->bookCommandee->setStarsOn();
    }
}

class BookStarsOffCommand extends BookCommand {
    function execute() {
        $this->bookCommandee->setStarsOff();
    }
}

  writeln('BEGIN TESTING COMMAND PATTERN');
  writeln('');
 
  $book = new BookCommandee('Design Patterns', 'Gamma, Helm, Johnson, and Vlissides');
  writeln('book after creation: ');
  writeln($book->getAuthorAndTitle());
  writeln('');
 
  $starsOn = new BookStarsOnCommand($book);
  callCommand($starsOn);
  writeln('book after stars on: ');
  writeln($book->getAuthorAndTitle());
  writeln('');
 
  $starsOff = new BookStarsOffCommand($book);
  callCommand($starsOff);
  writeln('book after stars off: ');
  writeln($book->getAuthorAndTitle());
  writeln('');

  writeln('END TESTING COMMAND PATTERN');
 
  // the callCommand function demonstrates that a specified
  // function in BookCommandee can be executed with only 
  // an instance of BookCommand.
  function callCommand(BookCommand $bookCommand_in) {
    $bookCommand_in->execute();
  }

  function writeln($line_in) {
    echo $line_in."<br/>";
  }

?>

Kết quả:

BEGIN TESTING COMMAND PATTERN

book after creation: 
Design Patterns by Gamma, Helm, Johnson, and Vlissides

book after stars on: 
Design*Patterns by Gamma,*Helm,*Johnson,*and*Vlissides

book after stars off: 
Design Patterns by Gamma, Helm, Johnson, and Vlissides

END TESTING COMMAND PATTERN

Cài ứng dụng cafedev để dễ dàng cập nhật tin và học lập trình mọi lúc mọi nơi tại đây.

Tài liệu từ cafedev:

Nếu bạn thấy hay và hữu ích, bạn có thể tham gia các kênh sau của cafedev để nhận được nhiều hơn nữa:

Chào thân ái và quyết thắng!

Đăng ký kênh youtube để ủng hộ Cafedev nha các bạn, Thanks you!