【設計模式】—— 組合模式Composite

  前言:【模式總覽】——————————by xingoohtml

  模式意圖

  使對象組合成樹形的結構。使用戶對單個對象和組合對象的使用具備一致性。java

  

  應用場景

  1 表示對象的 部分-總體 層次結構安全

  2 忽略組合對象與單個對象的不一樣,統一的使用組合結構中的全部對象。this

  模式結構

  【安全的組合模式】spa

  這種組合模式,葉子節點,也就是單個對象不具備對象的控制功能。僅僅有簡單的業務操做。code

 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 }

  執行結果component

Composite operation Leaf operation Leaf operation

 

  【透明的組合模式】htm

  這種組合模式,葉子節點與組合對象具備相同的方法,外表看來毫無差別。不過葉子節點的處理方法默認爲空。忽略葉子節點,與組合對象的差別性。對象

 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 }

  本例中的結構層次blog

  執行結果

composite operation! leaf operation! leaf operation! composite operation! leaf operation!
相關文章
相關標籤/搜索