2011-10-26 31 views
0

這裏簡要介紹業務需求。使用設計模式來建模訂閱系統

我有一個名爲PricingSchedule的實體,它表示系統的「訂閱」。我們在團隊無處不在的語言中使用「定價計劃」一詞,而不是「訂閱」,但理論上,訂閱是同樣的事情。

什麼決定了PricingSchedule的價格是兩件事情結合: 1.在PricingSchedule(的「持續時間」又名多久是您的訂閱...... 1年,2年,等... 2.您想在定價時間表中包含多少款式(另一個實體)您可以選擇兩種方式來包含款式; 1.支付每種款式,2.支付所有款式

第二種是新增的要求。之前,這主要是由PricingSchedule的持續時間確定的價格。

我的問題是這樣的... PriceSchedule的價格並不意味着任何持續時間或StylePricingType自身應用。當他們結合在一起時,我只能得到最終的價格;又名,2年持續5種風格。

我們有四種可能的預定持續時間,從幾天到3或4年不等。

我們有兩種可能的方式來記錄風格選擇; 1.每種風格或2.所有風格。這兩件事合起來然後決定整體價格。

我開始思考戰略設計模式可以幫助我在這裏,又名;

public interface IDurationPricingStrategy 
public decimal GetDurationPriceFor(PricingSchedule) 

public interface IStylePricingStrategy 
public decimal GetStylePriceFor(PricingSchedule) 

這是一個很好的方法來區分可能會改變前進的事情,但在這裏是磨擦;如果不知道其他策略的「條件」,我就無法實施一個策略。

例如,對於IStylePricingStrategy,我實現了無限風情定價選項,如下所示:

public class UnlimitedStylePricingStrategy : IStylePricingStrategy 
{ 
public decimal GetStylePriceFor(PricingSchedule) 
{ 
if (PricingSchedule.Duration.Type == DurationType.OneYear) 
{ 
return decimal x; 
} 
if (PricingSchedule.Duration.Type == DurationType.TwoYears) 
{ 
return decimal x; 
} 
} 
} 

,如果我採取這種做法,這意味着如果當我來補充或更改時間定價類型,那麼我必須改變我的StyleStrategy實現類,這打破了SRP,基本上讓我回到原點。

如果只有一件「物品」決定了PricingSchedule的價格,但是當我有兩件這樣的事情時,這很容易,那就是我撞牆的地方。

是否有另一種模式可以使用,或以某種方式使用策略模式不同?我覺得這個問題仍然讓我走向戰略,但我不知道如何結合兩個戰略,而不是一個。

非常感謝! 邁克

回答

1

我想一種方法是在創建期間的接口:

public interface IDuration 
{ 
    int GetDuration(); 
    decimal CalculatePrice(object whatever); // int something, or whatever. 
} 

在有你的時間表類使用它:

public class PricingSchedule 
{ 
    public IDuration Duration { get; set; } 
} 

然後付款樣式類可以使用像這樣的持續時間:

public class UnlimitedStylePricingStyle : PricingStyle 
{ 
    public override void GetStylePriceFor(PricingSchedule schedule) 
    { 
     int duration = schedule.Duration.GetDuration(); 

     //..... 
    } 
} 

棘手的是天,我不知道你會如何處理這個問題,但我認爲使用界面是你最好的選擇。如果您需要添加新的持續時間,只需實現界面IDuration即可。

public override void GetStylePriceFor(PricingSchedule schedule) 
{ 
    int duration = schedule.Duration.GetDuration(); 

    int temp = 34; 

    decimal result = schedule.Duration.CalculatePrice(temp); 
} 

希望這給你一個粗略的想法:

你可以然後通過類似計算價格。

+0

傑森,非常感謝您花時間閱讀我的文章。如果有趣的是,你提到「持續時間」,B/C我看着我的模型,我的測試,並在最後十分鐘,我意識到我錯過了我的模型持續時間對象!雖然你在這裏的使用是很好的想法。我將運行這個,看看它帶來了我的測試。再次感謝! Mike –

+0

乾杯:)希望我的帖子對你有積極的影響。 –