员工的工资由两个要素计算得出 -Basic
和StandardPoint
。一名员工每月将获得一个基本积分和一个或多个(或没有)标准积分。
有各种 StandardPoints,即 StarPerformerPoint、RecognitionPoint、ReferralPoint、BrandingPoint。
有计算 TotalAnnualSalary 的功能。一年内,一名员工最多可获得 4 个标准积分。即使他得到更多的分数,也只会计算 4 来计算年薪总额。
我有以下代码。它工作正常。但是,内存利用率不高。StandardPointElement 被创建多次。
在这种情况下,我们如何优化对象创建?
更新
我们可以使用享元模式之类的东西吗?
维基百科说关于享元模式
享元是一种通过与其他类似对象共享尽可能多的数据来最小化内存使用的对象;当简单的重复表示将使用不可接受的内存量时,这是一种使用大量对象的方法。
在文章Flyweight Design Pattern - C#中,Shailendra Chauhan 提到了以下内容
外部数据是在运行时动态计算的,它保存在享元对象之外。因此它可以是有状态的。
代码
interface ISalaryScoreElement
{
int SalaryScore { get; }
}
public class BasicElement : ISalaryScoreElement
{
public int SalaryScore
{
get
{
return 100;
}
}
}
public class StandardPointElement : ISalaryScoreElement
{
public int SalaryScore
{
get
{
return 10;
}
}
}
员工类
class Employee
{
private List<string> eligibility;
public List<string> EligibleSalaryTypes
{
get
{
return eligibility;
}
}
public Employee(List<string> eligibility)
{
this.eligibility = eligibility;
}
public int GetTotalAnnualSalary()
{
int totalSalary = 0;
ISalaryScoreElement sal = null;
CalculatorFactory factory = new CalculatorFactory();
int occurenceCountForStandardPoint = 0;
foreach (string salaryType in EligibleSalaryTypes)
{
switch (salaryType)
{
case "Basic":
sal = factory.GetSalaryElement("BasicElement");
break;
case "ReferralPoint":
sal = factory.GetSalaryElement("StandardPointElement");
break;
case "BrandingPoint":
sal = factory.GetSalaryElement("StandardPointElement");
break;
case "RecognitionPoint":
sal = factory.GetSalaryElement("StandardPointElement");
break;
case "StarPerformerPoint":
sal = factory.GetSalaryElement("StandardPointElement");
break;
default:
throw new Exception("No mapping available");
}
if (sal is StandardPointElement)
{
occurenceCountForStandardPoint++;
if (occurenceCountForStandardPoint > 4)
{
//StandardPointElement can be considered a maximum of 4 times for salary calculation
continue;
}
}
totalSalary = totalSalary + sal.SalaryScore;
}
return totalSalary;
}
}
工厂
class CalculatorFactory
{
public ISalaryScoreElement GetSalaryElement(string salaryKey)
{
ISalaryScoreElement c = null;
switch (salaryKey)
{
case "BasicElement":
c = new BasicElement();
break;
case "StandardPointElement":
c = new StandardPointElement();
break;
default:
throw new Exception("Factory cannot create the object specified");
}
return c;
}
}
客户
class Program
{
static void Main(string[] args)
{
List<string> eligibleBonus = new List<string>();
//For January 2013
eligibleBonus.Add("Basic");
eligibleBonus.Add("StarPerformerPoint");
//For February 2013
eligibleBonus.Add("Basic");
eligibleBonus.Add("StarPerformerPoint");
eligibleBonus.Add("ReferralPoint");
//For March 2013
eligibleBonus.Add("Basic");
eligibleBonus.Add("BrandingPoint");
eligibleBonus.Add("RecognitionPoint");
//For April 2013
eligibleBonus.Add("Basic");
eligibleBonus.Add("BrandingPoint");
Employee e = new Employee(eligibleBonus);
int effectiveSalary = e.GetTotalAnnualSalary();
Console.WriteLine(effectiveSalary);
Console.ReadKey();
}
}