大话设计模式-组合模式

大话设计模式-组合模式_第1张图片

abstract class Component {
	protected $name;

	function __construct($name) {
		$this->name = $name;
	}

	public abstract function add(Component $c);
	public abstract function remove(Component $c);
	public abstract function display($depth);
}

//叶节点没有子节点
class Leaf extends Component {
	function __construct($name) {
		parent::__construct($name);
	}

	//由于叶子没有再增加分枝和树叶,所以add和remove方法实现它没有意义,但这样做可以消除叶节点和枝节点对象在抽象层次的区别,他们具备完全一致的接口
	public function add(Component $c) {
		echo 'Cannot add to a leaf';
	}

	public function remove(Component $c) {
		echo 'Cannot remove from a leaf';
	}

	public function display($depth) {
		//叶节点的具体方法,此处是显示其名称和级别
		echo str_pad('', $depth, '-').$this->name.'<br/>';
	}
}

//枝节点
class Composite extends Component {
	//一个子对象集合用来存储其下属的枝节点和叶节点
	private $children;

	function __construct($name) {
		parent::__construct($name);
	}

	public function add(Component $c) {
		$this->children[] = $c;
	}

	public function remove(Component $c) {
		unset($this->children[array_search($c, $this->children)]);
	}

	public function display($depth) {
		echo  str_pad('', $depth, '-').$this->name.'<br/>';
		foreach($this->children as $component) {
			$component->display($depth + 2);
		}
	}
}

//生成树根root,根上长出LeafA和leafB
$root = new Composite('root');
$root->add(new Leaf('Leaf A'));
$root->add(new Leaf('Leaf B'));

//根上长出分枝Composite X,分枝上也有两叶 LeafXA和LeafXB
$comp = new Composite('Composite X');
$comp->add(new Leaf("Leaf XA"));
$comp->add(new Leaf("Leaf XB"));

$root->add($comp);

//在Composite X上再长出分枝CompositeXY,分枝上也有两叶leafXYA和leafXYB
$comp2 = new Composite('Composite XY');
$comp2->add(new Leaf('Leaf XYA'));
$comp2->add(new Leaf('Leaf XYB'));

$comp->add($comp2);

$root->add(new Leaf('Leaf C'));

//根部长出两叶LeafC和LeafD,可惜LeafD没长牢,被风吹走了
$leaf = new Leaf('Leaf D');
$root->add($leaf);
$root->remove($leaf);

//显示大树的样子
$root->display(1);

大话设计模式-组合模式_第2张图片

//公司类
abstract class Company {
	protected $name;

	function __construct($name) {
		$this->name = $name;
	}

	//增加
	public abstract function add(Company $c);
	//移除
	public abstract function remove(Company $c);
	//显示
	public abstract function display($depth);
	//履行职责
	public abstract function lineOfDuty();
}

//具体公司类 实现接口 树枝节点
class ConcreteCompany extends Company {
	private $children;

	function __construct($name) {
		parent::__construct($name);
	}

	public function add(Company $c) {
		$this->children[] = $c;
	}

	public function remove(Company $c) {
		unset($this->children[array_search($c, $this->children)]);
	}

	public function display($depth) {
		echo  str_pad('', $depth, '-').$this->name.'<br/>';
		foreach($this->children as $component) {
			$component->display($depth + 2);
		}
	}

	//履行职责
	public function lineOfDuty() {
		foreach($this->children as $component) {
			$component->lineOfDuty();
		}
	}
}

//人力资源部
class HRDepartment extends Company {
	function __construct($name) {
		parent::__construct($name);
	}

	public function add(Company $c) {
	}

	public function remove(Company $c) {
	}

	public function display($depth) {
		echo str_pad('', $depth, '-').$this->name.'<br/>';
	}

	public function lineOfDuty() {
		echo $this->name.'员工招聘培训管理<br/>';
	}
}

//财务部
class FinanceDepartment extends Company {
	function __construct($name) {
		parent::__construct($name);
	}

	public function add(Company $c) {
	}

	public function remove(Company $c) {
	}

	public function display($depth) {
		echo str_pad('', $depth, '-').$this->name.'<br/>';
	}

	public function lineOfDuty() {
		echo $this->name.'公司财务收支管理<br/>';
	}
}


$root = new ConcreteCompany('北京总公司');
$root->add(new HRDepartment('总公司人力资源部'));
$root->add(new FinanceDepartment('总公司财务部'));

$comp = new ConcreteCompany('上海华东分公司');
$comp->add(new HRDepartment('华东分公司人力资源部'));
$comp->add(new FinanceDepartment('华东分公司财务部'));
$root->add($comp);

$comp1 = new ConcreteCompany('南京办事处');
$comp1->add(new HRDepartment('南京办事处人力资源部'));
$comp1->add(new FinanceDepartment('南京办事处财务部'));
$comp->add($comp1);

$comp2 = new ConcreteCompany('杭州办事处');
$comp2->add(new HRDepartment('杭州办事处人力资源部'));
$comp2->add(new FinanceDepartment('杭州办事处财务部'));
$comp->add($comp2);

echo '结构图:<br/>';
$root->display(1);

echo '职责:<br/>';
$root->lineOfDuty();

你可能感兴趣的:(设计模式,c,function,search,存储,Class)