Java design Pattern _ combined mode

Source: Internet
Author: User


/** *  */package com.wangbiao.design.composite;import java.util.arraylist;import  java.util.List;/** *  @Title: component.java *  @Package   com.wangbiao.design.composite *  @Description: todo  *  @author  wangbiao     *  @date  2014-9-28  Morning 9:50:26  *  @version  v1.0 * /public abstract class component {    string name =  Null;        public component (String name)  {         this.name = name;    }         public abstract void add (Component component);     public abstract void remove (component component);     public abstract void display ();} Class reaf extends component{        public reaf (String  name)  {        super (name);    }      @Override     public void add (component component)  {         // not applicable for Reaf         system.out.println ("Not applicable for reaf");     }     @Override     public void remove (Component  component)  {        // not applicable for  reaf        system.out.println ("Not applicable for  reaf ");    }     @Override     public  Void display () &NBSP;{&NBSp;       system.out.println ("reaf: " + name);     }}class Composite extends Component{    private List< Component> list = new arraylist<component> ();         public composite (String name)  {         Super (name);    }     @Override     public void  add (component component)  {        list.add (Component);     }     @Override     public void remove (component component)  {        list.remove (Component);     }     @Override     public void display ()  {        for  (component component : list)  {             if (Component instanceof composite) {                 system.out.println (" xxxxxxxxxxxxxxcompositexxxxxxxxxxxxxx :  "+component.name);             }else{                 system.out.println ("xxxxxxxxxxxxxxreafxxxxxxxxxxxxxx : " +component.name);             }             component.display ();         }             }    }/** *   */package com.wangbiao.design.composite;/** *  @Title: client.java *  @Package   com.wangbiao.design.composite *  @Description: todo  *  @author  wangbiao     *  @date  2014-9-28  Morning 10:04:16  *  @version  V1.0  */public class client {    /**     *  @param  args     */    public static void main ( String[] args)  {                 component component_one = new composite ("compositexxxxxxxx");         component_one.add (New reaf ("Reaf_one"));         component_one.add (New reaf ("Reaf_two"));                &nBsp Component component_two = new composite ("Xxxxxxxxcomposite");         component_two.add (New reaf ("Reaf_three"));         component_two.add (New reaf ("Reaf_four"));                 component_one.add (Component_two);                 component_one.display ();     }        /*     * result     xxxxxxxxxxxxxxreafxxxxxxxxxxxxxx : reaf_one    reaf:  reaf_one    xxxxxxxxxxxxxxReafxxxxxxxxxxxxxx : reaf_two     Reaf: reaf_two    xxxxxxxxxxxxxxCompositexxxxxxxxxxxxxx :  Xxxxxxxxcomposite    xxxxxxxxxxxxxxreafxxxxxxxxxxxxxx : reaf_three    reaf: reaf_three     xxxxxxxxxxxxxxreafxxxxxxxxxxxxxx : reaf_four    reaf: reaf_four     */}


Java design Pattern _ combined 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.