今天看了看
深入PHP++面向对象、模式与实践_第三版
其中看到“聚合”跟“组合”的概念,对此理解也比较模糊
文中是这样说的:
理解上面文本中的话,我个人理解是成这样:
首先是聚合
<?php
/**
* Created by PhpStorm.
* ClassName: AggregationA
* User: JAN
* Date: 14-9-12
* Time: 下午12:18
* Link: http://blog.segmentfault.com/janhuang
*/
namespace DesignPattern\SimpleExample\Aggregation;
class AggregationA
{
protected $aggregation = array();
public function __construct()
{
array_push($this->aggregation, new AggregationB());
array_push($this->aggregation, new AggregationC());
}
public function addAggregation(AggregationInterface $aggregation)
{
array_push($this->aggregation, $aggregation);
return $this;
}
public function getAggregationList()
{
return $this->aggregation;
}
}
<?php
/**
* Created by PhpStorm.
* ClassName: AggregationB
* User: JAN
* Date: 14-9-12
* Time: 下午12:18
* Link: http://blog.segmentfault.com/janhuang
*/
namespace DesignPattern\SimpleExample\Aggregation;
class AggregationB implements AggregationInterface
{
public function getName()
{
return $this;
}
}
<?php
/**
* Created by PhpStorm.
* ClassName: AggregationC
* User: JAN
* Date: 14-9-12
* Time: 下午2:34
* Link: http://blog.segmentfault.com/janhuang
*/
namespace DesignPattern\SimpleExample\Aggregation;
class AggregationC implements AggregationInterface
{
public function getName()
{
return $this;
}
}
<?php
/**
* Created by PhpStorm.
* ClassName: AggregationInterface
* User: JAN
* Date: 14-9-12
* Time: 下午2:38
* Link: http://blog.segmentfault.com/janhuang
*/
namespace DesignPattern\SimpleExample\Aggregation;
interface AggregationInterface
{
public function getName();
}
类内聚了两个类:分别是agregationB, agregationC 分别都实现了AgregationInterface, 而且在实力AgregationA的时候构造时实例化B和C。
再次是组合:
A类
<?php
/**
* Created by PhpStorm.
* ClassName: CompositionA
* User: JAN
* Date: 14-9-12
* Time: 下午12:25
* Link: http://blog.segmentfault.com/janhuang
*/
namespace DesignPattern\SimpleExample\Composition;
class CompositionA
{
private $compositionB = null;
private $compositionC = null;
public function setCompositionB(CompositionB $compositionB)
{
$this->compositionB = $compositionB;
return $this;
}
public function getCompositionB()
{
return $this->compositionB;
}
public function setCompositionC(CompositionC $compositionC)
{
$this->compositionC = $compositionC;
return $this;
}
public function getCompositionC()
{
return $this->compositionC;
}
}
B类
<?php
/**
* Created by PhpStorm.
* ClassName: CompositionB
* User: JAN
* Date: 14-9-12
* Time: 下午12:25
* Link: http://blog.segmentfault.com/janhuang
*/
namespace DesignPattern\SimpleExample\Composition;
class CompositionB implements CompositionObjectInterface
{
public function getName()
{
return $this;
}
}
C类
<?php
/**
* Created by PhpStorm.
* ClassName: CompositionC
* User: JAN
* Date: 14-9-12
* Time: 下午2:42
* Link: http://blog.segmentfault.com/janhuang
*/
namespace DesignPattern\SimpleExample\Composition;
class CompositionC implements CompositionObjectInterface
{
public function getName()
{
return $this;
}
}
接口
<?php
/**
* Created by PhpStorm.
* ClassName: CompositionObjectInterface
* User: JAN
* Date: 14-9-12
* Time: 下午12:29
* Link: http://blog.segmentfault.com/janhuang
*/
namespace DesignPattern\SimpleExample\Composition;
interface CompositionObjectInterface
{
public function getName();
}
在实例化CompositionA 的时候并不会直接实例化其他类,这里只是清楚限定了CompositionB类作为参数类型传入setCompotionB方法,这里理解的组合就是:有提供组合的方法提供外界指定的类进行自由组合,并且使用组合当中的方法。
以上仅是个人的理解,不知道这对类的关系有没有帮助,最近在学习设计模式,概念有点模糊,还请各路英雄指点。感谢
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。