4
、客户端代码
|
- static void Main(string[] args)
- {
- decimal costCEO = 0.0M;
- decimal costVPD = 0.0M;
-
- //Create CEO Node
- IComponent compCEO = new Composite("CEO", 500000);
-
- //Create VP-Development and Developer nodes
- IComponent compVPDev = new Composite("VP-Development", 250000);
-
- IComponent compDev1 = new Component("Developer1", 75000);
- IComponent compDev2 = new Component("Developer2", 50000);
-
- compVPDev.Add(compDev1);
- compVPDev.Add(compDev2);
-
- //Create VP-Sales node
- IComponent compVPSales = new Component("VP-Sales", 300000);
-
- compCEO.Add(compVPDev);
- compCEO.Add(compVPSales);
-
- //Get the cost incurred at the CEO level
- compCEO.GetCost(ref costCEO);
-
- Console.WriteLine(String.Format("The Cost incurred at the CEO level is {0:c} ", costCEO));
-
- //Get the cost incurred at the VP-Development level
- compVPDev.GetCost(ref costVPD);
- Console.WriteLine(String.Format("The Cost incurred at the VP-Development level is {0:c} ", costVPD));
- }
|
组合模式,将对象组合成树形结构以表示“部分
-
整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性。解决整合与部分可以被一致对待问题。
本文转自 灵动生活 51CTO博客,原文链接:http://blog.51cto.com/smartlife/267507,如需转载请自行联系原作者