设计模式之桥接模式

设计模式之桥接模式

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

 

  

 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 }

 

 

 

 

  

posted @ 2015-06-17 23:55  起个名字七个字  阅读(152)  评论(0)    收藏  举报