java composite_java实现Composite组合模式的实例代码

//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<>();

@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

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

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 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

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

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();

}

}

输出如下:

jq2smnnmnyd.jpg

到此这篇关于java实现Composite组合模式的文章就介绍到这了,更多相关java组合模式内容请搜索聚米学院以前的文章或继续浏览下面的相关文章希望大家以后多多支持聚米学院!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值