设计模式之桥接模式

it2022-05-08  14

设计模式之桥接模式

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

 

  

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

相关资源:垃圾分类数据集及代码

最新回复(0)