自己工资自己算,策略设计模式(Strategy)
Insus.NET在想,每在公司或是老板给自己发工资时,每个人都会重新计算一下自己的工资,是否正确。好象老板或是会计都会把自己的工资计错了似的。因此,每个都人都是工资计算高手。
工资计算,需要知道基本工资是多少; 否有加薪,加薪方式,是按基本工资的百分比来加,还是由得公司或是老板随心而加,表现加多些,表现不好加少一些;是否有加班,加班的方式也要清楚,工作天加班,普通假日加班,或是法定假日加班;是否有补帖等等。
每一种方式,在此也算得上是算法,完成的都是相同的工作,只是实现不同罢了。为了简化与分工,我们可以把每种算法写成一个独立的类,减少它们之间的耦合。
每个算法,返回一个结果,因此可以写一个抽象类,让每个算法类继承这个公共接口。
Wage
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for Wage /// </summary> namespace Insus.NET { public abstract class Wage { public abstract double Salary(double basicWage); } }
具体算法,基本工资:
BasicWage
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for BasicWage /// </summary> namespace Insus.NET { public class BasicWage :Wage { public override double Salary(double basicWage) { return basicWage; } } }
去年公司业务好,收入丰,因此今年有得加薪,每个员工都是按基本工资百分比来加薪:
PlusWage
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for PlusWage /// </summary> namespace Insus.NET { public class PlusWage : Wage { private double _Rate = 1d; public PlusWage(double rate) //加工资百分比 { this._Rate = rate; } public override double Salary(double basicWage) { return basicWage * _Rate; } } }
今年沿续去年的业务,项目需要加班完成:
OvertimeWage
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for OvertimeWage /// </summary> namespace Insus.NET { public class OvertimeWage : Wage { private double _Hours; private double _OverTimeHourlyWage; private bool _OfficialHoliday; /// <summary> /// /// </summary> /// <param name="hours">加班小时数</param> /// <param name="rate">加班每小时工资</param> /// <param name="type">节假日,还是普通日</param> public OvertimeWage(double hours, double overTimeHourlyWage, bool officialHoliday) { this._Hours = hours; this._OverTimeHourlyWage = overTimeHourlyWage; this._OfficialHoliday = officialHoliday; } public override double Salary(double basicWage) { double salary = _Hours * _OverTimeHourlyWage * basicWage; if (_OfficialHoliday) salary = salary * 3; return salary; } } }
更加多的公司福利算法....
接下来,为了上面各个算法引用,用一个WageContext类来处理:
WageContext
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for WageContext /// </summary> namespace Insus.NET { public class WageContext { private Wage _Wage; public WageContext(string type) { switch (type) { case "基本工资": _Wage = new BasicWage(); break; case "加工资": _Wage = new PlusWage(0.10); break; case "加班工资": _Wage = new OvertimeWage(2, 10, true); break; } } public double GetSalary(double basicWage) { return _Wage.Salary(basicWage); } } }
OK,我们来看看这新年后第一个月薪水,公司在加薪,又有加班。
WageDemo.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Insus.NET; public partial class WageDemo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //"基本工资" //"加工资" //"加班工资" double basicWage = 8000d; double total = 0.0d; WageContext bw = new WageContext("基本工资"); total = bw.GetSalary(basicWage); WageContext pw = new WageContext("加工资"); double plusSalary = pw.GetSalary(basicWage); total = total + plusSalary; Response.Write(total.ToString()); } }
哈哈,有劳所获,开心一下子就行了,继续努力......