码迷,mamicode.com
首页 > Web开发 > 详细

PHP设计模式之组合模式

时间:2014-05-26 02:49:49      阅读:382      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   c   code   java   

组合模式适应于当我们的一个对象可能代表一个单一的实体,或者一个组合实体,但是仍然需要通过同样的方式被使用的情形

组合和聚合都描述了一个类长期持有其他类的一个或多个实例的情况。

聚合:被包含对象是容器的核心部分,但是他们也可以被其他对象所包含。聚合关系用一条以空心菱形开头的线来说明。

组合:被包含的对象只能被它的容器所引用。当容器被删除时,它也应该被删除。组合关系的菱形是实心的。

bubuko.com,布布扣
<?php
/**
*File name: WorkUnit.php
*Created by: ShowJoy
*Contact: 734578348@qq.com, xubiao522@gmail.com
*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>";
    }
}
?
bubuko.com,布布扣
bubuko.com,布布扣
<?php
/**
*File name: compsite.php
*Created by: ShowJoy
*Contact: 734578348@qq.com, xubiao522@gmail.com
*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); ?>
bubuko.com,布布扣

 

PHP设计模式之组合模式,布布扣,bubuko.com

PHP设计模式之组合模式

标签:style   class   blog   c   code   java   

原文地址:http://www.cnblogs.com/ShowJoy/p/3749315.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!