随便写的一个门面模式 但是又有点注入子系统的功能 先记下吧.
<?php /**门面设计模式 * */ abstract class Trad { public function buy() {} } class Proxy{ private $system = []; /** * 注册子系统 * * @return void */ public function register($class) { $this->system[] = function () use ($class){ return new $class(); }; } public function buy() { foreach($this->system as $v) { $system = $v(); if(method_exists($system,'buy')) { $system->buy(); } else { echo '方法不存在!'; } } } } class Ali extends Trad { public function buy () { echo 'buy ali'; } } class DangDang extends Trad { public function buy () { echo 'buy dangdang'; } } class Jd extends Trad { public function buy () { echo 'buy jd'; } } $proxy = new proxy(); $proxy->register(Ali::class); $proxy->register(DangDang::class); $proxy->register(Jd::class); $proxy->buy();