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

安徽动漫公司 网站制作 安徽网新dedecms 做门户网站

安徽动漫公司 网站制作 安徽网新,dedecms 做门户网站,wordpress 显示视频播放,应用公园app组合模式 描述基本使用所有节点方法一致使用 叶子无实现子节点使用 添加向上查询使用(没变化) 描述 组合模式用于描述部分与整体的关系,将个体对象与组合对象的行为统一,便于维护整个数据集。 基本使用 所有节点方法一致 定义…

组合模式

  • 描述
    • 基本使用
      • 所有节点方法一致
        • 使用
      • 叶子无实现子节点
        • 使用
    • 添加向上查询
        • 使用(没变化)

描述

组合模式用于描述部分与整体的关系,将个体对象与组合对象的行为统一,便于维护整个数据集。

基本使用

所有节点方法一致

  • 定义通用操作抽象组件
public abstract class AbstractComponent {private final String name;protected AbstractComponent(String name) {this.name = name;}public void description() {System.out.println("name: " + this.name);}public abstract void addItem(AbstractComponent child);public abstract void deleteItem(AbstractComponent child);public abstract List<AbstractComponent> getChildren();
}
  • 定义树枝节点
public class ComplexComponent extends AbstractComponent {private final List<AbstractComponent> children = new ArrayList<>();protected ComplexComponent(String name) {super(name);}public void addItem(AbstractComponent child) {children.add(child);}public void deleteItem(AbstractComponent child) {children.remove(child);}public List<AbstractComponent> getChildren() {return children;}
}
  • 定义叶子节点
public class LeafComponent extends AbstractComponent {protected LeafComponent(String name) {super(name);}@Overridepublic void addItem(AbstractComponent child) {}@Overridepublic void deleteItem(AbstractComponent child) {}@Overridepublic List<AbstractComponent> getChildren() {return null;}
}
使用

由于所有节点操作一致,在使用中无需强转

public class Sample {public static void main(String[] args) {AbstractComponent root = new ComplexComponent("L1");AbstractComponent l2 = new ComplexComponent("L2");root.addItem(l2);AbstractComponent l2_1 = new ComplexComponent("L2_1");AbstractComponent l2_2 = new ComplexComponent("L2_2");l2.addItem(l2_1);l2.addItem(l2_2);AbstractComponent l2_1_1 = new ComplexComponent("L2_1_1");l2_1.addItem(l2_1_1);AbstractComponent l2_1_2 = new ComplexComponent("L2_1_2");l2_1.addItem(l2_1_2);printComponent(root);}private static void printComponent(AbstractComponent root) {root.description();root.getChildren().forEach(component -> {if (component instanceof ComplexComponent) {printComponent(component);} else {component.description();}});}
}

叶子无实现子节点

  • 定义通用操作的抽象组件
public abstract class AbstractComponent {private final String name;protected AbstractComponent(String name) {this.name = name;}public void description() {System.out.println("name: " + this.name);}
}
  • 定义树枝节点
public class ComplexComponent extends AbstractComponent {private final List<AbstractComponent> children = new ArrayList<>();protected ComplexComponent(String name) {super(name);}public void addItem(AbstractComponent child) {children.add(child);}public void deleteItem(AbstractComponent child) {children.remove(child);}public List<AbstractComponent> getChildren() {return children;}
}
  • 定义叶子节点
public class LeafComponent extends AbstractComponent {protected LeafComponent(String name) {super(name);}
}
使用
public class Sample {public static void main(String[] args) {ComplexComponent root = new ComplexComponent("L1");ComplexComponent l2 = new ComplexComponent("L2");root.addItem(l2);ComplexComponent l2_1 = new ComplexComponent("L2_1");ComplexComponent l2_2 = new ComplexComponent("L2_2");l2.addItem(l2_1);l2.addItem(l2_2);ComplexComponent l2_1_1 = new ComplexComponent("L2_1_1");l2_1.addItem(l2_1_1);ComplexComponent l2_1_2 = new ComplexComponent("L2_1_2");l2_1.addItem(l2_1_2);printComponent(root);}private static void printComponent(ComplexComponent root) {root.description();root.getChildren().forEach(component -> {if (component instanceof ComplexComponent) {printComponent((ComplexComponent) component);} else {component.description();}});}
}

添加向上查询

在有的场景中,需要支持向上查询。
可以在通用抽象中定义一个上级节点,然后在父节点添加子节点同时为子节点关联父节点

  • 定义通用抽象组件
public abstract class AbstractComponent {private final String name;/*** 新增了一个父级成员*/private AbstractComponent parent;protected AbstractComponent(String name) {this.name = name;}public void description() {System.out.println("name: " + this.name + "  parent: " + (Objects.isNull(getParent()) ? "null" : getParent().getName()));}public abstract void addItem(AbstractComponent child);public abstract void deleteItem(AbstractComponent child);public abstract List<AbstractComponent> getChildren();public void setParent(AbstractComponent parent) {this.parent = parent;}public AbstractComponent getParent() {return parent;}public String getName() {return name;}
}
  • 定义树枝节点
