Net设计模式实例之组合模式(Composite Pattern)(2)
2010-01-18 10:01
549 查看
四.案例分析(Example)
1、场景
假设公司组织结构为:--总结理
----技术部门经理
------开发人员A
------开发人员B
----销售部门经理
总经理直接领导技术部经理和销售部经理,技术部经理直接领导开发人员A和开发人员B。销售部经理暂时没有直接下属员工,随着公司规模增大,销售部门会新增销售员工。计算组织结构的总工资状况。
如下图所示
Component叶子节点:叶节点没有子节点,Add方法实现没有任何意义。
Composite组合类:此类有一个员工集合_listEmployees,Add方法向此集合中添加员工信息。
GetCost方法获得组织结构中的工资待遇总和
2、代码
1、接口IComponent |
public interface IComponent { string Title { get; set; } decimal Salary { get; set; } void Add(IComponent c); void GetCost(ref decimal salary); } [align=left]8. [/align] |
2、叶节点Component |
public class Component : IComponent { public string Title { get; set; } public decimal Salary { get; set; } public Component(string Title, decimal Salary) { this.Title = Title; this.Salary = Salary; } public void Add(IComponent c) { Console.WriteLine("Cannot add to the leaf!"); } public void GetCost(ref decimal salary) { salary += Salary; } } [align=left]22. [/align] [align=left] [/align] |
相关文章推荐
- Net设计模式实例之组合模式(Composite Pattern)(1) 推荐
- Net设计模式实例之组合模式(Composite Pattern)(3)
- Net设计模式实例之组合模式(Composite Pattern)
- net设计模式实例之组合模式(Composite Pattern)
- Net设计模式实例之组合模式(Composite Pattern)
- Net设计模式实例之备忘录模式(Memento Pattern)
- Net设计模式实例之简单工厂模式(Simple Factory Pattern)
- Net设计模式实例之外观模式(Façade Pattern)
- Net设计模式实例之模板方法模式(Template Mothed Pattern)(1) 推荐
- Net设计模式实例之命令模式(Command Pattern)
- .Net中的设计模式——从实例谈OOP、工厂模式和重构
- Net设计模式实例之原型模式( Prototype Pattern)(2)
- Net设计模式实例之中介者模式(Mediator Pattern)(1) 推荐
- Net设计模式实例之解释器模式(Interpreter Pattern)
- Net设计模式实例之工厂方法模式( Factory Method Pattern)
- Net设计模式实例之桥接模式( Bridge Pattern)
- Net设计模式实例系列文章总结(郝宪玮)
- Net设计模式实例之简单工厂模式(Simple Factory Pattern)
- Net设计模式实例之单例模式( Singleton Pattern)
- Net设计模式实例之装饰者模式(Decorator Pattern)