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

低代码建站平台网站排名靠前的方法

低代码建站平台,网站排名靠前的方法,抖音关键词推广,建站吧网站建设组合模式是一种结构型设计模式,主要用来将多个对象组织成树形结构以表示“部分-整体”的层次结构,因此该模式也称为“部分-整体”模式。简言之,组合模式就是用来将一组对象组合成树状结构,并且能像使用独立对象一样使用它们。 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.jinmujx.cn/news/118176.html

相关文章:

  • 公司做网站都咨询哪些问题微信公众号运营
  • 普通网站备案it培训机构哪个好一点
  • 网站里的搜索怎么做如何制作自己的网址
  • 51网站空间相册廊坊百度推广seo
  • 做网站能赚到流量费吗佛山网络营销推广
  • 自己的网站做飘窗优化大师的作用
  • 响应式网站一般做几个尺寸谷歌浏览器网址
  • 新疆建设云网站新闻发稿发布平台
  • 天津市党的基层组织建设信息系统网站长沙网址seo
  • 条幅在线设计网站交换链接的其它叫法是
  • 公司主页网站制作100种找客户的方法
  • 网站内容优化方案成都网站建设
  • 用心做的网站有名的seo外包公司
  • 网站创建想法衡阳seo优化
  • 如何打造网站企业营销推广
  • 上海专业做网站价格google高级搜索
  • 如何做自己的网站商城站热门职业培训班
  • 百度浏览器官网seo外包杭州
  • 温州做外贸网站设计谷歌aso优化
  • 动态网站如何打开免费域名申请个人网站
  • 刷赞网站推广软件济南百度
  • wordpress编辑器下载北京谷歌seo
  • 公司网站开发软件代运营电商公司排行榜
  • 学生保险网站百度推广运营怎么做
  • seo优化销售seo关键词排名优化怎样收费
  • 购物网站建设与开发互联网去哪里学
  • 广州手机网站建设报价电商seo优化
  • 7个优秀网站设计赏析阿里巴巴官网首页
  • 金华竞价排名 金华企业网站建设什么是搜索引擎优化的核心
  • 做网站时无法上传图片关键词优化的策略