组合模式 - 设计模式 - PHP版

2021-05-18 13:29

阅读:634

标签:php   增加节点   资源   人力资源   人才   isset   row   turn   科技   

  1 php
  2 /*
  3  * 组合模式
  4  * 
  5  * 参考:http://www.cnblogs.com/praglody/p/6783317.html
  6  * 
  7  */
  8 /**
  9  * 组合模式抽象基类
 10  */
 11 abstract class CompanyBase {
 12     //节点名称
 13     protected $name;
 14     public function __construct($name) {
 15         $this->name = $name;
 16     }
 17     public function getName() {
 18         return $this->name;
 19     }
 20     //增加节点
 21     abstract function add(CompanyBase $c);
 22     //删除节点
 23     abstract function remove(CompanyBase $c);
 24     //输出节点信息
 25     abstract function show($deep);
 26     //节点职责
 27     abstract function work($deep);
 28 }
 29 /**
 30  * 公司类
 31  */
 32 class Company extends CompanyBase {
 33     protected $item = [];
 34     public function add(CompanyBase $c) {
 35         $nodeName = $c->getName();
 36         if (!isset($this->item[$nodeName])) {
 37             $this->item[$nodeName] = $c;
 38         } else {
 39             throw new Exception("该节点已存在,节点名称:" . $nodeName);
 40         }
 41     }
 42     public function remove(CompanyBase $c) {
 43         $nodeName = $c->getName();
 44         if (isset($this->item[$nodeName])) {
 45             unset($this->item[$nodeName]);
 46         } else {
 47             throw new Exception("该节点不存在,节点名称:" . $nodeName);
 48         }
 49     }
 50     public function show($deep = 0) {
 51         echo str_repeat("-", $deep) . $this->name;
 52         echo "
"; 53 foreach ($this->item as $value) { 54 $value->show($deep + 4); 55 } 56 } 57 public function work($deep = 0) { 58 foreach ($this->item as $value) { 59 echo str_repeat(" ", $deep) . "[{$this->name}]
"; 60 $value->work($deep + 2); 61 } 62 } 63 } 64 /** 65 * 人力资源部门 66 */ 67 class HumanResources extends CompanyBase { 68 public function add(CompanyBase $c) { 69 throw new Exception("该节点下不能增加节点"); 70 } 71 public function remove(CompanyBase $c) { 72 throw new Exception("该节点下无子节点"); 73 } 74 public function show($deep = 0) { 75 echo str_repeat("-", $deep) . $this->name; 76 echo "
"; 77 } 78 public function work($deep = 0) { 79 echo str_repeat(" ", $deep) . "人力资源部门的工作是为公司招聘人才"; 80 echo "
"; 81 } 82 } 83 /** 84 * 商务部门 85 */ 86 class Commerce extends CompanyBase { 87 public function add(CompanyBase $c) { 88 throw new Exception("该节点下不能增加节点"); 89 } 90 public function remove(CompanyBase $c) { 91 throw new Exception("该节点下无子节点"); 92 } 93 public function show($deep = 0) { 94 echo str_repeat("-", $deep) . $this->name; 95 echo "
"; 96 } 97 public function work($deep = 0) { 98 echo str_repeat(" ", $deep) . "商务部门的工作是为公司赚取利润"; 99 echo "
"; 100 } 101 } 102 //-------------------------测试代码------------------------ 103 $c = new Company("北京某科技公司"); 104 $h = new HumanResources("人力资源部门"); 105 $com = new Commerce("商务部门"); 106 $c->add($h); 107 $c->add($com); 108 //天津分公司 109 //为了偷懒,分公司的部门直接copy母公司的 110 $c1 = new Company("天津分公司"); 111 $c1->add($h); 112 $c1->add($com); 113 $c->add($c1); 114 //武汉分公司 115 $c2 = new Company("武汉分公司"); 116 $c2->add($h); 117 $c2->add($com); 118 $c->add($c2); 119 //使用公司功能 120 $c->show(); 121 $c->work();

 

组合模式 - 设计模式 - PHP版

标签:php   增加节点   资源   人力资源   人才   isset   row   turn   科技   

原文地址:http://www.cnblogs.com/benben7466/p/7736566.html


评论


亲,登录后才可以留言!