Design mode: Compositing mode

Source: Internet
Author: User

Original address: http://leihuang.org/2014/12/09/composite/


Structural mode how to design the static structure between objects, how to complete the inheritance, implementation and dependency between objects, which is related to the system design is robust (robust): such as easy to understand, easy to maintain, easy to modify, low coupling and other issues. As the name of the Structural model, the pattern under its classification gives a variety of object relationship structures that can be applied in different situations.

    • Default Adapter Mode
    • Adapter mode
    • Bridge mode
    • Composite mode
    • Decorator mode
    • Facade mode
    • Flyweight mode
    • Proxy mode

Composite mode is one of the structural design patterns, which constructs a tree-like object structure, such as a file system, by recursive means; the data structure represented by the composite pattern is a collection of objects with a unified interface interface, and all objects (traversal) can be accessed through an object.

For example, a tree consists of branches and leaves, branches and leaves on top of it, so that it keeps circulating ... Such as

If you make a tree, will you define every branch? That's not going to happen. Then it is necessary to use the combination mode. The basic structure of the composite mode is as follows.

We are now using the combination pattern to achieve this tree.

Ibranch interface

Public interface Ibranch {public    void print ();

Leaf category

public class Leaf implements Ibranch {    private String leaf = null;    Public leaf (String leaf) {        this.leaf = leaf;    }    @Override public    void print () {        System.out.println (leaf);    }}

Branchcomposite class

Import Java.util.linkedlist;import Java.util.list;public class Branchcomposite implements Ibranch {    private List <IBranch> branchlist = null;    Public Branchcomposite () {        branchlist = new linkedlist<ibranch> ();    }    public void Add (Ibranch branch) {        branchlist.add (branch);    }    public boolean remove (Ibranch branch) {        return Branchlist.remove (branch);    }    @Override public    void print () {for        (Ibranch branch:branchlist) {            branch.print ();    }}}

Client class

public class Client {public    static void Main (string[] args) {        ibranch leaf1 = new Leaf ("leaf 1");        Ibranch leaf2 = new Leaf ("Leaf 2");        Ibranch leaf3 = new Leaf ("Leaf 3");        Branchcomposite branch0 = new Branchcomposite ();        Branchcomposite branch1 = new Branchcomposite ();        Branchcomposite branch2 = new Branchcomposite ();        Branch0.add (LEAF1);        Branch0.add (BRANCH1);        Branch1.add (LEAF2);        Branch1.add (BRANCH2);        Branch2.add (LEAF3);        Branch0.print ();        Branch1.print ();        Branch2.print ();    }}

2014-12-09 17:09:18

Brave,happy,thanksgiving!


Design mode: Compositing mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.