A brief talk on the Java design pattern--combined mode (Composite)

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/45458081
I. Overview

Combines objects into a tree structure to represent a "partial-whole" hierarchy. "Composite makes the user consistent with the use of individual objects and composite objects.

Second, applicability

1. You want to represent the part of the object-the overall hierarchy.

2. You want users to ignore the difference between a combined object and a single object, and the user will use all the objects in the composite structure uniformly.

Third, participants

1.Component declares an interface for an object in a group. When appropriate, implements the default behavior for all classes of common interfaces. Declares an interface for accessing and managing component subcomponents. Optionally, define an interface in the recursive structure to access a parent part and implement it where appropriate.

2.Leaf represents a leaf node object in a group, and the leaf node has no child nodes. Defines the behavior of a node object in a composition.

3.Composite defines the behavior of those parts that have child parts. Stores the child parts. Implement operations related to subassemblies in the component interface.

4.Client an object that manipulates a combined part via the component interface.

Four, class diagram

V. Examples

Component
Package com.lyz.design.composite;import java.util.list;/** * Definition Component Class Employer * @author Liuyazhuang * */public abstr Act class Employer {    private String name;    public void SetName (String name) {        this.name = name;    }    Public String GetName () {        return this.name;    }    public abstract void Add (employer employer);        public abstract void Delete (employer employer);        public list<employer> employers;        public void Printinfo () {        System.out.println (name);    }        Public list<employer> getemployers () {        return this.employers;    }}

Leaf

Package com.lyz.design.composite;/** * Defines leaf class Programmer * @author Liuyazhuang * */public class Programmer extends Employe R {public    Programmer (String name) {        setName (name);        Employers = null;//Programmer, indicates no subordinate,    } public    void Add (Employer employer) {    } public    void Delete (employer Employer) {    }}

Package com.lyz.design.composite;/** * Defines leaf class projectassistant * @author Liuyazhuang * */public class Projectassistant ex Tends employer {public    projectassistant (String name) {        setName (name);        Employers = null;//Project assistant, indicating that there is no subordinate, public    void Add (Employer employer) {            } public    void Delete ( Employer employer) {            }}

Composite
Package Com.lyz.design.composite;import java.util.arraylist;/** * Define composite Class Projectmanager class * @author Liuyazhuang * * /public class Projectmanager extends Employer {public    Projectmanager (String name) {        setName (name);        Employers = new arraylist<employer> ();    }        public void Add (Employer employer) {        employers.add (employer);    }    public void Delete (Employer employer) {        employers.remove (employer);}    }

Client
Package Com.lyz.design.composite;import java.util.list;/** * Testing class * @author Liuyazhuang * */public class Test {public    static void Main (string[] args) {        employer PM = new Projectmanager ("Project manager");        Employer PA = new Projectassistant ("Project Assistant");        Employer Programmer1 = new Programmer ("Programmer One");        Employer Programmer2 = new Programmer ("Programmer II");                Pm.add (PA);//Add Project Assistant Pm.add (PROGRAMMER2) for the project manager,        //Add programmer                list<employer> EMS = pm.getemployers        for the project manager; for (employer Em:ems) {            System.out.println (Em.getname ());}}}    

result
Project Assistant Programmer II


A brief talk on the Java design pattern--combined mode (Composite)

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.