Tự học Design Pattern | Code ví dụ Decorator 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 Decorator 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 mẫu Decorator, một lớp sẽ thêm chức năng vào lớp khác mà không thay đổi cấu trúc của các lớp khác.

Trong ví dụ này, lớp Sách sẽ có tiêu đề của nó được BookTitleDecorator hiển thị theo các cách khác nhau và đó là các lớp con BookTitleExclaimDecorator và BookTitleStarDecorator.

Trong ví dụ của tôi, tôi làm điều này bằng cách yêu cầu BookTitleDecorator tạo một bản sao giá trị tiêu đề của Sách, giá trị này sau đó được thay đổi để hiển thị. Tùy thuộc vào việc triển khai, có thể tốt hơn nếu thực sự thay đổi đối tượng ban đầu.

Phần code

<?php

class Book {
    private $author;
    private $title;
    function __construct($title_in, $author_in) {
        $this->author = $author_in;
        $this->title  = $title_in;
    }
    function getAuthor() {
        return $this->author;
    }
    function getTitle() {
        return $this->title;
    }
    function getAuthorAndTitle() {
      return $this->getTitle().' by '.$this->getAuthor();
    }
}

class BookTitleDecorator {
    protected $book;
    protected $title;
    public function __construct(Book $book_in) {
        $this->book = $book_in;
        $this->resetTitle();
    }   
    //doing this so original object is not altered
    function resetTitle() {
        $this->title = $this->book->getTitle();
    }
    function showTitle() {
        return $this->title;
    }
}

class BookTitleExclaimDecorator extends BookTitleDecorator {
    private $btd;
    public function __construct(BookTitleDecorator $btd_in) {
        $this->btd = $btd_in;
    }
    function exclaimTitle() {
        $this->btd->title = "!" . $this->btd->title . "!";
    }
}

class BookTitleStarDecorator extends BookTitleDecorator {
    private $btd;
    public function __construct(BookTitleDecorator $btd_in) {
        $this->btd = $btd_in;
    }
    function starTitle() {
        $this->btd->title = Str_replace(" ","*",$this->btd->title);
    }
}

  writeln('BEGIN TESTING DECORATOR PATTERN');
  writeln('');

  $patternBook = new Book('Gamma, Helm, Johnson, and Vlissides', 'Design Patterns');
 
  $decorator = new BookTitleDecorator($patternBook);
  $starDecorator = new BookTitleStarDecorator($decorator);
  $exclaimDecorator = new BookTitleExclaimDecorator($decorator);
 
  writeln('showing title : ');
  writeln($decorator->showTitle());
  writeln('');
 
  writeln('showing title after two exclaims added : ');
  $exclaimDecorator->exclaimTitle();
  $exclaimDecorator->exclaimTitle();
  writeln($decorator->showTitle());
  writeln('');
 
  writeln('showing title after star added : ');
  $starDecorator->starTitle();
  writeln($decorator->showTitle());
  writeln('');
 
  writeln('showing title after reset: ');
  writeln($decorator->resetTitle());
  writeln($decorator->showTitle());
  writeln('');

  writeln('END TESTING DECORATOR PATTERN');

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

?>

Kết quả:

BEGIN TESTING DECORATOR PATTERN

showing title : 
Design Patterns

showing title after two exclaims added : 
!!Design Patterns!!

showing title after star added : 
!!Design*Patterns!!

showing title after reset: 
Design Patterns

END TESTING DECORATOR 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!