『设计模式』 又谈麦当劳的食品--组合模式(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)