/** * */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