The second implementation method of PHP combination mode

黄舟
Release: 2023-03-06 16:36:02
Original
1141 people have browsed it

PHPCombined modeSecond implementation method

container as $c) $c->doAction(); } /** * add component * @param Component $c */ public function addComponent(Component $c) { if(!in_array($c, $this->container, true)) $this->container[] = $c; } /** * remove conponent * @param Component $c */ public function removeComponent(Component $c) { $this->container = array_diff($this->container, array($c)); } /** * get all components * @return array */ public function getComponents() { return $this->container; } } // test code $leaf = new Leaf(); $composite = new Composite(); $composite->addComponent($leaf); $composite->addComponent(new Leaf()); $composite->addComponent(new Leaf()); $composite->doAction(); /** * 总结: * 如果你想像对待单个对象一样对待组合对象,那么组合模式十分有用。可能是因为组合对象本质上和局部对象 * 相似,也可能因为在环境中组合对象和局部对象的特征相同。因为组合是树型结构,所以对整体的操作会影响 * 到局部,而通过组合,对客户端代码来说局部的数据又是透明的。组合模式使这些操作和查询对客户端代码透明。 * 对象树可以方便的进行遍历。你也可以轻松的在组合结构中加入新的组件类型。 * 但另一方面,组合模式又依赖于其组成部分的简单性。随着我们引入复杂的规则,代码会变得越来越难以维护。 * 组合模式不能很好的在关系型数据库中保存数据,但却非常适合使用 XML 持久化。 */
Copy after login

The above is the detailed content of The second implementation method of PHP combination mode. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!