对象的结构模式,又称“部分-整体”模式。
合成模式将对象组织到树结构中,可以用来描述整体与部分的关系。合成模式可以使客户端将单纯元素与复合元素同等对待。
适用场合:
1. 需要描述对象的部分和整体的等级结构。
2. 需要客户端忽略掉个体构件和组合构件的区别。客户端必须平等对待所有的构件,包括个体构件和组合构件。
优点:
1. 合成模式可以很容易地增加新种类的构件。
2. 使用合成模式可以使客户端变得很容易设计,因为客户端不需要知道构件是树叶构件还是树枝构件。
缺点:
1. 使用合成模式后,控制树枝构件的类型就不太容易。
2. 用继承的方法来增加新的行为很困难。
透明式的合成模式:
1. com.cleversoft.designpatterns.transparentcomposite.Graphics.java:
package
com.cleversoft.designpatterns.transparentcomposite;
abstract public class Graphics {
public abstract void draw();
public abstract void add(Graphics g);
public abstract void remove(Graphics g);
public abstract Graphics getChild( int i);
}
abstract public class Graphics {
public abstract void draw();
public abstract void add(Graphics g);
public abstract void remove(Graphics g);
public abstract Graphics getChild( int i);
}
2. com.cleversoft.designpatterns.transparentcomposite.Circle.java:
package
com.cleversoft.designpatterns.transparentcomposite;
public class Circle extends Graphics {
public void draw() {
System.out.println("Drawing Circle");
}
public void add(Graphics g) {
// do nothing
}
public void remove(Graphics g) {
// do nothing
}
public Graphics getChild(int i) {
return null;
}
}
public class Circle extends Graphics {
public void draw() {
System.out.println("Drawing Circle");
}
public void add(Graphics g) {
// do nothing
}
public void remove(Graphics g) {
// do nothing
}
public Graphics getChild(int i) {
return null;
}
}
3. com.cleversoft.designpatterns.transparentcomposite.Line.java:
package
com.cleversoft.designpatterns.transparentcomposite;
public class Line extends Graphics {
public void draw() {
System.out.println("Draw Line");
}
public void add(Graphics g) {
// do nothing
}
public void remove(Graphics g) {
// do nothing
}
public Graphics getChild(int i) {
return null;
}
}
public class Line extends Graphics {
public void draw() {
System.out.println("Draw Line");
}
public void add(Graphics g) {
// do nothing
}
public void remove(Graphics g) {
// do nothing
}
public Graphics getChild(int i) {
return null;
}
}
4. com.cleversoft.designpatterns.transparentcomposite.Rectangle.java:
package
com.cleversoft.designpatterns.transparentcomposite;
public class Rectangle extends Graphics {
public void draw() {
System.out.println("Drawing Rectangle");
}
public void add(Graphics g) {
// do nothing
}
public void remove(Graphics g) {
// do nothing
}
public Graphics getChild(int i) {
return null;
}
}
public class Rectangle extends Graphics {
public void draw() {
System.out.println("Drawing Rectangle");
}
public void add(Graphics g) {
// do nothing
}
public void remove(Graphics g) {
// do nothing
}
public Graphics getChild(int i) {
return null;
}
}
5. com.cleversoft.designpatterns.transparentcomposite.Picture.java:
package
com.cleversoft.designpatterns.transparentcomposite;
import java.util.Vector;
public class Picture extends Graphics {
private Vector<Graphics> list = new Vector<Graphics>(10);
public void draw() {
for (int i = 0; i < list.size(); i++) {
Graphics g = (Graphics) list.get(i);
g.draw();
}
}
public void add(Graphics g) {
list.add(g);
}
public void remove(Graphics g) {
list.remove(g);
}
public Graphics getChild(int i) {
return (Graphics) list.get(i);
}
}
import java.util.Vector;
public class Picture extends Graphics {
private Vector<Graphics> list = new Vector<Graphics>(10);
public void draw() {
for (int i = 0; i < list.size(); i++) {
Graphics g = (Graphics) list.get(i);
g.draw();
}
}
public void add(Graphics g) {
list.add(g);
}
public void remove(Graphics g) {
list.remove(g);
}
public Graphics getChild(int i) {
return (Graphics) list.get(i);
}
}
6. com.cleversoft.designpatterns.transparentcomposite.Client.java:
package
com.cleversoft.designpatterns.transparentcomposite;
public class Client {
/** *//**
* Transparent Composite
*/
public static void main(String[] args) {
Picture p = new Picture();
p.add(new Circle());
p.add(new Line());
Rectangle r = new Rectangle();
p.add(r);
p.draw();
}
}
public class Client {
/** *//**
* Transparent Composite
*/
public static void main(String[] args) {
Picture p = new Picture();
p.add(new Circle());
p.add(new Line());
Rectangle r = new Rectangle();
p.add(r);
p.draw();
}
}
安全式的合成模式:
1. com.cleversoft.designpatterns.safecomposite.Graphics.java:
package
com.cleversoft.designpatterns.safecomposite;
abstract public class Graphics {
public abstract void draw();
}
abstract public class Graphics {
public abstract void draw();
}
2. com.cleversoft.designpatterns.safecomposite.Circle.java:
package
com.cleversoft.designpatterns.safecomposite;
public class Circle extends Graphics {
public void draw() {
System.out.println("Drawing Circle");
}
}
public class Circle extends Graphics {
public void draw() {
System.out.println("Drawing Circle");
}
}
3. com.cleversoft.designpatterns.safecomposite.Line.java:
package
com.cleversoft.designpatterns.safecomposite;
public class Line extends Graphics {
public void draw() {
System.out.println("Drawing Line");
}
}
public class Line extends Graphics {
public void draw() {
System.out.println("Drawing Line");
}
}
4. com.cleversoft.designpatterns.safecomposite.Rectangle.java:
package
com.cleversoft.designpatterns.safecomposite;
public class Rectangle extends Graphics {
public void draw() {
System.out.println("Drawing Rectangle");
}
}
public class Rectangle extends Graphics {
public void draw() {
System.out.println("Drawing Rectangle");
}
}
5. com.cleversoft.designpatterns.safecomposite.Picture.java:
package
com.cleversoft.designpatterns.safecomposite;
import java.util.Vector;
public class Picture extends Graphics {
private Vector<Graphics> list = new Vector<Graphics>(10);
public void draw() {
for (int i = 0; i < list.size(); i++) {
Graphics g = (Graphics) list.get(i);
g.draw();
}
}
public void add(Graphics g) {
list.add(g);
}
public void remove(Graphics g) {
list.remove(g);
}
public Graphics getChild(int i) {
return (Graphics) list.get(i);
}
}
import java.util.Vector;
public class Picture extends Graphics {
private Vector<Graphics> list = new Vector<Graphics>(10);
public void draw() {
for (int i = 0; i < list.size(); i++) {
Graphics g = (Graphics) list.get(i);
g.draw();
}
}
public void add(Graphics g) {
list.add(g);
}
public void remove(Graphics g) {
list.remove(g);
}
public Graphics getChild(int i) {
return (Graphics) list.get(i);
}
}
6. com.cleversoft.designpatterns.safecomposite.Client.java:
package
com.cleversoft.designpatterns.safecomposite;
public class Client {
/** *//**
* Safe Composite
*/
public static void main(String[] args) {
Picture p = new Picture();
p.add(new Circle());
p.add(new Line());
Rectangle r = new Rectangle();
p.add(r);
p.draw();
}
}
public class Client {
/** *//**
* Safe Composite
*/
public static void main(String[] args) {
Picture p = new Picture();
p.add(new Circle());
p.add(new Line());
Rectangle r = new Rectangle();
p.add(r);
p.draw();
}
}
参考资料:
1. 《Java与模式》
2. http://terrylee.cnblogs.com/archive/2006/03/11/347919.html
|
|