PHPAdapter patternObjectAdaptation code analysis
'; } } interface Targetable { /** * Source 类中同名的方法, * 适配器中不需要使用的方法可以不在此接口中定义 */ function action(); /** * 需要给 Source 类新增的方法 */ function action2(); } /** * 适配器类 * 相对于 类适配 更加灵活 */ class Adapter implements Targetable { /** * 不是继承 Source 类, 而是持有 Source 类的实例 */ private $sou = null; public function construct(Source $s) { $this->sou = $s; } public function action() { $this->sou->action(); } public function action2() { echo 'call action2', '
'; } } // test code $s = new Source(); $ad = new Adapter($s); $ad->action(); $ad->action2();
The above is the detailed content of PHP Adapter Pattern Object Adaptation Code Analysis. For more information, please follow other related articles on the PHP Chinese website!