设计模式之桥接模式

设计模式之桥接模式

  桥接模式是为了把抽象与实现相分离,让它们都可以独立的变化。

 

  

 1 interface Say{
 2     public function say();
 3 }
 4 
 5 abstract class Person {
 6     public $mouth;
 7 
 8     public __construct(Say $mouth){
 9         $this->mouth = $mouth;
10     }
11     abstract function say();
12 }
13 
14 class man extends Person{
15     public function say(){
16         $this->mouth->say();
17     }
18 }
19 
20 class woman extends Person{
21     public function say(){
22         $this->mouth->say();
23     }
24 }
25 
26 public class ManMouth implements Say{
27     public function say(){
28         echo "男人说话";
29     }
30 }
31 
32 public class WomenMouth implements Say{
33     public function say(){
34         echo "女人说话";
35     }
36 }
37 
38 class client{
39     Person $person  = new Man(new $ManMouth());
40     $person->say(); // 输出 男人说话
41 
42     Person $person  = new Woman(new $womenMouth());
43     $person->say(); // 输出 女人说话
44 }

 

 

 

 

  

转载于:https://www.cnblogs.com/Super-Man/p/4584689.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值