您的当前位置:首页>全部文章>文章详情

php设计模式之桥接模式(Bridge)

发表于:2019-08-16 10:26:45浏览:61次TAG:
将抽象部分与它的实现部分分离,使他们都可以独立的变抽象与它的实现分离,即抽象类和它的派生类用来实现自己的对象 桥接与适配器模式的关系(适配器模式上面已讲解): - 桥接属于聚合关系,两者关联 但不继承 - 适配器属于组合关系,适配者需要继承源 聚合关系:A对象可以包含B对象 但B对象不是A对象的一部分 ```php imp->operationImp(); } } // 修正抽象化角色,扩展抽象化角色,改变和修正父类对抽象化的定义 class RefinedAbstraction extends Abstraction { public function __construct(Implementor $imp) { $this->imp = $imp; } public function operation() { $this->imp->operationImp(); } } // client $abstration = new RefinedAbstraction(new ConcreteImplementorA()); $abstration->operation(); // string(1) "A" $abstration = new RefinedAbstraction(new ConcreteImplementorB()); $abstration->operation(); // string(1) "B" ?> ``` 输出 ```bash vagrant@vagrant-ubuntu:~$ sudo php bridge.php string(1) "A" string(1) "B" ```
栏目分类全部>
腾讯云采购季云服务器一折促销