组合模式(Composite)

将对象建立为部分-整体的层次关系或者构造树的数据表现。

 

 

Component (DrawingElement)

            声明组合对象接口

            显现接口默认操作。

            声明访问并管理子组件的接口

            (可选)定义访问父组件接口。

LeafPrimitiveElement

            表示叶子对象,叶子没有孩子。

            实现叶子对象操作。

CompositeCompositeElement

            定义有孩子组件的行为

            保存孩子组件

            实现孩子组件相关操作。

ClientCompositeApp

            通过组件接口操作组合模式的对象。

 

代码:

 

//Component

public abstract class DrawingElement{

            protected String name;

 

            public DrawingElement(String name){

                        this.name=name;

            }

           

            public abstract void Add(DrawingElement d);

            public abstract void Remove(DrawingElement d);

            public abstract void Display(int indent);

}

 

//Leaf

public class PrimitiveElement extends DrawingElement{

            public PrimitiveElement(String name){

                        super(name)

            }

 

            public void Add(DrawingElement c){

                        System.out.println(“Cannot add”);

            }

 

            public void Remove(DrawingElement c){

                        System.out.println(“Cannot remove”);

            }

           

            public void Display(int indent){

                        System.out.println(“draw a indent”+name);

            }

           

}

 

//Composite

import java.util.ArrayList;

 

public class CompositeElement extends DrawingElement{

            private ArrayList elements =new ArrayList();

 

            public CompositeElement(String name){

                        super(name);

            }

 

            public void Add(DrawingElement d){

                        elements.add(d);

            }

           

            public void Remove(DrawingElement d){

                        elements.Remove(d);

            }

 

            public void Display(int indent){

                        System.out.println(“draw a indent”+name);

                        for(int i=0; i<elements.size();i++){

                                    (DrawingElement)(elements.get(i)).Display(indent+2);

           

            }         

}

 

public class CompositeApp{

            public static void main(String[] args){

                        CompositeElement root=new CompositeElement(“picture”);

                        root.Add(new PrimitiveElement(“red line”));

                        root.Add(new PrimitiveElement(“blue line”));

                        root.Add(new PrimitiveElement(“green line”));

                        CompositeElement comp=new CompositeElement(“tow ciricles”);

comp.Add(new PrimitiveElement(“black circle”);

comp.Add(new PrimitiveElement(“white circle”);

                        root.Add(comp);

                        PrimitiveElement 1=new PrimitiveElement(“yello line”);

                        root.Add(l);

                        root.Remove(l);

                        root.Display(l);

            }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值