【发布时间】:2013-03-31 09:11:45
【问题描述】:
请耐心等待,让我解释一下。假设我在一家汽车保险公司工作,我们正在尝试开发一个在线门户网站,我们的客户可以在该门户网站上购买保险单。假设我们提供 3 种保险产品汽车保险 (CI)、摩托车保险 (MI) 和卡车保险 (TI)。我要说的是,这些产品中的 90% 在我们实现它们的方式上是相似的,但在剩下的 10% 上却大不相同。
让我以C#为例来演示(下面的代码是一个粗略的想法来演示我的问题不是真正的代码)。
public class CarInsurancePolicy
{
//common properties among all 3 insurance products
public decimal MadeYear {get;set}
public decimal PurcahsePrice {get;set}
//specific to this particular product
public bool HasCentralLocking {get;set}
public DateTime SpecialDateToDoWithCar {get;set}
}
public class MotorcycleInsurancePolicy
{
//common properties among all 3 insurance products
public decimal MadeYear {get;set}
public decimal PurcahsePrice {get;set}
//specific to this particular product
public int EngineStroke {get;set}
public DateTime SpecialDateToDoWithMotorcycle {get;set}
}
public class TruckInsurancePolicy
{
//common properties among all 3 insurance products
public decimal MadeYear {get;set}
public decimal PurcahsePrice {get;set}
//specific to this particular product
public decimal LoadCapacityInTons {get;set}
public DateTime SpecialDateToDoWithTruck {get;set}
}
正如您所见,对于每个策略类,它们的属性大多相同,但与每种类型有很大不同。现在来到数据库部分。
我不知道我是否应该这样做
CREATE TABLE BaseInsurancePolicy
(
//all common columns
)
CREATE TABLE CarInsurancePolicy
(
//specific to this type of product
)
CREATE TABLE MotorcycleInsurancePolicy
(
//specific to this type of product
)
CREATE TABLE TruckInsurancePolicy
(
//specific to this type of product
)
或者我应该这样做
CREATE TABLE GiantInsurancePolicyTable
(
//all columns and everything is NULLABLE
)
现在进入工作流部分
我们有几个基本的通用步骤来评价任何类型的产品。假设我们考虑到制造年份,KM 旅行等形成一个基本评级,然后根据特定的产品类型,我们有特殊的方法来计算溢价。
public class RatingEngingForCar
{
//can be common step
public decimal GetPremium() {}
//specific for car
private void ApplyA() {}
private void ApplyC() {}
}
public class RatingEngingForMotorcycle
{
//can be common step
public decimal GetPremium() {}
//specific for motorcycle
private void ApplyE() {}
private void ApplyF() {}
}
public class RatingEngingForTruck
{
//can be common step
public decimal GetPremium() {}
//specific for motorcycle
private void ApplyX() {}
private void ApplyZ() {}
}
然后我们有 90% 相似但 10% 差异很大的工作流程。然后再生成保险单(实际的保单文件)和发票也是一样的。
所以我不知道是否应该想出某种通用但灵活的方法来形成基类并开始继承和修改每个产品的特定行为。 或者我应该从第一个产品中复制过去并针对第 2、3、4、5 个产品进行修改吗?
在 UI 方面,我正在尝试将我们的 javascript 组件化,以便通常只要 html 结构和 id/name/class 相同,那么将通过包含和启动 JS 自动提供功能。但问题是我需要为每个产品在任何地方复制 HTML。
我不知道我是否已经从非常高的层次上足够清楚地解释了我的问题。如果不是,我将根据 cmets 更新/澄清。非常感谢。
【问题讨论】:
-
跟随你的心:“不要重复自己”。 :)
-
我现在没有任何想法。我有点想为所有东西都有一个基类,但是我不知道复杂性是否能证明灵活性。然后我想复制粘贴但是我觉得这显然是错误的!
-
3NF,基于接口的设计,采用 DI、SOLID 和 GRASP 原则。只是建议,我对这种讨论很感兴趣。
-
不仅仅是后端,还有前端。像 HTML、Javascript、CSS 一样,它有点像白标,但我猜不是。这对我来说也是新的。到目前为止,我们还没有找到我们喜欢的方式。我还查看了en.wikipedia.org/wiki/Data,_Context,_and_Interaction,但还没有完全了解我们如何从中受益。
-
我希望我的问题解释得足够清楚。
标签: c# oop architecture