Composite 设计模式

转自:Composite 设计模式

定义

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

结构



示例

Component:组件中的对象声明接口。在适当的情况下,实现所有类共有接口的默认行为。声明一个接口用于访问和管理 Component 的子组件。

1

2

3

4

5

6

7

8

9

10

11


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

}




Leaf:表示叶子节点对象。叶子节点没有子节点。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21


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 display() {      

        // print super.name

    }

}



Composite:定义枝节点行为,用来存储子组件。在 Component 接口中实现与子组件相关的操作。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26


class Composite extends Component {

    

    private List<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 display(int depth) {

        for (Component c : children) {

            c.display();

        }

    }

}




Client:通过 Component 接口操作结构中的对象

1

2

3

4

5

6

7

8

9

10

11

12

13

14


public class CompositePattern {

    

    public static void main(String[] args) {

        Composite root = new Composite("root");

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


        Composite comX = new Composite("comX");

        comX.add(new Leaf("Leaf Xa");

        comX.add(new Leaf("Leaf Xb");


        root.add(comX);

        root.display();

    }

}



样例

1

2

3

4

5

6

7

8

9

10


abstract class MessageUnit {

    protected String json;


    public MessageUnit(String json) {

        this.json = json;

    }


    public abstract void add(MessageUnit unit);

    public abstract String toJSONString();

}




定义子组件和枝组件








































































































class MessageHeader extends MessageUnit {

    public MessageHeader(String json) {

        super(json);

    }


    @Override

    public void add(MessageUnit unit) {

        // Nothing to do

    }


    @Override

    public String toJSONString() {

        return json;

    }

}


class MessageBody extends MessageUnit {

    public MessageBody(String json) {

        super(json);

    }


    @Override

    public void add(MessageUnit unit) {

        // Nothing to do

    }


    @Override

    public String toJSONString() {

        return json;

    }

}


class Message extends MessageUnit {


    private List<MessageUnit> messageUnits = new ArrayList<MessageUnit>();


    public Message(String json) {

        super(json);

    }


    @Override

    public void add(MessageUnit unit) {

        this.messageUnits.add(unit);

    }


    @Override

    public String toJSONString() {

        List<Object> messages = new ArrayList<Object>();


        for (MessageUnit messageUnit : messageUnits) {

            String json = messageUnit.toJSONString();

            Object obj = JSONObject.parseObject(json, MessageProtocol.class);


            messages.add(obj)

        }

        return JSON.toJSONString(messages);

    }

}

Client

1

2

3

4

5

6

7

8

9

10

11


public class Client {

   

    public static void main(Stirng[] args) {

        Message message = new Message("{...}");

        message.add(new MessageHeader("{...}"));

        message.add(new MessageBody("{...}"));


        String json = body.toJSONString();

        System.out.println("json :"   json);

    }

}


转载请并标注: “本文转载自 linkedkeeper.com (文/张松然)”