//20210121 写在前面:刚期末考试完,考了面向对象,里边儿有23个设计模式,我寻思着考完挨个儿实现一下,本文实现组合模式 组合模式核心思想类似文件夹的概念,构件树形结构,树
//20210121
写在前面:刚期末考试完,考了面向对象,里边儿有23个设计模式,我寻思着考完挨个儿实现一下,本文实现组合模式
- 组合模式核心思想类似文件夹的概念,构件树形结构,树形有叶子结点和文件夹结点,文件夹结点可以包含叶子结点和文件夹结点
- 分为两种模式
- 透明型:所有节点构造全部相同,但是由于叶子结点没有下层结点,所以其有些方法为空,会不安全
- 安全型:叶子结点和文件架节点构造不同,这样展示的时候需要判断节点属性,不方便调用,但是由于没有空方法,会很安全
透明型组合模式程序源代码:
//节点抽象父类 /** * 透明模式就是把组合使用的方法放到抽象类中,不管叶子对象还是数值对象都有相同的结构 * 这样做的好处就是叶子结点和树枝结点对于外界没有区别,他们具备完全一致的行为接口 */ public abstract class ComponentTransparent { protected String name; public ComponentTransparent(String name){ this.name = name; } //增加一个叶子构件或者树枝构件 public abstract void add(ComponentTransparent componentTransparent); //删除 public abstract void remove(ComponentTransparent componentTransparent); //获取分支下的所有叶子构件和树枝构件 public abstract void display(int depth); }
//文件架节点实现子类 import java.util.ArrayList; public class CompositeTransparent extends ComponentTransparent{ public CompositeTransparent(String name){ super(name); } //构建容器 private ArrayList<ComponentTransparent> componentTransparentsArraylist= new ArrayList<>(); @Override public void add(ComponentTransparent componentTransparent) { this.componentTransparentsArraylist.add(componentTransparent); } @Override public void remove(ComponentTransparent componentTransparent) { this.componentTransparentsArraylist.remove(componentTransparent); } @Override public void display(int depth) { //输出树形结构 for (int i = 0;i<depth;++i){ System.out.print("-"); } System.out.println(this.name); //下级遍历 for(ComponentTransparent componentTransparent:this.componentTransparentsArraylist){ componentTransparent.display(depth+1); } } }
//叶子节点实现子类 public class LeafTransparent extends ComponentTransparent{ public LeafTransparent(String name){ super(name); } @Override public void add(ComponentTransparent componentTransparent) { //空实现,抛出"不支持请求"异常 throw new UnsupportedOperationException(); } @Override public void remove(ComponentTransparent componentTransparent) { throw new UnsupportedOperationException(); } @Override public void display(int depth) { //输出树形结构的叶子节点 for (int i = 0;i<depth;++i){ System.out.print("-"); } System.out.println(this.name); } }
安全型组合模式源代码:安全型中,叶子结点没有增加移除方法,方法需要自己实现,而不会在父类中指出
//节点抽象父类 public abstract class ComponentSafty { protected String name; public ComponentSafty(String name){ this.name = name; } //展示 public abstract void display(int depth); }
//文件夹节点实现子类 import java.util.ArrayList; public class CompositeSafty extends ComponentSafty{ public CompositeSafty(String name){ super(name); } private ArrayList<ComponentSafty> componentSaftyArrayList = new ArrayList<>(); public void add(ComponentSafty component){ this.componentSaftyArrayList.add(component); } public void remove(ComponentSafty componentSafty){ this.componentSaftyArrayList.remove(componentSafty); } @Override public void display(int depth) { for (int i=0;i<depth;++i){ System.out.print("-"); } System.out.println(this.name); for (ComponentSafty componentSafty : componentSaftyArrayList) { componentSafty.display(depth+1); } } }
//叶子结点实现子类 public class LeafSafty extends ComponentSafty{ public LeafSafty(String name){ super(name); } @Override public void display(int depth) { for (int i=0;i<depth;++i){ System.out.print("-"); } System.out.println(this.name); } }
测试主类程序源代码
//测试主类 public class Main { private static void transparent(){ //创建根节点以及其子节点 ComponentTransparent root = new CompositeTransparent("root"); root.add(new LeafTransparent("Leaf A")); root.add(new LeafTransparent("Leaf B")); //创建第二层结点及其子节点 ComponentTransparent branch = new CompositeTransparent("Composite X"); branch.add(new LeafTransparent("Leaf XA")); branch.add(new LeafTransparent("Leaf XB")); root.add(branch); //创建第三层节点及其子结点 ComponentTransparent branch2 = new CompositeTransparent("Composite XY"); branch2.add(new LeafTransparent("Leaf XYA")); branch2.add(new LeafTransparent("Leaf XYB")); branch.add(branch2); //创建第二层结点 root.add(new LeafTransparent("Leaf C")); //常见第二层节点并删除 ComponentTransparent leaf = new LeafTransparent("Leaf D"); root.add(leaf); root.display(1); root.remove(leaf); for(int i =0;i<10;++i){ System.out.print("="); } System.out.println(); //展示 root.display(1); } private static void safty(){ //创建根节点以及其子节点 CompositeSafty root = new CompositeSafty("root"); root.add(new LeafSafty("Leaf A")); root.add(new LeafSafty("Leaf B")); //创建第二层结点及其子节点 CompositeSafty branch = new CompositeSafty("Composite X"); branch.add(new LeafSafty("Leaf XA")); branch.add(new LeafSafty("Leaf XB")); root.add(branch); //创建第三层节点及其子结点 CompositeSafty branch2 = new CompositeSafty("Composite XY"); branch2.add(new LeafSafty("Leaf XYA")); branch2.add(new LeafSafty("Leaf XYB")); branch.add(branch2); //创建第二层结点 root.add(new LeafSafty("Leaf C")); //常见第二层节点并删除 LeafSafty leaf = new LeafSafty("Leaf D"); root.add(leaf); root.display(1); root.remove(leaf); for(int i =0;i<10;++i){ System.out.print("="); } System.out.println(); //展示 root.display(1); } public static void main(String[] args) { System.out.println("透明模式:"); transparent(); for(int i =0;i<10;++i){ System.out.print("="); } System.out.println(); System.out.println("安全模式:"); safty(); } }
输出如下:
到此这篇关于java实现Composite组合模式的文章就介绍到这了,更多相关java组合模式内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!