mukul singh
mukul singh

Reputation: 81

Need advice on interfaces

I just need a bit of feedback regarding a problem I am trying to solve...

Here is a description of the problem :

My company sells some products for which the customer can pay over a certain period of time. The customers are classed as existing or new. In order to let the customers buy the product, we check the credit worthiness and on occasions a customer can be asked to deposit a bond, which is refundable. Some customers have a good payment history with us, so we don't need to charge them a bond amount. In order to implement the assessment, I have designed a solution as follows:

public interface ICreditAssessor
{
    CreditAssessment Process();
    Decimal CalculateBond(BondCalculator bc);
}

Two classes are defined which implement this interface.

public class GoodClientProcessor : ICreditAssessor{
    ..... methods
}

public class OtherClientProcessor : ICreditAssessor{
    ..... methods
}

There is a class which returns the appropriate processor depending on whether the customers have a good payment history with us or not.

Also, I have implemented a BondCalculator as follows:

public class BondCalculator
{
    List<IRiskEvaluator> riskEvaluators;

    public BondCalculator()
    {
        riskEvaluators = new List<IRiskEvaluator>();
    }

    public Decimal GetSuggestedBond()
    {
        Decimal riskAmount = 0;
        foreach (IRiskEvaluator ire in riskEvaluators)
        {
            Decimal tempRisk = ire.EvaluateRisk();

            if (tempRisk > riskAmount)
            {
                riskAmount = tempRisk;
            }
        }

        return riskAmount;
    }

    public void SetRiskEvaluator(IRiskEvaluator re)
    {
        this.riskEvaluators.Add(re);
    }
}

Interface IRiskEvaluator is as follows:

public interface IRiskEvaluator
{
    Decimal EvaluateRisk();
}

The two classes implementing this interface are as follows:

public class FinancialRiskEvaluator : IRiskEvaluator
{
    Decimal IRiskEvaluator.EvaluateRisk()
    {
        ... calculate risk amount
    }
}

and

public class ProductRiskEvaluator : IRiskEvaluator
{        

    Decimal IRiskEvaluator.EvaluateRisk()
    {
        ... calculate risk amount
    }
}

Now calling all this is done via a method. The relevant code is as below:

ICreditAssessor creditAssessor = CreditAssessorFactory.GetAssessor(somecriteria);
CreditAssessment assessment = creditAssessor.Process();
.
.
.
BondCalculator bc = new BondCalculator();
bc.SetRiskEvaluator(new FinancialRiskEvaluator(xmlResults));
bc.SetRiskEvaluator(new ProductRiskEvaluator(productCost));
creditCheckProcessor.CalculateBond(bc);

Is this design OK or can it be improved any further? One issue I see is that as the customers with good payment history do not need a bond, I still need to call the method CalculateBond and return 0 for the bond value. This somehow does not feel right. Can this somehow be improved upon? Any comments/suggestion are appreciated.

Upvotes: 8

Views: 176

Answers (1)

phoog
phoog

Reputation: 43056

You could add a boolean BondRequired property to make the intent explicit, rather than depending on people to infer that "a bond of zero doesn't make much sense; the developer must have intended that result to represent no bond at all."

However, I agree with Magnum that this is already more complicated than seems necessary, so adding more members to the type may not be the best thing to do.

Upvotes: 1

Related Questions