Composite_Pattern

本文深入探讨了软件设计中的组合模式,通过实例代码展示了如何构建树形结构来表示部分整体层次结构,使得客户能够一致地使用单个对象和组合对象。文章解释了透明模式和安全模式的区别,并提供了实际应用案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package composite_pattern;

public abstract class Component {
    protected String name;

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

    public abstract void add(Component c);

    public abstract void remove(Component c);

    public abstract void display(int depth);

    public abstract void setStructure(char tag, int depth);
}

package composite_pattern;

import java.util.ArrayList;

public class Composite extends Component {
    private ArrayList<Component> children = new ArrayList<Component>();

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

    @Override
    public void add(Component c) {
        children.add(c);
    }

    @Override
    public void remove(Component c) {
        children.remove(c);
    }

    @Override
    public void setStructure(char tag, int depth) {
        for (int i = 0; i < depth; i++) {
            System.out.print(tag);
        }
    }

    @Override
    public void display(int depth) {
        setStructure('-', depth);
        System.out.println(name);
        for (Component c : children) {
            c.display(depth + 2);
        }
    }
}

package composite_pattern;

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

    @Override
    public void add(Component c) {
        System.out.println("Can not add to a leaf.");
    }

    @Override
    public void remove(Component c) {
        System.out.println("Can not remove from a leaf.");
    }

    @Override
    public void setStructure(char tag, int depth) {
        for (int i = 0; i < depth; i++) {
            System.out.print(tag);
        }
    }

    @Override
    public void display(int depth) {
        setStructure('-', depth);
        System.out.println(name);
    }
}

package composite_pattern;

public class Main {
    public static void main(String args[]) {
        Composite root = new Composite("root");
        root.add(new Leaf("Leaf A"));
        root.add(new Leaf("Leaf B"));

        Composite comp = new Composite("Composite X");
        comp.add(new Leaf("Leaf XA"));
        comp.add(new Leaf("Leaf XB"));

        root.add(comp);

        Composite comp2 = new Composite("Composite XY");
        comp2.add(new Leaf("Leaf XYA"));
        comp2.add(new Leaf("Leaf XYB"));

        comp.add(comp2);

        root.add(new Leaf("Leaf C"));

        Leaf leaf = new Leaf("Leaf D");
        root.add(leaf);
        root.remove(leaf);
        root.display(1);
    }
}
/*
 * The composite_pattern is just like the structure of "Tree". The Component is
 * the abstraction of Leaf and Composite. The Composite can own its children but
 * the Leaf not.This is the "transparent mode".
 * But there is a question:why do we implement the methods of "add" and "remove" if
 * we actually don't need it in "Leaf" ?It's ridiculous!But on the other hand,it does
 * make the client more convenient so that they can omit the step of distinguishing
 * whether the current node is the leaf or not.
 * when we remove the method of "add" and "remove" in the Component,it is called
 * the "safe mode".How to choose them?It depends on the specific situation.
 * 
 */

This is a general introduction to the 23 design patterns:
https://blog.youkuaiyun.com/GZHarryAnonymous/article/details/81567214

See more source code:[GZHarryAnonymous](https://github.com/GZHarryAnonymous/Design_Pattern)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值