public class ComplexComponent extends AbstractComponent {private final List<AbstractComponent> children = new ArrayList<>();protected ComplexComponent(String name) {super(name);}/*** 在添加子节点时 同时设置子节点的父级节点* @param child*/public void addItem(AbstractComponent child) {children.add(child);child.setParent(this);}/*** 在移除子节点时 同时清空子节点的父级节点* @param child*/public void deleteItem(AbstractComponent child) {children.remove(child);child.setParent(null);}public List<AbstractComponent> getChildren() {return children;}
}
  • 定义叶子节点(没变化)
public class LeafComponent extends AbstractComponent {protected LeafComponent(String name) {super(name);}@Overridepublic void addItem(AbstractComponent child) {}@Overridepublic void deleteItem(AbstractComponent child) {}@Overridepublic List<AbstractComponent> getChildren() {return null;}
}
使用(没变化)
public class Sample {public static void main(String[] args) {AbstractComponent root = new ComplexComponent("L1");AbstractComponent l2 = new ComplexComponent("L2");root.addItem(l2);AbstractComponent l2_1 = new ComplexComponent("L2_1");AbstractComponent l2_2 = new ComplexComponent("L2_2");l2.addItem(l2_1);l2.addItem(l2_2);AbstractComponent l2_1_1 = new ComplexComponent("L2_1_1");l2_1.addItem(l2_1_1);AbstractComponent l2_1_2 = new ComplexComponent("L2_1_2");l2_1.addItem(l2_1_2);printComponent(root);}private static void printComponent(AbstractComponent root) {root.description();root.getChildren().forEach(component -> {if (component instanceof ComplexComponent) {printComponent(component);} else {component.description();}});}
}

文章转载自:

http://x7Cb2vCn.fdxhk.cn
http://qmc4QjqB.fdxhk.cn
http://imbgvEel.fdxhk.cn
http://0lsY3eJN.fdxhk.cn
http://hhjlKhom.fdxhk.cn
http://pLPLNPiD.fdxhk.cn
http://EDGA7szS.fdxhk.cn
http://ixNXrpUR.fdxhk.cn
http://zAR55cl7.fdxhk.cn
http://89L5XoSZ.fdxhk.cn
http://LtMegTTF.fdxhk.cn
http://7d7Ts8KL.fdxhk.cn
http://6L5PUDIp.fdxhk.cn
http://ghdH6qTN.fdxhk.cn
http://3jyPRCJ4.fdxhk.cn
http://2rj3rQgP.fdxhk.cn
http://dsODstNe.fdxhk.cn
http://alwXmtUn.fdxhk.cn
http://rJIgyfcJ.fdxhk.cn
http://tK2pqL1t.fdxhk.cn
http://6463jbr5.fdxhk.cn
http://VN0K68Lg.fdxhk.cn
http://65Lj5TkR.fdxhk.cn
http://b0yIw8n7.fdxhk.cn
http://47wodpE8.fdxhk.cn
http://sPTELnTv.fdxhk.cn
http://FMOwTrdN.fdxhk.cn
http://2U26MJdK.fdxhk.cn
http://5mzZ8ENP.fdxhk.cn
http://eUyLeyT8.fdxhk.cn
http://www.dtcms.com/wzjs/737620.html

相关文章:

  • 平面设计素材网站知乎wordpress 图片展示页面
  • 做图片网站赚钱吗苏州网络推广商
  • 网站导航栏 字体视频拍摄收费标准
  • 阆中网站网站建设建设网站公司塞尼铁克
  • 校园网站制作方法wordpress 地区插件
  • 一个做炉石视频的网站河南网站建设哪家好
  • 庄河建网站商城网站开发定制
  • 常德经济技术开发区徐州seo外包平台
  • 海南海口府城网站开发建网站费用明细
  • 南昌装修网站建设电子商务专业就业方向及前景
  • 做地方网站收益怎么样电子商务网站建设与维护考试
  • 深圳建设集团网站首页华夏星光工业设计公司
  • 一起做单网站怎么样企业年金查询官网
  • wordpress网站转app插件下载聊城的网站制作公司
  • 网站建设布为网wordpress文章分类权限
  • 自定义网站主页设计wordpress收不到邮箱
  • 做网站域名有什么用网站的版权信息
  • 为什么实验楼网站上做实验这么卡asp 网站发布器
  • 网站开发工作要求个人网站尺寸
  • 企业网站推广名词解释增城新闻头条今天
  • 中国建设银行网站下载智慧团建登录官网手机版
  • 天津做网站认准津坤科技瀑布流响应式网站模板
  • 建站公司服务费包括哪些义乌比较好的外贸公司
  • 网站排名提高湖南网站建设公司 要上磐石网络
  • 婚纱影楼网站模板wordpress 时间线插件
  • 网站建设教论坛买域名后怎么做网站
  • 企业建设电子商务网站的预期收益2017网站风格
  • 大连哪家公司做网站比较好中国桥梁建设网站
  • 网站咨询弹窗是怎么做的班级优化大师头像
  • 企业网站的优化方案域名注册后怎么使用