【发布时间】:2014-01-31 14:47:02
【问题描述】:
我在 .Net 4.0 中有以下 C# 代码。它需要将 IBusiness 类型转换为 IRetailBusiness。
//Type checking
if (bus is IRetailBusiness)
{
//Type casting
investmentReturns.Add(new RetailInvestmentReturn((IRetailBusiness)bus));
}
if (bus is IIntellectualRights)
{
investmentReturns.Add(new IntellectualRightsInvestmentReturn((IIntellectualRights)bus));
}
业务场景:
我正在为投资控股公司设计一个软件系统。公司拥有零售业务和知识产权业务。 BookShop 和 AudioCDShop 是零售业务的例子。 EngineDesignPatent 和BenzolMedicinePatent 是知识产权业务的例子。这两种业务类型完全不相关。
投资公司有一个概念叫InvestmentReturn(但每个个体企业对这个概念完全一无所知)。 InvestmentReturn 是从每项业务中获得的利润,使用ProfitElement 计算。对于每种“业务类型”(零售、知识产权),使用的 ProfitElement 是不同的。
问题
如何重构此类设计以避免type casting 和type checking?
抽象投资
public abstract class InvestmentReturn
{
public double ProfitElement { get; set; }
public IBusiness Business{ get; set; }
public abstract double GetInvestmentProfit();
public double CalculateBaseProfit()
{
double profit = 0;
if (ProfitElement < 5)
{
profit = ProfitElement * 5 / 100;
}
else if (ProfitElement < 20)
{
profit = ProfitElement * 7 / 100;
}
else
{
profit = ProfitElement * 10 / 100;
}
return profit;
}
}
扩展
public class RetailInvestmentReturn : InvestmentReturn
{
public RetailInvestmentReturn(IRetailBusiness retail)
{
Business = retail;
}
public override double GetInvestmentProfit()
{
//GrossRevenue is the ProfitElement for RetailBusiness
ProfitElement = ((IRetailBusiness)Business).GrossRevenue;
return base.CalculateBaseProfit();
}
}
public class IntellectualRightsInvestmentReturn : InvestmentReturn
{
public IntellectualRightsInvestmentReturn(IIntellectualRights intellectual)
{
Business = intellectual;
}
public override double GetInvestmentProfit()
{
//Royalty is the ProfitElement for IntellectualRights Business
ProfitElement = ((IIntellectualRights)Business).Royalty;
return base.CalculateBaseProfit();
}
}
客户
class Program
{
static void Main(string[] args)
{
#region MyBusines
List<IBusiness> allMyProfitableBusiness = new List<IBusiness>();
BookShop bookShop1 = new BookShop(75);
AudioCDShop cd1Shop = new AudioCDShop(80);
EngineDesignPatent enginePatent = new EngineDesignPatent(1200);
BenzolMedicinePatent medicinePatent = new BenzolMedicinePatent(1450);
allMyProfitableBusiness.Add(bookShop1);
allMyProfitableBusiness.Add(cd1Shop);
allMyProfitableBusiness.Add(enginePatent);
allMyProfitableBusiness.Add(medicinePatent);
#endregion
List<InvestmentReturn> investmentReturns = new List<InvestmentReturn>();
foreach (IBusiness bus in allMyProfitableBusiness)
{
//Type checking
if (bus is IRetailBusiness)
{
//Type casting
investmentReturns.Add(new RetailInvestmentReturn((IRetailBusiness)bus));
}
if (bus is IIntellectualRights)
{
investmentReturns.Add(new IntellectualRightsInvestmentReturn((IIntellectualRights)bus));
}
}
double totalProfit = 0;
foreach (var profitelement in investmentReturns)
{
totalProfit = totalProfit + profitelement.GetInvestmentProfit();
Console.WriteLine("Profit: {0:c}", profitelement.GetInvestmentProfit());
}
Console.ReadKey();
}
}
业务领域实体
public interface IBusiness
{
}
public abstract class EntityBaseClass
{
}
public interface IRetailBusiness : IBusiness
{
double GrossRevenue { get; set; }
}
public interface IIntellectualRights : IBusiness
{
double Royalty { get; set; }
}
#region Intellectuals
public class EngineDesignPatent : EntityBaseClass, IIntellectualRights
{
public double Royalty { get; set; }
public EngineDesignPatent(double royalty)
{
Royalty = royalty;
}
}
public class BenzolMedicinePatent : EntityBaseClass, IIntellectualRights
{
public double Royalty { get; set; }
public BenzolMedicinePatent(double royalty)
{
Royalty = royalty;
}
}
#endregion
#region Retails
public class BookShop : EntityBaseClass, IRetailBusiness
{
public double GrossRevenue { get; set; }
public BookShop(double grossRevenue)
{
GrossRevenue = grossRevenue;
}
}
public class AudioCDShop : EntityBaseClass, IRetailBusiness
{
public double GrossRevenue { get; set; }
public AudioCDShop(double grossRevenue)
{
GrossRevenue = grossRevenue;
}
}
#endregion
参考文献
【问题讨论】:
-
这里有很多代码。你能想出一个更简单(如果做作的话)的例子来证明你的问题吗?
标签: c# generics design-patterns domain-driven-design visitor-pattern