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 Bridge 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 Bridge Design Pattern, việc thực thi và trừu tượng hóa hàm nằm trong các cấu trúc phân cấp lớp riêng biệt.

Trong ví dụ này, chúng ta có BridgeBook sử dụng BridgeBookCapsImp hoặc BridgeBookStarsImp. BridgeBook sẽ chỉ định một triển khai hoặc thực hiện khác mỗi khi BridgeBook được khởi tạo.

bridge pattern rất hữu ích khi bạn muốn tách một lớp khỏi việc triển khai nó.

Phần code

<?php

abstract class BridgeBook {     
    private $bbAuthor;
    private $bbTitle;
    private $bbImp;
    function __construct($author_in, $title_in, $choice_in) {
      $this->bbAuthor = $author_in;
      $this->bbTitle  = $title_in;
      if ('STARS' == $choice_in) {
        $this->bbImp = new BridgeBookStarsImp();
      } else {
        $this->bbImp = new BridgeBookCapsImp();
      }
    }    
    function showAuthor() {
      return $this->bbImp->showAuthor($this->bbAuthor);
    }
    function showTitle() {
      return $this->bbImp->showTitle($this->bbTitle);
    }
}
 
class BridgeBookAuthorTitle extends BridgeBook {    
    function showAuthorTitle() {
      return $this->showAuthor() . "'s " . $this->showTitle();
    }
}  
 
class BridgeBookTitleAuthor extends BridgeBook {    
    function showTitleAuthor() {
      return $this->showTitle() . ' by ' . $this->showAuthor();
    }
}
 
abstract class BridgeBookImp {    
    abstract function showAuthor($author);
    abstract function showTitle($title);
}

class BridgeBookCapsImp extends BridgeBookImp {    
    function showAuthor($author_in) {
      return strtoupper($author_in); 
    }
    function showTitle($title_in) {
      return strtoupper($title_in); 
    }
}

class BridgeBookStarsImp extends BridgeBookImp {    
    function showAuthor($author_in) {
      return Str_replace(" ","*",$author_in); 
    }
    function showTitle($title_in) {
      return Str_replace(" ","*",$title_in); 
    }
}

  writeln('BEGIN TESTING BRIDGE PATTERN');
  writeln('');
 
  writeln('test 1 - author title with caps');
  $book = new BridgeBookAuthorTitle('Larry Truett','PHP for Cats','CAPS');
  writeln($book->showAuthorTitle());
  writeln('');

  writeln('test 2 - author title with stars');
  $book = new BridgeBookAuthorTitle('Larry Truett','PHP for Cats','STARS');
  writeln($book->showAuthorTitle());
  writeln('');

  writeln('test 3 - title author with caps');
  $book = new BridgeBookTitleAuthor('Larry Truett','PHP for Cats','CAPS');
  writeln($book->showTitleAuthor());
  writeln('');

  writeln('test 4 - title author with stars');
  $book = new BridgeBookTitleAuthor('Larry Truett','PHP for Cats','STARS');
  writeln($book->showTitleAuthor());
  writeln('');

  writeln('END TESTING BRIDGE PATTERN');

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

?>

Kết quả:

BEGIN TESTING BRIDGE PATTERN

test 1 - author title with caps
LARRY TRUETT's PHP FOR CATS

test 2 - author title with stars
Larry*Truett's PHP*for*Cats

test 3 - title author with caps
PHP FOR CATS by LARRY TRUETT

test 4 - title author with stars
PHP*for*Cats by Larry*Truett

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