Note that there are some explanatory texts on larger screens.

plurals
  1. POBest design pattern for refactoring a class that does calculation based on many parameters
    primarykey
    data
    text
    <p>I'm refactoring a set of classes as below which does some price calculations. the calculation is done based on many parameters. The code is :</p> <pre><code>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 &amp; 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&lt;int, List&lt;int&gt;&gt; _States_neighboureness; private static ReadOnlyCollection&lt;City&gt; _Citieslist; private static ReadOnlyCollection&lt;Province&gt; _Provinceslist; protected ReadOnlyCollection&lt;City&gt; Citieslist {get { return _Citieslist; }} protected ReadOnlyCollection&lt;Province&gt; ProvincesList {get { return _Provinceslist; }} protected ReadOnlyDictionary&lt;int, List&lt;int&gt;&gt; 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 &amp; parameters // for calculating prices } } public SpecialParcel : GeneralParcel { public decimal CalculatePrice() { //use of those three static variables in calculations // plus other properties &amp; parameters // for calculating prices } } </code></pre> <p>Right now, the code uses <strong>"Strategy pattern"</strong> efficiently.</p> <p>my question is that those three static properties, really are not part of parcel object, they are need only for price calculations, so which design pattern or wrapping(refactoring), is suggested?</p> <p>Is having another interface as below necessary (&amp; then wrap those static properties inside it?, even make static that class, because it is basically only some calculations), then how to connect it to IParcel? Doing so, how to implement <strong><code>CalculatePrice()</code></strong> in <strong><code>SpecialParcel</code></strong> &amp; <strong><code>ExpressParcel</code></strong> classes?</p> <pre><code>public interface IPriceCalculator { decimal CalculatePrice(); } </code></pre> <p>EDIT: the above was only a big picture of all system, there is also other consideration that in comments, we discus about them, and i write them here again for clearing things .</p> <p>there is <code>BulkDiscount</code> for all of ParcelTypes. bulk post happens when customer send more than 10 parcels(or any threshold), also there is special discount when one customer send more than 10 parcel to a unique destination(there is only one receiver). now this type of discounts are managed in each parcel type's <code>CalculatePrice()</code>. even there are discount for blinds for under 7Kg parcels.</p> <p>also right now there are 3 <code>parceltype</code>, i show only 2 of them here. but we need to add other type in future(TNT &amp; DHL support). each type has many services that customer can select and pay for it. for example, <code>sms service</code> or <code>email service</code> &amp; so on.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload