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,如需转载请自行联系原作者

目录
相关文章
|
1月前
|
设计模式 算法 Java
行为型设计模式-策略模式(Strategy Pattern)
行为型设计模式-策略模式(Strategy Pattern)
|
1月前
|
设计模式 算法
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
48 1
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
|
5天前
|
编译器 C# Windows
一个基于Net Core3.0的WPF框架Hello World实例
一个基于Net Core3.0的WPF框架Hello World实例
|
30天前
|
设计模式 安全 Java
【设计模式】JAVA Design Patterns——Curiously Recurring Template Pattern(奇异递归模板模式)
该文介绍了一种C++的编程技巧——奇异递归模板模式(CRTP),旨在让派生组件能继承基本组件的特定功能。通过示例展示了如何创建一个`Fighter`接口和`MmaFighter`类,其中`MmaFighter`及其子类如`MmaBantamweightFighter`和`MmaHeavyweightFighter`强制类型安全,确保相同重量级的拳手之间才能进行比赛。这种设计避免了不同重量级拳手间的错误匹配,编译时会报错。CRTP适用于处理类型冲突、参数化类方法和限制方法只对相同类型实例生效的情况。
【设计模式】JAVA Design Patterns——Curiously Recurring Template Pattern(奇异递归模板模式)
|
1月前
|
设计模式 Java 应用服务中间件
设计模式 -结构型模式_门面模式(外观模式) Facade Pattern 在开源软件中的应用
设计模式 -结构型模式_门面模式(外观模式) Facade Pattern 在开源软件中的应用
41 1
|
1月前
|
设计模式 存储 Java
Java设计模式:解释一下单例模式(Singleton Pattern)。
`Singleton Pattern`是Java中的创建型设计模式,确保类只有一个实例并提供全局访问点。它通过私有化构造函数,用静态方法返回唯一的实例。类内静态变量存储此实例,对外仅通过静态方法访问。
26 1
|
1月前
|
设计模式 Java API
【设计模式】JAVA Design Patterns——Combinator(功能模式)
【设计模式】JAVA Design Patterns——Combinator(功能模式)
|
1月前
|
设计模式 监控 Java
【设计模式】JAVA Design Patterns——Circuit Breaker(断路器模式)
【设计模式】JAVA Design Patterns——Circuit Breaker(断路器模式)
|
1月前
|
设计模式 Java 程序员
【设计模式】JAVA Design Patterns——Bytecode(字节码模式)
【设计模式】JAVA Design Patterns——Bytecode(字节码模式)
|
1天前
|
设计模式 搜索推荐
工厂方法模式-大话设计模式
工厂方法模式-大话设计模式
5 1