组合模式

作者: Stephenwish | 来源:发表于2020-07-28 14:29 被阅读0次
image.png
组合模式本质上是构造类似树形结构,一般用在后台管理系统的菜单
第一步,先创建一个抽象类
public abstract class Component {
    protected String name;

    public Component(String name) {
        this.name = name;
    }

    public  abstract void doSomthing();
}
实现抽象类实例,并且里面放置容器(装载自己实现的抽象父类)
public class Composite extends Component{
    public Composite(String name) {
        super(name);
    }

    @Override
    public void doSomthing() {
        System.err.println("CCCC");
    }


    protected List<Component> components =new ArrayList<>();



    public List<Component> getComponents() {
        return components;
    }

    public void add(Component component){
        components.add(component);
    }

    public void delete(Component component){
        components.remove(component);
    }
}
客户端验证场景,通过客户端要用到的具体类,再具体实现(Branch,Root,Leaf)其中printNode 是用来打印节点信息
public class Client {
    public static void main(String[] args) {
        Composite root = new Root("root");
        Composite leaf = new Leaf("leaf");
        Composite branch1 = new Branch("branch1");
        Composite branch2 = new Branch("branch2");
        root.add(branch1);
        root.add(branch2);
        branch1.add(leaf);
        printNode(root,0);
    }


    private static void printNode(Composite component,int depth){
        for (int i = 0; i < depth; i++) {
            System.err.print("--");
        }

        System.err.println(component.name);
        for (Component entity : component.getComponents()) {
            if (entity instanceof Branch) {
                printNode(((Composite) entity),depth+1);

            }else {
                printNode(((Composite) entity),depth+1);
            }
        }

    }
}

具体实现类
public class Root extends  Composite{

    public Root(String name) {
        super(name);
    }

    @Override
    public void doSomthing() {
      //  System.err.println("Root" + name);
    }

    @Override
    public String toString() {
        return "Root{" +
                "name='" + name + '\'' +
                '}';
    }
}

public class Leaf extends Composite{
    public Leaf(String name) {
        super(name);
    }

    @Override
    public void doSomthing() {
      //  System.err.println("Leaf_" + name);
    }

    @Override
    public String toString() {
        return "Leaf{" +
                "name='" + name + '\'' +
                '}';
    }
}


public class Branch extends  Composite{

    public Branch(String name) {
        super(name);
    }

    public List<Component> getComponents() {
        return components;
    }

    public void add(Component component){
        components.add(component);
    }

    public void delete(Component component){
        components.remove(component);
    }

    @Override
    public void doSomthing() {
       // System.err.println("Branch_"+name);
    }

    @Override
    public String toString() {
        return "Branch{" +
                "name='" + name + '\'' +
                '}';
    }
}

相关文章

  • 设计模式:组合模式 职责链模式

    组合模式 职责链模式 组合模式 组合模式将对象组合成树形结构,以表示“部分-整体”的层次结构。 在组合模式的树形结...

  • 第4章 结构型模式-组合模式

    一、组合模式简介 二、组合模式的优缺点 三、组合模式的使用场景 、组合模式的实例

  • 组合模式(统一叶子与组合对象)

    目录 从生活场景出发,映射组合模式 组合模式的理论概念 组合模式的实现 组合模式在源码中的应用 组合 “优于” 继...

  • 组合模式

    1. 组合模式 1.1 组合模式的定义 组合模式(Composite): 又称部分-整体模式, 将对象组合成树形结...

  • 组合模式

    设计模式系列7--组合模式 《Objective-c 编程之道 iOS 设计模式解析》 - 组合模式 常见组合模式...

  • 设计模式 | 组合模式及典型应用

    本文的主要内容: 介绍组合模式 示例 组合模式总结 源码分析组合模式的典型应用java.awt中的组合模式Java...

  • 组合模式

    一、组合模式介绍 二、组合模式代码实例

  • 组合模式

    设计模式之组合模式 什么是组合模式? 组合模式允许你将对象组合成树形结构来表现”部分-整体“的层次结构,使得客户以...

  • 15、组合模式(Composite Pattern)

    1. 组合模式 1.1 简介   Composite模式,即组合模式,又叫部分整体模式。Composite模式将对...

  • 组合模式原型解析

    组合模式定义: 组合模式,将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象...

网友评论

    本文标题:组合模式

    本文链接:https://www.haomeiwen.com/subject/ceyfrktx.html