PHP适配器模式之类适配的代码解析
<?php // 适配器模式-类适配 /** * 需要被适配的类 * 需求:给 Source 新增一个新的方法但又不修改 Source 的源代码 */ class Source { public function action() { echo 'call action', '<br/>'; } } interface Targetable { /** * Source 类中同名的方法 */ function action(); /** * 需要给 Source 类新增的方法 */ function action2(); } /** * 适配器类 */ class Adapter extends Source implements Targetable { public function action2() { echo 'call <b>action2</b>', '<br/>'; } } // test code $ad = new Adapter(); $ad->action(); $ad->action2();
以上就是PHP适配器模式之类适配的代码解析的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1
| 本站CDN由 数掘科技 提供