我正在重構一組類,如下所示,它執行一些價格計算。計算是基於許多參數完成的。 的代碼是:重構基於許多參數進行計算的類的最佳設計模式
public interface IParcel {
int SourceCode { get; set; }
int DestinationCode { get; set; }
int weight{get;set;}
decimal CalculatePrice();
}
public abstract class GeneralParcel : IParcel {
//implementation of inteface properties
//these properties set with in SourceCode & DestinationCode
//and are used in CalculatePrice() inside classes that inherit from GeneralParcel
protected SourceProvinceCode{get; protected set;}
protected DestinationProvinceCode{get;protected set;}
//private variables we need for calculations
private static ReadOnlyDictionary<int, List<int>> _States_neighboureness;
private static ReadOnlyCollection<City> _Citieslist;
private static ReadOnlyCollection<Province> _Provinceslist;
protected ReadOnlyCollection<City> Citieslist {get { return _Citieslist; }}
protected ReadOnlyCollection<Province> ProvincesList {get { return _Provinceslist; }}
protected ReadOnlyDictionary<int, List<int>> StatesNeighboureness {get {return _States_neighboureness; }}
//constructor code that initializes the static variables
//implementation is in concrete classes
public abstract decimal CalculatePrice();
}
public ExpressParcel : GeneralParcel {
public decimal CalculatePrice() {
//use of those three static variables in calculations
// plus other properties & parameters
// for calculating prices
}
}
public SpecialParcel : GeneralParcel {
public decimal CalculatePrice() {
//use of those three static variables in calculations
// plus other properties & parameters
// for calculating prices
}
}
眼下,代碼使用「策略模式」有效。
我的問題是,那三個靜態屬性,真的不是parcel對象的一部分,它們只需要進行價格計算,那麼建議哪個設計模式或者wrap(重構)呢?
是否有另一個接口,如下所需(&然後將它們內部的靜態屬性包裹起來?甚至使得該靜態類,因爲它基本上只是一些計算),那麼如何將它連接到IParcel? 這樣做,如何實現CalculatePrice()
在SpecialParcel
& ExpressParcel
類?
public interface IPriceCalculator {
decimal CalculatePrice();
}
編輯:以上只是所有系統的大畫面,也有在評論,我們鐵餅關於他們其他的考慮,我在這裏他們寫一遍清理的東西。
對於所有ParcelType,有BulkDiscount
。當客戶發送10個以上的包裹(或任何閾值)時,發送大量帖子,當一個客戶發送超過10個包裹到一個唯一的目的地(只有一個接收者)時,也有特別折扣。現在這種類型的折扣在每個宗地類型的CalculatePrice()
中進行管理。即使7Kg以下的包裹也有折扣。
也現在有3 parceltype
,我只在這裏顯示2。但我們需要在將來添加其他類型(TNT & DHL支持)。每種類型都有很多服務,客戶可以選擇並付費。例如 ,sms service
或email service
&等等。
請考慮http://codereview.stackexchange.com/。 –
是的,謝謝,將'this'添加到'there' –