[设计模式]组合模式

组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。


意图

将对象组合成树形结构以表示”部分-整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。


实现

这边我们实现一个树状分支

这次我们先看下需要生成的树状log

I/System.out: 
I/System.out: ┗节点1
I/System.out:   ┣叶子1
I/System.out:   ┣叶子2
I/System.out: ┗节点2
I/System.out:   ┣叶子3
I/System.out: ┗节点3
I/System.out:   ┣叶子4
I/System.out:   ┣叶子5
I/System.out:   ┣叶子6
I/System.out: ┗节点4
I/System.out:   ┗节点5
I/System.out:     ┣叶子100
I/System.out:     ┣叶子101
I/System.out:   ┣叶子7
I/System.out:   ┣叶子8
I/System.out:   ┣叶子9
I/System.out: ┣叶子10
I/System.out: ┣叶子11

这边我们先按正常的思路来,就是先创建一个根、在创建节点、在创建叶子。最后实现一颗树,添加之后再遍历打印就好了。

    public class Root{
        private List mList = new ArrayList();
        private String name;

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

        public void add(Branch branch){
            mList.add(branch);
        }

        public void add(Leaf leaf){
            mList.add(leaf);
        }

        public void show(){
            System.out.println(name);
        }

        public List getList(){
            return mList;
        }
    }

    public class Branch {
        private List mList = new ArrayList();
        private String name;

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

        public void add(Branch branch){
            mList.add(branch);
        }

        public void add(Leaf leaf){
            mList.add(leaf);
        }

        public void show(){
            System.out.println("┗"+name);
        }

        public List getList(){
            return mList;
        }
    }

    public class Leaf{
        private String name;

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

        public void show(){
            System.out.println("┣"+name);
        }
    }


    public void main(String... args){
        Root root;
        Branch branch;
        Branch branch2;

        root = new Root("根");

        branch = new Branch("节点1");
        branch.add(new Leaf("叶子1"));
        branch.add(new Leaf("叶子2"));
        root.add(branch);
        branch = new Branch("节点2");
        branch.add(new Leaf("叶子3"));
        root.add(branch);
        branch = new Branch("节点3");
        branch.add(new Leaf("叶子4"));
        branch.add(new Leaf("叶子5"));
        branch.add(new Leaf("叶子6"));
        root.add(branch);
        branch = new Branch("节点4");
        branch2 = new Branch("节点5");
        branch2.add(new Leaf("叶子100"));
        branch2.add(new Leaf("叶子101"));
        branch.add(branch2);
        branch.add(new Leaf("叶子7"));
        branch.add(new Leaf("叶子8"));
        branch.add(new Leaf("叶子9"));
        root.add(branch);

        root.add(new Leaf("叶子10"));
        root.add(new Leaf("叶子11"));

        root.show();
        showAll(root.getList(),"");
    }

    private void showAll(List list,String str) {
        int len = list.size();
        for (int i = 0; i < len; i++) {
            Object o = list.get(i);
            System.out.print(str);
            if (o instanceof Leaf){
                Leaf leaf = (Leaf) o;
                leaf.show();
            } else{
                Branch branch = (Branch) o;
                branch.show();
                showAll(branch.getList(),str+"  ");
            }
        }
    }

然后我们尝试使用组合模式来做。就是将全部根、节点、叶子都用同一个抽象类来实现,把拥有的相同属性整合,在实现。

  • 创建一个抽象类
    public abstract class Corp{
        protected List mList = new ArrayList();
        protected String name;

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

        public void add(Corp corp){
            mList.add(corp);
        }

        public List getList(){
            return mList;
        }

        public abstract void show();
    }
  • 抽象类扩展
    public class Root extends Corp{
        public Root(String name) {
            super(name);
        }

        @Override
        public void show() {
            System.out.println(name);
        }
    }

    public class Branch extends Corp{
        public Branch(String name) {
            super(name);
        }

        @Override
        public void show(){
            System.out.println("┗"+name);
        }
    }

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

        @Override
        public void show() {
            System.out.println("┣"+name);
        }
    }
  • 使用
    public void main(String... args){
        Root root;
        Branch branch;
        Branch branch2;

        root = new Root("根");

        branch = new Branch("节点1");
        branch.add(new Leaf("叶子1"));
        branch.add(new Leaf("叶子2"));
        root.add(branch);
        branch = new Branch("节点2");
        branch.add(new Leaf("叶子3"));
        root.add(branch);
        branch = new Branch("节点3");
        branch.add(new Leaf("叶子4"));
        branch.add(new Leaf("叶子5"));
        branch.add(new Leaf("叶子6"));
        root.add(branch);
        branch = new Branch("节点4");
        branch2 = new Branch("节点5");
        branch2.add(new Leaf("叶子100"));
        branch2.add(new Leaf("叶子101"));
        branch.add(branch2);
        branch.add(new Leaf("叶子7"));
        branch.add(new Leaf("叶子8"));
        branch.add(new Leaf("叶子9"));
        root.add(branch);

        root.add(new Leaf("叶子10"));
        root.add(new Leaf("叶子11"));

        root.show();
        showAll(root,"");
    }

    private void showAll(Corp root,String str) {
        List<Corp> corps = root.getList();
        for (Corp corp : corps) {
            System.out.print(str);
            if (corp instanceof Leaf){
                Leaf leaf = (Leaf) corp;
                leaf.show();
            } else{
                Branch branch = (Branch) corp;
                branch.show();
                showAll(corp,str+"  ");
            }
        }
    }

效果还是和上面的一样!


资料

菜鸟教程

JAVA设计模式之禅

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值