Net设计模式实例之组合模式(Composite Pattern)(3)

简介:
4 、客户端代码
  1. static void Main(string[] args)   
  2.         {   
  3.             decimal costCEO = 0.0M;   
  4.             decimal costVPD = 0.0M;   
  5.   
  6.             //Create CEO Node   
  7.             IComponent compCEO = new Composite("CEO", 500000);   
  8.   
  9.             //Create VP-Development and Developer nodes   
  10.             IComponent compVPDev = new Composite("VP-Development", 250000);   
  11.   
  12.             IComponent compDev1 = new Component("Developer1", 75000);   
  13.             IComponent compDev2 = new Component("Developer2", 50000);   
  14.   
  15.             compVPDev.Add(compDev1);   
  16.             compVPDev.Add(compDev2);   
  17.   
  18.             //Create VP-Sales node   
  19.             IComponent compVPSales = new Component("VP-Sales", 300000);   
  20.   
  21.             compCEO.Add(compVPDev);   
  22.             compCEO.Add(compVPSales);   
  23.   
  24.             //Get the cost incurred at the CEO level   
  25.             compCEO.GetCost(ref costCEO);   
  26.   
  27.             Console.WriteLine(String.Format("The Cost incurred at the CEO            level is {0:c} ", costCEO));   
  28.   
  29.             //Get the cost incurred at the VP-Development level   
  30.             compVPDev.GetCost(ref costVPD);   
  31.             Console.WriteLine(String.Format("The Cost incurred at the VP-Development level is {0:c} ", costVPD));   
  32.         }   
33.     
 

五、总结(Summary

组合模式,将对象组合成树形结构以表示“部分 - 整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性。解决整合与部分可以被一致对待问题。









本文转自 灵动生活 51CTO博客,原文链接:http://blog.51cto.com/smartlife/267507,如需转载请自行联系原作者

目录
相关文章
|
2月前
|
设计模式 缓存 安全
【设计模式】单例模式:确保类只有一个实例
【设计模式】单例模式:确保类只有一个实例
23 0
|
4月前
|
设计模式 算法 Java
行为型设计模式-策略模式(Strategy Pattern)
行为型设计模式-策略模式(Strategy Pattern)
|
10天前
|
设计模式 安全 Java
[设计模式Java实现附plantuml源码~结构型]树形结构的处理——组合模式
[设计模式Java实现附plantuml源码~结构型]树形结构的处理——组合模式
|
13天前
|
设计模式 Java 容器
【设计模式系列笔记】组合模式
组合模式(Composite Pattern)是一种结构型设计模式,它允许将对象组合成树状结构以表示部分-整体的层次结构。组合模式使得客户端可以统一处理单个对象和对象组合,而无需区分它们的类型。
40 12
|
20天前
|
设计模式 存储 Java
Java设计模式:解释一下单例模式(Singleton Pattern)。
`Singleton Pattern`是Java中的创建型设计模式,确保类只有一个实例并提供全局访问点。它通过私有化构造函数,用静态方法返回唯一的实例。类内静态变量存储此实例,对外仅通过静态方法访问。
16 1
|
22天前
|
设计模式 Java
小谈设计模式(20)—组合模式
小谈设计模式(20)—组合模式
|
29天前
|
设计模式 存储 Java
23种设计模式,组合模式的概念优缺点以及JAVA代码举例
【4月更文挑战第5天】组合模式(Composite Pattern)是一种结构型设计模式,旨在通过将对象组合成树形结构以表示部分-整体的层次结构,使用户对单个对象和组合对象的使用具有一致性。这种模式让客户可以统一地处理单个对象和组合对象
37 6
|
2月前
|
设计模式 JavaScript uml
设计模式之组合模式
设计模式之组合模式
设计模式之组合模式
|
2月前
|
设计模式 存储 安全
【设计模式】组合模式
【设计模式】组合模式
|
3月前
|
设计模式 Java
浅谈设计模式 - 组合模式(十二)
浅谈设计模式 - 组合模式(十二)
52 0