【设计模式】—— 组合模式Composite

  前言:【模式总览】——————————by xingoo

  模式意图

  使对象组合成树形的结构。使用户对单个对象和组合对象的使用具有一致性。

  

  应用场景

  1 表示对象的 部分-整体 层次结构

  2 忽略组合对象与单个对象的不同,统一的使用组合结构中的所有对象。

  模式结构

  【安全的组合模式】

  这种组合模式,叶子节点,也就是单个对象不具有对象的控制功能。仅仅有简单的业务操作。

 1 package com.xingoo.composite.safe;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 interface Component{
 7     Composite getCmposite();
 8     void sampleComposite();
 9 }
10 
11 class Leaf implements Component{
12 
13     public Composite getCmposite() {
14         return null;
15     }
16 
17     public void sampleComposite() {
18         System.out.println("Leaf operation");
19     }
20     
21 }
22 
23 class Composite implements Component{
24 
25     private List<Component> list = new ArrayList();
26     
27     public void add(Component component){
28         list.add(component);
29     }
30     
31     public void remove(Component component){
32         list.remove(component);
33     }
34     
35     public Composite getCmposite() {
36         return this;
37     }
38 
39     public void sampleComposite() {
40         System.out.println("Composite operation");
41         for(Component com : list){
42             com.sampleComposite();
43         }
44     }
45     
46 }
47 public class Client {
48     public static void main(String[] args) {
49         Component leaf1 = new Leaf();
50         Component leaf2 = new Leaf();
51         Component composite = new Composite();
52         composite.getCmposite().add(leaf1);
53         composite.getCmposite().add(leaf2);
54         composite.getCmposite().sampleComposite();
55     }
56 }

  执行结果

Composite operation
Leaf operation
Leaf operation

 

  【透明的组合模式】

  这种组合模式,叶子节点与组合对象具有相同的方法,外表看来毫无差异。不过叶子节点的处理方法默认为空。忽略叶子节点,与组合对象的差异性。

 1 package com.xingoo.composite.transparent;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 interface Component{
 7     public void SampleOperation();
 8     public void add(Component component);
 9     public void remove(Component component);
10     public Component getComponent();
11 }
12 
13 class Leaf implements Component{
14     public void SampleOperation() {
15         System.out.println("leaf operation!");
16     }
17 
18     public void add(Component component) {
19         
20     }
21 
22     public void remove(Component component) {
23         
24     }
25     
26     public Component getComponent(){
27         return this;
28     }
29 }
30 
31 class Composite implements Component{
32     
33     private List<Component> list = new ArrayList();
34     
35     public void SampleOperation() {
36         System.out.println("composite operation!");
37         for(Component com : list){
38             com.getComponent().SampleOperation();
39         }
40     }
41 
42     public void add(Component component) {
43         list.add(component);
44     }
45 
46     public void remove(Component component) {
47         list.remove(component);
48     }
49     
50     public Component getComponent(){
51         return this;
52     }
53 }
54 public class Client {
55     public static void main(String[] args) {
56         Component leaf1 = new Leaf();
57         Component leaf2 = new Leaf();
58         Component leaf3 = new Leaf();
59         Component composite1 = new Composite();
60         Component composite = new Composite();
61         
62         composite1.add(leaf3);
63         
64         composite.getComponent().add(leaf1);
65         composite.getComponent().add(leaf2);
66         composite.getComponent().add(composite1);
67         
68         composite.getComponent().SampleOperation();
69     }
70 }

  本例中的结构层次

  执行结果

composite operation!
leaf operation!
leaf operation!
composite operation!
leaf operation!

 

posted @ 2014-10-29 19:46 xingoo 阅读(...) 评论(...) 编辑 收藏