长沙网站建设网站推广微信营销,网站建设可以抵扣吗,互联网公司排名2024,企业信息查询系统官网贵州//20210121写在前面#xff1a;刚期末考试完#xff0c;考了面向对象#xff0c;里边儿有23个设计模式#xff0c;我寻思着考完挨个儿实现一下#xff0c;本文实现组合模式组合模式核心思想类似文件夹的概念#xff0c;构件树形结构#xff0c;树形有叶子结点和文件夹结…//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 componentTransparentsArraylist new ArrayList();Overridepublic void add(ComponentTransparent componentTransparent) {this.componentTransparentsArraylist.add(componentTransparent);}Overridepublic void remove(ComponentTransparent componentTransparent) {this.componentTransparentsArraylist.remove(componentTransparent);}Overridepublic void display(int depth) {//输出树形结构for (int i 0;iSystem.out.print(-);}System.out.println(this.name);//下级遍历for(ComponentTransparent componentTransparent:this.componentTransparentsArraylist){componentTransparent.display(depth1);}}}//叶子节点实现子类public class LeafTransparent extends ComponentTransparent{public LeafTransparent(String name){super(name);}Overridepublic void add(ComponentTransparent componentTransparent) {//空实现抛出不支持请求异常throw new UnsupportedOperationException();}Overridepublic void remove(ComponentTransparent componentTransparent) {throw new UnsupportedOperationException();}Overridepublic void display(int depth) {//输出树形结构的叶子节点for (int i 0;iSystem.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 componentSaftyArrayList new ArrayList();public void add(ComponentSafty component){this.componentSaftyArrayList.add(component);}public void remove(ComponentSafty componentSafty){this.componentSaftyArrayList.remove(componentSafty);}Overridepublic void display(int depth) {for (int i0;iSystem.out.print(-);}System.out.println(this.name);for (ComponentSafty componentSafty : componentSaftyArrayList) {componentSafty.display(depth1);}}}//叶子结点实现子类public class LeafSafty extends ComponentSafty{public LeafSafty(String name){super(name);}Overridepublic void display(int depth) {for (int i0;iSystem.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;i10;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;i10;i){System.out.print();}System.out.println();//展示root.display(1);}public static void main(String[] args) {System.out.println(透明模式);transparent();for(int i 0;i10;i){System.out.print();}System.out.println();System.out.println(安全模式);safty();}}输出如下到此这篇关于java实现Composite组合模式的文章就介绍到这了,更多相关java组合模式内容请搜索云海天教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持云海天教程