本文共 1011 字,大约阅读时间需要 3 分钟。
)
、场景
总经理直接领导技术部经理和销售部经理,技术部经理直接领导开发人员 A 和开发人员 B 。销售部经理暂时没有直接下属员工,随着公司规模增大,销售部门会新增销售员工。计算组织结构的总工资状况。 IComponent 接口:此接口包括了 Component 和 Composite 的所有属性,公司每个角色都有职称 Title 和工资待遇 Salary , Add 方法把员工加入到组织团队中。 Component 叶子节点:叶节点没有子节点, Add 方法实现没有任何意义。 Composite 组合类:此类有一个员工集合 _listEmployees,Add 方法向此集合中添加员工信息。 、代码
|
- public interface IComponent
- {
- string Title { get; set; }
- decimal Salary { get; set; }
- void Add(IComponent c);
- void GetCost(ref decimal salary);
- }
|
|
- 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;
- }
- }
|
本文转自 灵动生活 51CTO博客,原文链接:http://blog.51cto.com/smartlife/267505,如需转载请自行联系原作者