前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >组合模式

组合模式

作者头像
tanoak
发布2019-03-20 09:45:01
4720
发布2019-03-20 09:45:01
举报
文章被收录于专栏:java闲聊java闲聊
  • 概述
  • UML类图
  • 代码栗子
  • 总结
  1. 概述
    • 概念 组合模式是指将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性。
    • 作用:让客户端不再区分操作的是组合对象还是叶子对象,而是以一个统一的方式来操作。
  2. UML类图

image.png

  1. 代码栗子
    • 栗子 公司与部门、文件与文件夹、
    • code public interface ICorp { /** * 获取员工信息 * @return */ String getInfo() ; } //叶子节点 public interface ILeaf extends ICorp{ /** * 获取员工信息 * @return */ @Override String getInfo() ; } //树枝 public interface IBranch extends ICorp { /** * 增加员工(树叶节点)或者是经理(树枝节点) * @param corp */ void addSubordinate(ICorp corp); /** * 获得下属的信息 * @return */ ArrayList<ICorp> getSubordinate(); } public class Branch implements IBranch{ /** * 名称 */ private String name; /** * 职位 */ private String position; /** * 薪水 */ private int salary; ArrayList<ICorp> subordinateList = new ArrayList<>(); public Branch(String name,String position,int salary){ this.name = name; this.position = position; this.salary = salary; } /** * 增加一个下属,可能是小头目,也可能是个小兵 * @param corp */ @Override public void addSubordinate(ICorp corp) { this.subordinateList.add(corp); } /** * 我有哪些下属 * @return */ @Override public ArrayList<ICorp> getSubordinate() { return this.subordinateList; } /** * 领导也是人,他也有信息 * @return */ @Override public String getInfo() { String info = ""; info = "姓名:" + this.name; info = info + "\t职位:"+ this.position; info = info + "\t薪水:" + this.salary; return info; } } public class Leaf implements ILeaf{ /** * 名称 */ private String name; /** * 职位 */ private String position; /** * 薪水 */ private int salary ; public Leaf(String name,String position,int salary){ this.name = name; this.position = position; this.salary = salary; } /** * 获得小兵的信息 * @return */ @Override public String getInfo() { String info = ""; info = "姓名:" + this.name; info = info + "\t职位:"+ this.position; info = info + "\t薪水:" + this.salary; return info; } }
      • 测试 public class Client { /** * ceo */ private static Branch ceo; /** * 开发经理 */ private static Branch developDep; /** * 财务经理 */ private static Branch financeDep; static { //初始化组织结构 ceo = new Branch("马总", "总经理", 100000); developDep = new Branch("张三", "研发部门经理", 10000); financeDep = new Branch("里斯", "财务部经理", 20000); Leaf a = new Leaf("a", "开发人员", 2000); Leaf b = new Leaf("b", "开发人员", 2000); Leaf c = new Leaf("c", "开发人员", 2000); Leaf d = new Leaf("d", "财务人员", 4000); Leaf e = new Leaf("e", "财务人员", 4000); Leaf f = new Leaf("f", "财务人员", 4000); ceo.addSubordinate(developDep); ceo.addSubordinate(financeDep); developDep.addSubordinate(a); developDep.addSubordinate(b); developDep.addSubordinate(c); financeDep.addSubordinate(d); financeDep.addSubordinate(e); financeDep.addSubordinate(f); } public static void main(String[] args) { // //首先把CEO的信息打印出来 System.out.println(ceo.getInfo()); //然后是所有员工信息 System.out.println(getTreeInfo(ceo)); } /** * 遍历整棵树,只要给我根节点,我就能遍历出所有的节点 * @param root * @return */ public static String getTreeInfo(Branch root) { ArrayList<ICorp> subordinateList = root.getSubordinate(); String info = ""; for (ICorp s : subordinateList) { /** * 是员工就直接获得信息 */ if (s instanceof Leaf) { info = info + s.getInfo() + "\n"; } else { //是个小头目 info = info + s.getInfo() + "\n" + getTreeInfo((Branch) s); } } return info; } }
  2. 总结
    • 场景 当遇到想表示树形结构时(如菜单栏 等),优先考虑组合模式
    • 缺点 安全性和透明性是互相矛盾的,这是由于叶子节点和非叶子节点行为的不一致以及需要提供一个一致的行为接口所造成的,是不可调和的矛盾
    • 实际中,组合模式的大多数使用场景可以通过表设计进行规范解决 ps: 这样就可以呈现出一个树形结构

参考资料

书籍:《设计模式之禅》

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.03.16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档