合成模式(Composite Pattern)

    合成模型模式属于对象的结构模式,有时又叫做部分-整体模式。合成模式将对象组织到树结构中,可以用来描述整体与部分的关系。合成模式可以使客户端将单纯元素与复合元素同等对待。

类图:
合成模式(Composite Pattern)_第1张图片

 

一、安全式和透明式的合成模式
合成模式(Composite Pattern)_第2张图片
 
1、安全式的合成模式
1)类图
合成模式(Composite Pattern)_第3张图片
合成模式(Composite Pattern)_第4张图片
 
 
2)java代码
Component类:
 package composite.safe;
public interface Component {
 
 public Composite getComposite();
 
 public void operation();
}
 
Composite类: 
package composite.safe;
import java.util.Enumeration;
import java.util.Vector;
public class Composite implements Component{
 private Vector componentVector = new Vector();
 public Composite getComposite() {
  return this;
 }
 public void operation() {
  Enumeration enumeration = components();
  while(enumeration.hasMoreElements()){
   ((Component) enumeration.nextElement()).operation();
  }
 }
 public void add(Component c){
  componentVector.addElement(c);
 }
 public void remove(Component c){
  componentVector.removeElement(c);
 }
 public Enumeration components(){
  return componentVector.elements();
 }
}
 
Leaf类:
package composite.safe;
public class Leaf implements Component{
 public Composite getComposite() {
  // TODO Auto-generated method stub
  return null;
 }
 public void operation() {
  System.out.println("haha........");
 }
}
 
 
Client类:
package composite.safe;
public class Client {
 public static void main(String args[]){
  Composite c1 = new Composite();
  Composite c2 = new Composite();
  Leaf c3 = new Leaf();
  Leaf c4 = new Leaf();
  Leaf c5 = new Leaf();
  c2.add(c3);
  c2.add(c4);
  c1.add(c5);
  c1.add(c2);
  c1.operation();
 }
}
 
二、透明式的合成模式
1)类图
合成模式(Composite Pattern)_第5张图片
合成模式(Composite Pattern)_第6张图片
 
 
2)java代码
Component类:
package composite.transparency;
import java.util.Enumeration;
public interface Component {
 
 public Composite getComposite();
 public void operation();
 public void add(Component c);
 public void remove(Component c);
 public Enumeration components();
}
 
Composite类:
package composite.transparency;
import java.util.Enumeration;
import java.util.Vector;
public class Composite implements Component{
 private Vector componentVector = new Vector();
 public Composite getComposite() {
  return this;
 }
 public void operation() {
  Enumeration enumeration = components();
  while(enumeration.hasMoreElements()){
   ((Component) enumeration.nextElement()).operation();
  }
 }
 public void add(Component c){
  componentVector.addElement(c);
 }
 public void remove(Component c){
  componentVector.removeElement(c);
 }
 public Enumeration components(){
  return componentVector.elements();
 }
}
 
Leaf类:
package composite.transparency;
import java.util.Enumeration;
public class Leaf implements Component{
 public Composite getComposite() {
  return null;
 }
 public void operation() {
  System.out.println("haha........");
 }
 public void add(Component c) {
 }
 public Enumeration components() {
  return null;
 }
 public void remove(Component c) {
 }
}
 
Client类:
package composite.transparency;
public class Client {
 public static void main(String args[]){
  Composite c1 = new Composite();
  Composite c2 = new Composite();
  Leaf c3 = new Leaf();
  Leaf c4 = new Leaf();
  Leaf c5 = new Leaf();
  c2.add(c3);
  c2.add(c4);
  c1.add(c5);
  c1.add(c2);
  c1.operation();
 }
}

你可能感兴趣的:(合成模式(Composite Pattern))