[keywords]:java,design pattern, design pattern, Java and Schema learning, composite, synthetic mode
[Environment]:staruml5.0 + JDK6
[Author]:winty (wintys@gmail.com) http://www.blogjava.net/wintys/
[Body]:
Transparent synthesis mode
Package pattern.composite.transparent;
Import java.util.*; /** * Transparent synthesis mode: Composite pattern * @version 2009-6-3 * @author winty (wintys@gmail.com)/public class Transparentcompo
sitetest{public static void Main (string[] args) {Graphics G1, G2;
Graphics P1, p2;
G1 = new Line ();
G2 = new Circle ();
P1 = new picture ();
P2 = new picture ();
G1.draw ();
G2.draw ();
System.out.println ("");
P1.add (G1);
P1.add (G2);
P1.draw ();
System.out.println ("");
P2.add (G1);
P2.add (G2);
P2.add (p1);
P2.draw ();
}/** * Abstract component role/interface graphics{void Draw ();
void Add (Graphics g);
void Remove (int i);
Graphics getchild (int i); /** * Leaf Role */class line implements graphics{@Override public void Draw () {System.out.println ("dra
W a line "); @Override public void Add (Graphics g) {} @OverridE public void Remove (int i) {} @Override public Graphics getchild (int i) {return null; }/** * Leaf role */class Circle implements graphics{@Override public void Draw () {System.out.println (
"Draw a Circle");
@Override public void Add (Graphics g) {} @Override public void remove (int i) {} @Override
Public Graphics getchild (int i) {return null;
}/** * Composite role/class picture implements graphics{private list<graphics> Graphics;
Public picture () {graphics = new arraylist<graphics> ();
@Override public void Draw () {iterator<graphics> it = graphics.iterator ();
while (It.hasnext ()) {Graphics g = it.next ();
G.draw ();
@Override public void Add (Graphics g) {Graphics.add (g);
@Override public void Remove (int i) {graphics.remove (i); } @OvErride public Graphics getchild (int i) {return graphics.get (i); }
}
Run Result:
Draw a line
Draw a Circle
Draw a line
Draw a Circle
Draw a line
Draw a Circle
Draw a line
Draw a Circle