PHP設計模式之組合模式

來源:互聯網
上載者:User

標籤:style   class   blog   c   code   java   

組合模式適應於當我們的一個對象可能代表一個單一的實體,或者一個組合實體,但是仍然需要通過同樣的方式被使用的情形

組合和彙總都描述了一個類長期持有其他類的一個或多個執行個體的情況。

彙總:被包含對象是容器的核心部分,但是他們也可以被其他對象所包含。彙總關係用一條以空心菱形開頭的線來說明。

組合:被包含的對象只能被它的容器所引用。當容器被刪除時,它也應該被刪除。組合關係的菱形是實心的。

<?php/***File name: WorkUnit.php*Created by: ShowJoy*Contact: [email protected], [email protected]*Last modified: 2014-5-24*/abstract class WorkUnit{    protected $tasks = array();    protected $name = NULL;        function __construct($name){        $this->name = $name;    }        function getName(){        return $this->name;    }        abstract function add(Employee $e);    abstract function remove(Employee $e);    abstract function assignTask($task);    abstract function completeTask($task);}class Team extends WorkUnit{    private $_employees = array();        function add(Employee $e){        $this->_employees[] = $e;        echo "<p>{$e->getName()} has been added to team {$this->getName()}.</p>";    }        function remove(Employee $e){        $index = array_search($e, $this->_employees);        unset($this->_employees[$index]);        echo "<p>{$e->getName()} has been removed from team {$this->getName()}.</p>";    }        function assignTask($task){        $this->tasks[] = $task;        echo "<p>A new task has been assigned to team {$this->getName()}. It should be easy to do with {$this->getCount()} team member(s).</p>";    }        function completeTask($task){        $index = array_search($task, $this->tasks);        unset($this->tasks[$index]);        echo "<p>The ‘$task‘ task has been completed by team {$this->getName()}.</p>";    }        function getCount(){        return count($this->_employees);            }}class Employee extends WorkUnit{    function add(Employee $e){        return false;    }        function remove(Employee  $e){        return false;    }        function assignTask($task){        $this->tasks[] = $task;        echo "<p>A new task has been assigned to {$this->getName()}. It will be done by {$this->getName()} alone.</p>";    }        function completeTask($task){        $index =  array_search($task, $this->tasks);        unset($this->tasks[$index]);        echo "<p>The ‘$task‘ task has been completed by employee {$this->getName()}.</p>";    }}?
<?php/***File name: compsite.php*Created by: ShowJoy*Contact: [email protected], xubiao522[email protected]*Last modified: 2014-5-24*/require ‘WorkUnit.php‘;$team = new Team(‘showjoy‘);$e1 = new Employee(‘xu‘);$e2 = new Employee(‘biao‘);$e3 = new Employee(‘god‘);$team->add($e1);$team->add($e2);$team->add($e3);$team->assignTask(‘do something by team‘);$e1->assignTask(‘do it alone‘);$team->completeTask(‘do something by team‘);
$e1->completeTask(‘do it alone‘);
unset($team,$e1,$e2,$e3); ?>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.