Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 744
mukul singh Avatar asked Oct 20 '11 01:10

mukul singh


People also ask

Why are interfaces are important?

A good User Interface is important because it can turn potential visitors to buyers as it facilitates interaction between the user and your website or application. It does not only focus only on the aesthetics but also maximizes responsiveness, efficiency and accessibility of a website.

What do you mean by interface?

Definition of interface (Entry 1 of 2) 1a : the place at which independent and often unrelated systems meet and act on or communicate with each other the man-machine interface. b : the means by which interaction or communication is achieved at an interface.

What is an example of a interface?

Examples of user interfacesvirtual reality. ATMs. speedometer. the old iPod click wheel.

What is user friendly interface?

A user-friendly interface is a software interface where the user can easily understand and navigate through the application in an efficient way.


1 Answers

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.

like image 69
phoog Avatar answered Oct 19 '22 02:10

phoog