当前位置: 首页 > news >正文

自助建站比较好的php更换wordpress用户头像

自助建站比较好的,php更换wordpress用户头像,wordpress打赏可见,电商项目流程组合模式是一种结构型设计模式,主要用来将多个对象组织成树形结构以表示“部分-整体”的层次结构,因此该模式也称为“部分-整体”模式。简言之,组合模式就是用来将一组对象组合成树状结构,并且能像使用独立对象一样使用它们。 Co…

组合模式是一种结构型设计模式,主要用来将多个对象组织成树形结构以表示“部分-整体”的层次结构,因此该模式也称为“部分-整体”模式。简言之,组合模式就是用来将一组对象组合成树状结构,并且能像使用独立对象一样使用它们。

Composite is a structural design pattern that lets you organize multiple objects into a tree structure to represent the "part whole" relationship. In short, the composite pattern can be used to combine a group of objects into a tree structure and use them as independent objects.  

结构设计

为实现组合模式,首先需要创建一个可以组合多个对象的单一对象(Component),这个对象用来访问和管理其子对象,并对外提供公共接口。然后,定义没有子节点的对象(Leaf,基本对象)和包含子对象的对象(Composite,组合对象)。最后,将这些对象组装到之前创建的对象上。这样,外部(Client)就可通过Component调用公共接口。组合模式包含如下角色:
Component,组合对象,为组合中的对象声明公共接口,并提供默认实现。
Leaf,叶节点对象,叶节点最终会完成大部分的实际工作,因为它们无法将工作指派给其他部分。
Compoiste,组合,也称容器,包含叶节点或其他容器的单位。容器不知道其子项目所属的具体类, 它只通过通用的组件接口与其子项目交互。
组合模式类图表示如下:
请添加图片描述
注意:
(1) 组合模式对基本对象和组合对象的使用具有一致性。外部代码调用Component公共接口时,无需区别对待基本对象和组合对象(透明性),大多数情况下可以一致地处理它们。

伪代码实现

接下来将使用代码介绍下组合模式的实现。

// 1、Component,组合对象,为组合中的对象声明公共接口,并提供默认实现。  
public abstract class Component {private String name;protected List<Component> children = new ArrayList<>();public Component(String componentName) {this.name = componentName;}public void operation() {System.out.println(this.name);}public Component getChild(String componentName) {for (Component current : children) {if (current.name.equals(componentName)) {return current;}Component childComponent = current.getChild(componentName);if (childComponent != null) {return childComponent;}}return null;}public abstract void add(Component component);public abstract void remove(Component component);
}
// 2、Compoiste,组合,也称容器,包含叶节点或其他容器的单位。容器不知道其子项目所属的具体类,
// 它只通过通用的组件接口与其子项目交互。  
public class Composite extends Component {public Composite(String componentName) {super(componentName);}@Overridepublic void add(Component component) {this.children.add(component);}@Overridepublic void remove(Component component) {this.children.remove(component);}
}
// 3、Leaf,叶节点对象,叶节点最终会完成大部分的实际工作,因为它们无法将工作指派给其他部分。  
public class Leaf extends Component {public Leaf(String componentName) {super(componentName);}@Overridepublic void add(Component component) {throw new RuntimeException("叶节点不能添加子节点");}@Overridepublic void remove(Component component) {throw new RuntimeException("叶节点不包含子节点,无法移除子节点");}
}
// 4、客户端调用
public class CompositeClient {public void test() {Component root = new Composite("root");root.add(new Leaf("Leaf A"));Composite branch = new Composite("Composite X");Leaf leafXa = new Leaf("Leaf XA");branch.add(leafXa);branch.add(new Leaf("Leaf XB"));branch.remove(leafXa);root.add(branch);Component leafXb = root.getChild("Leaf XB");leafXb.operation();}
}

这里只介绍了基于透明性的设计与实现,组合模式还支持一种基于安全性的设计与实现,更多安全性相关知识可以执行搜索并学习。

适用场景

在以下情况下可以考虑使用组合模式:
(1) 如果需要实现树状对象结构, 可以考虑使用组合模式。
组合模式提供了两种共享公共接口的基本元素类型:简单叶节点和复杂容器。容器中可以包含叶节点和其他容器。这使得开发者可以构建树状嵌套递归对象结构。
(2) 如果希望客户端代码以相同方式处理简单和复杂元素, 可以使用该模式。
组合模式中定义的所有元素共用同一个接口。在这一接口的帮助下,客户端不必在意其所使用的对象的具体类。

优缺点

组合模式最大特点是将多个对象组织成树形结构。组合模式有以下优点:
(1) 可以利用多态和递归机制更方便地使用复杂树结构。
(2) 符合开闭原则。无需更改现有代码,开发者就可以在应用中添加新元素,使其成为对象树的一部分。
但是组合模式也存在以下缺点:
(1) 对于功能差异较大的类, 提供公共接口或许会有困难。在特定情况下,开发者需要过度一般化组件接口,使其变得令人难以理解。

参考

《设计模式:可复用面向对象软件的基础》 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides 著 李英军, 马晓星 等译
https://refactoringguru.cn/design-patterns/composite 组合模式
https://www.cnblogs.com/adamjwh/p/9033547.html 简说设计模式——组合模式
https://blog.csdn.net/ShuSheng0007/article/details/116378002 秒懂设计模式之组合模式(Composite Pattern)
https://www.runoob.com/design-pattern/composite-pattern.html 组合模式

http://www.yayakq.cn/news/532473/

相关文章:

  • 大连企业制作网站自学seo能找到工作吗
  • 自贡市建设局网站网站建设与网页设计制作绿皮书
  • 网站链接跳转怎么做今天刚刚发生的新闻事故
  • 模板网站建设咨询wordpress站点是什么意思
  • 如何做内网站的宣传栏江苏省宿迁市建设局网站
  • 做网站哪家公司比较好而且不贵如何做产品销售网站
  • 网站alexa流量查询动漫制作专业专科
  • 西安网站建设云李北京seo包年
  • 广州做网站一般多少钱南京seo推广优化
  • 只做网站不做app三原网站建设
  • word页面设计模板seo优化设计
  • 免费做手机网站建设网站建设与管理自考题
  • 网站开发赚不赚钱软件设计与开发
  • 明薇通网站建设首选做网站用的编程工具
  • 如何学建设网站新赣州房产网
  • 做外贸soho 需要有网站吗视频网站调用
  • 可以做本地生活服务的有哪些网站w3c网站怎么做
  • 网站seo报价福建住房和城乡建设网站
  • 电脑iis做网站盐城做百度网站
  • 衡阳网站建设公司哪家好青岛网站建设大全
  • 微信网站开发登录做哪方面的网站好呢
  • 电商网站网络服务猪价大涨已成定局
  • 如何最便宜建设一个网站修改wordpress布局
  • ps做网站对齐技巧vue.js2.5 pc网站开发
  • 陕西省建设工程安全协会网站网站后台制作教程
  • 青岛开发网站做销售网站的好做么
  • 阿凡达网站设计wordpress摄影主题 lens
  • 网站公共模板是什么陕西交通建设集团西长分公司网站
  • vc6.0做网站浙江网站建设价位
  • 平面设计公司网站建设费用如何做seo网站