『设计模式』 又谈麦当劳的食品--组合模式(Composite)
生活随笔
收集整理的這篇文章主要介紹了
『设计模式』 又谈麦当劳的食品--组合模式(Composite)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
23種設計模式+額外常用設計模式匯總 (持續更新)
我又又又談了一期麥當勞,麥當勞看到了記得打錢😂
引入
- 商品類別樹的節點被分成兩種,一種是容器節點,另一種是葉子節點。
- 容器節點可以包含其他容器節點或者葉子節點
組合模式
- 組合模式有時又叫做部分——整體模式(Part-Whole)
。組合模式將對象組織到樹結構中,可以用來描述整體與部分的關系。組合模式可以使客戶端將單純元素與復合元素同等看待。 - 一個樹結構由兩種節點組成:樹枝節點和樹葉節點。樹枝節點可以有子節點,而一個樹葉節點不可以有子節點。除了根節點外,其它節點有且只有一個父節點。
模式結構
- 抽象構件(Component)角色
這是一個抽象角色,它給參與組合的對象規定一個接口。這個角色給出共有接口及其默認行為。 - 樹葉構件(Leaf) 角色
代表參加組合的樹葉對象。一個樹葉對象沒有下級子對象。 - 樹枝構件(Composite)角色
代表參加組合的有子對象的對象,并給出樹枝構件對象的行為。
示意性代碼
//抽象節點類abstract class Component{protected string name; public Component(string name){this.name = name;}public abstract void Add(Component c);public abstract void Remove(Component c);public abstract void Display(int depth);}//葉子節點類class Leaf:Component{public Leaf(string name) : base(name) { }public override void Add(Component c){Console.WriteLine("Cannot add to a leaf");}public override void Remove(Component c){Console.WriteLine("Cannot remove from a leaf");}public override void Display(int depth) {Console.WriteLine(new String('-',depth)+name);}}//組合類class Composite : Component{private List<Component> children = new List<Component>();public Composite(string name) : base(name) { }public override void Add(Component c){children.Add(c);}public override void Display(int depth){Console.WriteLine(new String('-', depth) + name);foreach(Component component in children){component.Display(depth + 2);}}public override void Remove(Component c){children.Remove(c);}}class Program{static void Main(string[] args){Composite root = new Composite("root");root.Add(new Leaf("LeafA"));root.Add(new Leaf("LeafB"));Composite comp = new Composite("CompositeX");comp.Add(new Leaf("LeafXA"));comp.Add(new Leaf("LeafXB"));root.Add(comp);Composite comp2 = new Composite("CompositeXY");comp2.Add(new Leaf("LeafXYA"));comp2.Add(new Leaf("LeafXYB"));comp.Add(comp2);root.Add(new Leaf("LeafC"));Leaf leaf = new Leaf("LeafD");root.Add(leaf);root.Remove(leaf);root.Display(1);Console.Read();}}認識組合模式
- 組合模式的目的
讓客戶端不再區分操作的是組合對象還是葉子對象,而是以一種統一的方式來操作 - 對象樹
組合模式會組合出樹形結構來,這也就意味著,所有可以使用對象樹來描述或操作的功能,都可以考慮使用組合模式。比如讀取XML文件,或是對語句進行語法分析等。 - 組合模式的實現根據所實現接口的區別分為兩種形式,分別稱為安全模式和透明模式。組合模式可以不提供父對象的管理方法,但組合模式必須在合適的地方提供子對象的管理方法(諸如Fadd、remove、Display等)。
透明方式
- 在Component里面聲明所有的用來管理子類對象的方法,包括add ()、remove (),以及Display ()方法。
- 優點:所有的構件類都有相同的接口。在客戶端看來,樹葉類對象與組合類對象的區別起碼在接口層次上消失了,客戶端可以同等的對待所有的對象。這就是透明形式的組合模式。
- 缺點:不夠安全,因為樹葉類對象和合成類對象在本質上是有區別的。樹葉類對象不可能有下平個層次的對象,因此add()、remove()以及Display ()方法沒有意義,在編譯時期不會出錯,而會在運行時期才會出錯。
安全方式
- 在Composite類里面聲明所有的用來管理子類對象的方法
- 優點:這樣的做法是安全的做法,樹葉類型的對象根本就沒有管理子類對象的方法,因此,如果客戶端對樹葉類對象使用這些方法時,程序會在編譯時期出錯。
- 缺點:不夠透明,樹葉類和合成類將具有不同的接口
使用情況
- 需求中是體現部分與整體層次的結構時
- 希望用戶忽略組合對象與單個對象的不同,統一的使用組合結構中的所有對象時。
優點
- 定義了包含基本對象和組合對象的類層次結構
基本對象可以組合成組合對象,組合對象又能組合成更復雜的組合對象,可以不斷地遞歸組合下去,從而構成一個統一的組合對象的類層次結構 - 統一了組合對象和葉子對象
- 簡化了客戶端調用
不用區分組合對象和葉子對象 - 更容易擴展
由于客戶端是統一的面對Component來操作,因此,新定義的Composite或leaf子類能夠很容易的與已有的結構一起工作,而不需改變客戶端
缺點
很難限制組合中的組件類型
這是容易添加新的組件帶來的問題,在需要檢測組件類型的時候,使得我們不能依靠編譯期的類型約束來完成,必須在運行期間動態檢測
本質
同意葉子對象和組合對象
實例
麥當勞的食物的例子:
#mermaid-svg-24PbXM0dQT5L8bQM .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-24PbXM0dQT5L8bQM .label text{fill:#333}#mermaid-svg-24PbXM0dQT5L8bQM .node rect,#mermaid-svg-24PbXM0dQT5L8bQM .node circle,#mermaid-svg-24PbXM0dQT5L8bQM .node ellipse,#mermaid-svg-24PbXM0dQT5L8bQM .node polygon,#mermaid-svg-24PbXM0dQT5L8bQM .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-24PbXM0dQT5L8bQM .node .label{text-align:center;fill:#333}#mermaid-svg-24PbXM0dQT5L8bQM .node.clickable{cursor:pointer}#mermaid-svg-24PbXM0dQT5L8bQM .arrowheadPath{fill:#333}#mermaid-svg-24PbXM0dQT5L8bQM .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-24PbXM0dQT5L8bQM .flowchart-link{stroke:#333;fill:none}#mermaid-svg-24PbXM0dQT5L8bQM .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-24PbXM0dQT5L8bQM .edgeLabel rect{opacity:0.9}#mermaid-svg-24PbXM0dQT5L8bQM .edgeLabel span{color:#333}#mermaid-svg-24PbXM0dQT5L8bQM .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-24PbXM0dQT5L8bQM .cluster text{fill:#333}#mermaid-svg-24PbXM0dQT5L8bQM div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-24PbXM0dQT5L8bQM .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-24PbXM0dQT5L8bQM text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-24PbXM0dQT5L8bQM .actor-line{stroke:grey}#mermaid-svg-24PbXM0dQT5L8bQM .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-24PbXM0dQT5L8bQM .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-24PbXM0dQT5L8bQM #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-24PbXM0dQT5L8bQM .sequenceNumber{fill:#fff}#mermaid-svg-24PbXM0dQT5L8bQM #sequencenumber{fill:#333}#mermaid-svg-24PbXM0dQT5L8bQM #crosshead path{fill:#333;stroke:#333}#mermaid-svg-24PbXM0dQT5L8bQM .messageText{fill:#333;stroke:#333}#mermaid-svg-24PbXM0dQT5L8bQM .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-24PbXM0dQT5L8bQM .labelText,#mermaid-svg-24PbXM0dQT5L8bQM .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-24PbXM0dQT5L8bQM .loopText,#mermaid-svg-24PbXM0dQT5L8bQM .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-24PbXM0dQT5L8bQM .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-24PbXM0dQT5L8bQM .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-24PbXM0dQT5L8bQM .noteText,#mermaid-svg-24PbXM0dQT5L8bQM .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-24PbXM0dQT5L8bQM .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-24PbXM0dQT5L8bQM .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-24PbXM0dQT5L8bQM .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-24PbXM0dQT5L8bQM .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-24PbXM0dQT5L8bQM .section{stroke:none;opacity:0.2}#mermaid-svg-24PbXM0dQT5L8bQM .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-24PbXM0dQT5L8bQM .section2{fill:#fff400}#mermaid-svg-24PbXM0dQT5L8bQM .section1,#mermaid-svg-24PbXM0dQT5L8bQM .section3{fill:#fff;opacity:0.2}#mermaid-svg-24PbXM0dQT5L8bQM .sectionTitle0{fill:#333}#mermaid-svg-24PbXM0dQT5L8bQM .sectionTitle1{fill:#333}#mermaid-svg-24PbXM0dQT5L8bQM .sectionTitle2{fill:#333}#mermaid-svg-24PbXM0dQT5L8bQM .sectionTitle3{fill:#333}#mermaid-svg-24PbXM0dQT5L8bQM .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-24PbXM0dQT5L8bQM .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-24PbXM0dQT5L8bQM .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-24PbXM0dQT5L8bQM .grid path{stroke-width:0}#mermaid-svg-24PbXM0dQT5L8bQM .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-24PbXM0dQT5L8bQM .task{stroke-width:2}#mermaid-svg-24PbXM0dQT5L8bQM .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-24PbXM0dQT5L8bQM .taskText:not([font-size]){font-size:11px}#mermaid-svg-24PbXM0dQT5L8bQM .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-24PbXM0dQT5L8bQM .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-24PbXM0dQT5L8bQM .task.clickable{cursor:pointer}#mermaid-svg-24PbXM0dQT5L8bQM .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-24PbXM0dQT5L8bQM .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-24PbXM0dQT5L8bQM .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-24PbXM0dQT5L8bQM .taskText0,#mermaid-svg-24PbXM0dQT5L8bQM .taskText1,#mermaid-svg-24PbXM0dQT5L8bQM .taskText2,#mermaid-svg-24PbXM0dQT5L8bQM .taskText3{fill:#fff}#mermaid-svg-24PbXM0dQT5L8bQM .task0,#mermaid-svg-24PbXM0dQT5L8bQM .task1,#mermaid-svg-24PbXM0dQT5L8bQM .task2,#mermaid-svg-24PbXM0dQT5L8bQM .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-24PbXM0dQT5L8bQM .taskTextOutside0,#mermaid-svg-24PbXM0dQT5L8bQM .taskTextOutside2{fill:#000}#mermaid-svg-24PbXM0dQT5L8bQM .taskTextOutside1,#mermaid-svg-24PbXM0dQT5L8bQM .taskTextOutside3{fill:#000}#mermaid-svg-24PbXM0dQT5L8bQM .active0,#mermaid-svg-24PbXM0dQT5L8bQM .active1,#mermaid-svg-24PbXM0dQT5L8bQM .active2,#mermaid-svg-24PbXM0dQT5L8bQM .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-24PbXM0dQT5L8bQM .activeText0,#mermaid-svg-24PbXM0dQT5L8bQM .activeText1,#mermaid-svg-24PbXM0dQT5L8bQM .activeText2,#mermaid-svg-24PbXM0dQT5L8bQM .activeText3{fill:#000 !important}#mermaid-svg-24PbXM0dQT5L8bQM .done0,#mermaid-svg-24PbXM0dQT5L8bQM .done1,#mermaid-svg-24PbXM0dQT5L8bQM .done2,#mermaid-svg-24PbXM0dQT5L8bQM .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-24PbXM0dQT5L8bQM .doneText0,#mermaid-svg-24PbXM0dQT5L8bQM .doneText1,#mermaid-svg-24PbXM0dQT5L8bQM .doneText2,#mermaid-svg-24PbXM0dQT5L8bQM .doneText3{fill:#000 !important}#mermaid-svg-24PbXM0dQT5L8bQM .crit0,#mermaid-svg-24PbXM0dQT5L8bQM .crit1,#mermaid-svg-24PbXM0dQT5L8bQM .crit2,#mermaid-svg-24PbXM0dQT5L8bQM .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-24PbXM0dQT5L8bQM .activeCrit0,#mermaid-svg-24PbXM0dQT5L8bQM .activeCrit1,#mermaid-svg-24PbXM0dQT5L8bQM .activeCrit2,#mermaid-svg-24PbXM0dQT5L8bQM .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-24PbXM0dQT5L8bQM .doneCrit0,#mermaid-svg-24PbXM0dQT5L8bQM .doneCrit1,#mermaid-svg-24PbXM0dQT5L8bQM .doneCrit2,#mermaid-svg-24PbXM0dQT5L8bQM .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-24PbXM0dQT5L8bQM .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-24PbXM0dQT5L8bQM .milestoneText{font-style:italic}#mermaid-svg-24PbXM0dQT5L8bQM .doneCritText0,#mermaid-svg-24PbXM0dQT5L8bQM .doneCritText1,#mermaid-svg-24PbXM0dQT5L8bQM .doneCritText2,#mermaid-svg-24PbXM0dQT5L8bQM .doneCritText3{fill:#000 !important}#mermaid-svg-24PbXM0dQT5L8bQM .activeCritText0,#mermaid-svg-24PbXM0dQT5L8bQM .activeCritText1,#mermaid-svg-24PbXM0dQT5L8bQM .activeCritText2,#mermaid-svg-24PbXM0dQT5L8bQM .activeCritText3{fill:#000 !important}#mermaid-svg-24PbXM0dQT5L8bQM .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-24PbXM0dQT5L8bQM g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-24PbXM0dQT5L8bQM g.classGroup text .title{font-weight:bolder}#mermaid-svg-24PbXM0dQT5L8bQM g.clickable{cursor:pointer}#mermaid-svg-24PbXM0dQT5L8bQM g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-24PbXM0dQT5L8bQM g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-24PbXM0dQT5L8bQM .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-24PbXM0dQT5L8bQM .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-24PbXM0dQT5L8bQM .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-24PbXM0dQT5L8bQM .dashed-line{stroke-dasharray:3}#mermaid-svg-24PbXM0dQT5L8bQM #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-24PbXM0dQT5L8bQM #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-24PbXM0dQT5L8bQM #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-24PbXM0dQT5L8bQM #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-24PbXM0dQT5L8bQM #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-24PbXM0dQT5L8bQM #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-24PbXM0dQT5L8bQM #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-24PbXM0dQT5L8bQM #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-24PbXM0dQT5L8bQM .commit-id,#mermaid-svg-24PbXM0dQT5L8bQM .commit-msg,#mermaid-svg-24PbXM0dQT5L8bQM .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-24PbXM0dQT5L8bQM .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-24PbXM0dQT5L8bQM .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-24PbXM0dQT5L8bQM g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-24PbXM0dQT5L8bQM g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-24PbXM0dQT5L8bQM g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-24PbXM0dQT5L8bQM g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-24PbXM0dQT5L8bQM g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-24PbXM0dQT5L8bQM g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-24PbXM0dQT5L8bQM .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-24PbXM0dQT5L8bQM .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-24PbXM0dQT5L8bQM .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-24PbXM0dQT5L8bQM .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-24PbXM0dQT5L8bQM .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-24PbXM0dQT5L8bQM .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-24PbXM0dQT5L8bQM .edgeLabel text{fill:#333}#mermaid-svg-24PbXM0dQT5L8bQM .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-24PbXM0dQT5L8bQM .node circle.state-start{fill:black;stroke:black}#mermaid-svg-24PbXM0dQT5L8bQM .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-24PbXM0dQT5L8bQM #statediagram-barbEnd{fill:#9370db}#mermaid-svg-24PbXM0dQT5L8bQM .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-24PbXM0dQT5L8bQM .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-24PbXM0dQT5L8bQM .statediagram-state .divider{stroke:#9370db}#mermaid-svg-24PbXM0dQT5L8bQM .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-24PbXM0dQT5L8bQM .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-24PbXM0dQT5L8bQM .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-24PbXM0dQT5L8bQM .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-24PbXM0dQT5L8bQM .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-24PbXM0dQT5L8bQM .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-24PbXM0dQT5L8bQM .note-edge{stroke-dasharray:5}#mermaid-svg-24PbXM0dQT5L8bQM .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-24PbXM0dQT5L8bQM .error-icon{fill:#522}#mermaid-svg-24PbXM0dQT5L8bQM .error-text{fill:#522;stroke:#522}#mermaid-svg-24PbXM0dQT5L8bQM .edge-thickness-normal{stroke-width:2px}#mermaid-svg-24PbXM0dQT5L8bQM .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-24PbXM0dQT5L8bQM .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-24PbXM0dQT5L8bQM .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-24PbXM0dQT5L8bQM .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-24PbXM0dQT5L8bQM .marker{fill:#333}#mermaid-svg-24PbXM0dQT5L8bQM .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}#mermaid-svg-24PbXM0dQT5L8bQM {color: rgba(0, 0, 0, 0.75);font: ;}主食漢堡三明治雞腿堡牛肉堡黃油三明治甜辣三明治其中圓形為枝節點
菱形為葉節點
抽象節點
葉節點
package 組合模式;public class Leaf_Node extends Node{public Leaf_Node(String node_Name) {super(node_Name);}@Overridepublic void add_Node(Node node) {// TODO Auto-generated method stubSystem.out.println("Warning:此食物已經為最細化分類,不能進一步分類");}@Overridepublic void remove_Node(Node node) {// TODO Auto-generated method stubSystem.out.println("Warning:此食物已經為最細化分類,無子分類"); //暗示伍茲不行?玩梗!}@Overridepublic void show() {System.out.println(" "+Node_Name);} }枝節點
package 組合模式;import java.util.ArrayList; import java.util.List;public class Branch_Node extends Node {List<Node> Node_List=new ArrayList<Node>();public Branch_Node(String node_Name) {super(node_Name);}@Overridepublic void add_Node(Node node) {Node_List.add(node);}@Overridepublic void remove_Node(Node node) {Node_List.remove(node);}@Overridepublic void show() {System.out.println("+"+Node_Name);for(Node node:Node_List){System.out.print(" ");node.show();}}}客戶端
package 組合模式;public class Client {public static void main(String[] args) {Node Main_Food=new Branch_Node("主食");Node Hamburger=new Branch_Node("漢堡");Node Sandwich=new Branch_Node("三明治");Node JTHB=new Leaf_Node("雞腿堡");Node NRHB=new Leaf_Node("牛肉堡");Node HYSMZ=new Leaf_Node("黃油三明治");Node TLSMZ=new Leaf_Node("甜辣三明治");Sandwich.add_Node(HYSMZ);Sandwich.add_Node(TLSMZ);Hamburger.add_Node(JTHB);Hamburger.add_Node(NRHB);Main_Food.add_Node(Sandwich);Main_Food.add_Node(Hamburger);Main_Food.show();} }總結
以上是生活随笔為你收集整理的『设计模式』 又谈麦当劳的食品--组合模式(Composite)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 已备案域名购买要注意什么
- 下一篇: 『设计模式』适配器模式(Adapter)