Java描述设计模式(10):组合模式
本文源码:
GitHub·点这里 ||
GitEE·点这里
一、生活场景1、文件系统下图是常见的计算机文件系统的一部分。 文件系统是一个树结构,树上长有节点。树的节点有两种:
2、打印文件树结构public class C01_InScene { public static void main(String[] args) { File file = new File("F:tree") ; fileTree(file,0); } private static void fileTree(File file,int floor) { // 判断是否存在 if (file.exists()) { if (floor > 0) { // 循环打空格 for (int i = 0; i < floor; i++) { System.out.print(" "); } } if (file.isDirectory()) { System.out.println("+" + file.getName()); // 列出所有文件及文件夹 File[] files = file.listFiles(); if (null != files) { // 循环递归 for (File dirFile : files) { fileTree(dirFile,floor + 1); } } } else { System.out.println("-" + file.getName()); } } } } 执行效果:+代表文件夹,-代表文件。 +tree +dir1 +dir2 -dir2Leaf.txt -leaf1.txt -leaf2.txt -OneLeaf.txt -TwoLeaf.txt 3、组合模式描述
二、组合模式-安全式1、基础概念
2、模式图解3、源代码实现public class C02_Security_Model { public static void main(String[] args) { Composite root = new Composite("服装"); Composite composite1 = new Composite("男装"); Leaf manCoat = new Leaf("上衣"); Leaf manBottom = new Leaf("下衣"); composite1.addChild(manCoat); composite1.addChild(manBottom); Composite composite2 = new Composite("女装"); Leaf leaf1 = new Leaf("鞋子"); Leaf leaf2 = new Leaf("帽子"); root.addChild(leaf1); root.addChild(leaf2); root.addChild(composite1); root.addChild(composite2); root.printStruct(""); } } // 抽象构件角色类 interface Component { /* * 输出组件自身的名称 */ void printStruct(String preStr); } // 树枝构件角色类 class Composite implements Component{ // 用来存储组合对象中包含的子组件对象 private List<Component> childComponents = new ArrayList<Component>(); // 输出对象的名称 private String name; // 构造方法,传入组合对象的名字 public Composite (String name){ this.name = name; } /** * 聚集管理方法,增加一个子构件对象 * @param child 子构件对象 */ public void addChild(Component child){ childComponents.add(child); } /** * 聚集管理方法,删除一个子构件对象 * @param index 子构件对象的下标 */ public void removeChild(int index){ childComponents.remove(index); } /** * 聚集管理方法,返回所有子构件对象 */ public List getChild(){ return childComponents ; } /** * 输出对象的自身结构 * @param preStr 前缀,主要是按照层级拼接空格,实现向后缩进 */ @Override public void printStruct(String preStr) { //先输出自己 System.out.println(preStr+"+"+this.name); //如果还包含有子组件,那么就输出这些子组件对象 if (this.childComponents != null){ //添加两个空格,表示向后缩进两个空格 preStr = preStr+" "; //输出当前的子对象:使用函数递归的原理 for (Component c : childComponents) { c.printStruct(preStr); } } } } class Leaf implements Component{ // 输出叶子对象的名称 private String name; // 构造方法,传入叶子对象的名称 public Leaf (String name){ this.name = name ; } /** * 输出叶子对象的结构,叶子对象没有子对象,也就是输出叶子对象的名字 * @param preStr 前缀,主要是按照层级拼接的空格,实现向后缩进 */ @Override public void printStruct(String preStr) { System.out.println(preStr+"-"+name); } }
三、组合模式-透明式1、概念图解
2、源代码实现public class C03_Transparent_Model { public static void main(String[] args) { Component1 root = new Composite1("服装"); Component1 c1 = new Composite1("男装"); Component1 c2 = new Composite1("女装"); Component1 leaf1 = new Leaf1("衬衫"); Component1 leaf2 = new Leaf1("夹克"); Component1 leaf3 = new Leaf1("裙子"); Component1 leaf4 = new Leaf1("套装"); root.addChild(c1); root.addChild(c2); c1.addChild(leaf1); c1.addChild(leaf2); c2.addChild(leaf3); c2.addChild(leaf4); root.printStruct(""); } } abstract class Component1 { /** * 输出组件自身的名称 */ public abstract void printStruct(String preStr); // 聚集管理方法,增加一个子构件对象 public void addChild(Component1 child){ /** * 缺省实现,抛出异常,因为叶子对象没有此功能 * 或者子组件没有实现这个功能 */ throw new UnsupportedOperationException("对象不支持此功能"); } // 聚集管理方法,删除一个子构件对象 public void removeChild(int index){ /** * 缺省实现,抛出异常,因为叶子对象没有此功能 * 或者子组件没有实现这个功能 */ throw new UnsupportedOperationException("对象不支持此功能"); } // 聚集管理方法,返回所有子构件对象 public List<Component1> getChild(){ /** * 缺省实现,抛出异常,因为叶子对象没有此功能 * 或者子组件没有实现这个功能 */ throw new UnsupportedOperationException("对象不支持此功能"); } } class Composite1 extends Component1 { // 用来存储组合对象中包含的子组件对象 private List<Component1> childComponents = new ArrayList<Component1>(); // 输出对象名称 private String name ; public Composite1 (String name){ this.name = name; } /** * 聚集管理方法,增加一个子构件对象 * @param child 子构件对象 */ public void addChild(Component1 child){ childComponents.add(child); } /** * 聚集管理方法,删除一个子构件对象 * @param index 子构件对象的下标 */ public void removeChild(int index){ childComponents.remove(index); } // 聚集管理方法,返回所有子构件对象 public List<Component1> getChild(){ return childComponents ; } /** * 输出对象的自身结构 * @param preStr 前缀,主要是按照层级拼接空格,实现向后缩进 */ @Override public void printStruct(String preStr) { // 首先输出自己名称 System.out.println(preStr+"+"+this.name); // 如果还包含有子组件,那么就输出这些子组件对象 preStr = preStr + " "; if (this.childComponents != null) { // 添加两个空格,表示向后缩进 for (Component1 c : childComponents) { ////递归输出每个子对象 c.printStruct(preStr); } } } } class Leaf1 extends Component1 { private String name; public Leaf1 (String name){ this.name = name; } /** * 输出叶子对象的结构,叶子对象没有子对象,也就是输出叶子对象的名字 * @param preStr 前缀,主要是按照层级拼接的空格,实现向后缩进 */ @Override public void printStruct(String preStr) { System.out.println(preStr+"-"+name); } } 四、JDK中应用1、HashMap结构图2、分层结构
3、源代码
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |