Adapter Pattern
名前の通り、アダプターの機能。
インターフェースの互換性を持たせるための設計手段。
<? /* * 変換されるクラス */ class Banner{ protected $title = ""; function __construct($arg){ $this->title = $arg; } function show_with_paren(){ echo "((({$this->title})))<br />"; } function show_with_aster(){ echo "***{$this->title}***<br />"; } } /* * アダプターインターフェース */ interface PrintAdapter{ function print_weak();//<-- 新たなインターフェース function print_strong();//<-- 新たなインターフェース } /* * アダプター */ class PrintBannerAdapter extends Banner implements PrintAdapter{ function print_weak(){ $this->show_with_paren();//<-- 二つのインターフェースを変換 } function print_strong(){ $this->show_with_aster();//<-- 二つのインターフェースを変換 } } $print_banner = new PrintBannerAdapter("コーヒーの美味しさについて"); $print_banner->print_strong();//<-- 新たなインターフェースを利用 $print_banner->print_weak();//<-- 新たなインターフェースを利用 echo "<hr />"; /* * アダプター基底クラス */ abstract class AnotherPrintAdapter{ abstract function print_weak();//<-- 新たなインターフェース abstract function print_strong();//<-- 新たなインターフェース } /* * アダプター */ class AnotherPrintBannerAdapter extends AnotherPrintAdapter{ function __construct($title){ $this->banner = new Banner($title); } function print_weak(){ $this->banner->show_with_paren();//<-- 変換前クラスにデレゲート } function print_strong(){ $this->banner->show_with_aster();//<-- 変換前クラスにデレゲート } } $another_print_banner = new AnotherPrintBannerAdapter("ココアの温かさについて"); $another_print_banner->print_strong();//<-- 新たなインターフェースを利用 $another_print_banner->print_weak();//<-- 新たなインターフェースを利用 ?>