Java design pattern-compositing mode

Source: Internet
Author: User

Compositing mode: The composition mode represents the relationship of the part and the whole with the tree structure. The compositing mode allows the client to equate a single constituent object with a composite object made up of them.

Two forms: according to the implementation of the interface is divided into safe and transparent type

The composition mode can not provide a management method for the parent object, but the compositing mode must provide the child object management method where appropriate.

Safe type

The safe synthesis mode requires that the method of managing aggregation only appears in the tree component class, not in the leaf component class.

Role

Abstract component role: This is an abstract role that defines the common interface and default behavior for the object that participates in the composition, and can be used to manage all child objects. A composition object typically considers the child object it contains as an object of type component.

Leaf component role: A Leaf object is an object that has no subordinate child objects and defines the behavior of the original object that participates in the composition.

Branch Component role: Represents an object that participates in a group of subordinate child objects. The branch component class gives a way to manage sub-objects.

Component

 Package Com.design.composite.safe;  Public Interface Component {    Composite getcomposite ();     void operation ();}

Composite

 PackageCom.design.composite.safe;Importjava.util.ArrayList;Importjava.util.List; Public classCompositeImplementsComponent {Privatelist<component> list =NewArraylist<>(); @Override PublicComposite Getcomposite () {return  This; }    /*** A Business method*/@Override Public voidoperation () {//by yourself         for(Component c:list) {System.out.println (c.tostring ()); }    }     Public voidAdd (Component Component) {list.add (Component); }     Public voidRemove (Component Component) {list.remove (Component); }     PublicList components () {returnlist; }}

Leaf

 PackageCom.design.composite.safe; Public classLeafImplementsComponent {/*** Return to your own instance *@return     */@Override PublicComposite Getcomposite () {return NULL; }    /*** Business Method*/@Override Public voidoperation () {}}

Transparent type

Structure diagram

Unlike security, the transparent synthesis mode requires that all component classes, whether branches or leaves, conform to a fixed interface.

Role

Abstract component role: He assigns an interface to the object that participates in the composition, and regulates the common interface and default behavior. This interface can manage all child objects. There are methods like Add,remove.

Leaf component role: Represents the leaf object that participates in the composition, defining the behavior of the original object that participates in the composition. The realization of the mediocrity of the Add,remove method is given.

Branch Component role: Represents the object that participates in the composition of a child object, defining the behavior of such an object.

Component

 Package com.design.composite.transparent; Import java.util.List;  Public Interface Component {    Composite getcomposite ();     void operation ();     void Add (Component Component);     void Remove (Component Component);    List components ();}

Composite

 Packagecom.design.composite.transparent;Importjava.util.ArrayList;Importjava.util.List; Public classCompositeImplementsComponent {Privatelist<component> list =NewArraylist<>(); @Override PublicComposite Getcomposite () {return  This; } @Override Public voidoperation () { for(Component c:list) {System.out.println (c.tostring ()); }} @Override Public voidAdd (Component Component) {list.add (Component); } @Override Public voidRemove (Component Component) {list.remove (Component); } @Override PublicList components () {returnlist; }}

Leaf

 Packagecom.design.composite.transparent;Importjava.util.List; Public classLeafImplementsComponent {@Override PublicComposite Getcomposite () {return NULL; } @Override Public voidoperation () {} @Override Public voidAdd (Component Component) {} @Override Public voidRemove (Component Component) {} @Override PublicList components () {return NULL; }}

A drawing instance

Picture is a branch, line, Circle, Trangle is a leaf. The safe compositing mode used by this example.

Graphics

Package Com.design.composite.graphics;public interface Graphics {    void Draw ();}

Line

Package Com.design.composite.graphics;public class Line implements graphics {    private String color;    Public line (String color) {        This.color = color;    }    @Override public    Void Draw () {        System.out.println ("Draw line-" + color);}    }

  

Circle

Package Com.design.composite.graphics;public class Circle implements graphics {    private String color;    Public Circle (String color) {        This.color = color;    }    @Override public    Void Draw () {        System.out.println ("Draw Circle-" + color);}    }

  

Trangle

Package Com.design.composite.graphics;public class Trangle implements graphics {    private String color;    Public Trangle (String color) {        This.color = color;    }    @Override public    Void Draw () {        System.out.println ("Draw triangle-" + color);}    }

  

Picture

Package Com.design.composite.graphics;import Java.util.arraylist;import Java.util.list;public class picture Implements Graphics {    private list<graphics> List = new arraylist<> ();    @Override public    Void Draw () {        System.out.println ("picture contains ...");        for (Graphics g:list) {            g.draw ();        }        System.out.println ("All of the Above");    }    public void Add (Graphics g) {        list.add (g);    }    public void Remove (Graphics g) {        list.remove (g);    }    Public Graphics getchild (int i) {        return list.get (i);}    }

  

Test

Package Com.design.composite.graphics;public class Client {public    static void Main (string[] args) {line line        = n EW Line ("white");        Circle circle = New Circle ("Blue");        Trangle trangle = new Trangle ("Red");        Picture picture = new picture ();        Picture.add (line);        Picture.add (circle);        Picture.add (trangle);        Picture.draw ();        System.out.println ("---------------");        Graphics g = picture.getchild (0);        G.draw ();    }}

  

Results:

Java design pattern-compositing mode

Related Article

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.