今天看了看
深入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方法,这里理解的组合就是:有提供组合的方法提供外界指定的类进行自由组合,并且使用组合当中的方法。
以上仅是个人的理解,不知道这对类的关系有没有帮助,最近在学习设计模式,概念有点模糊,还请各路英雄指点。感谢
原文链接:https://www.f2er.com/javaschema/285339.html