简体   繁体   English

需要有关接口的建议

[英]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: 另外,我实现了一个BondCalculator ,如下所示:

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: 界面IRiskEvaluator如下:

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. 我看到的一个问题是,由于付款历史良好的客户不需要保证金,因此我仍然需要调用CalculateBond方法,并为保证金值返回0 This somehow does not feel right. 这有点不对劲。 Can this somehow be improved upon? 可以在某种程度上加以改进吗? Any comments/suggestion are appreciated. 任何意见/建议表示赞赏。

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." 您可以添加一个布尔的BondRequired属性以使意图明确,而不是依赖于人们推断“零键没有多大意义;开发人员必须打算使结果根本不代表任何键。”

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. 但是,我同意Magnum的说法,这已经比看起来必要的要复杂得多,因此,向该类型添加更多成员可能不是最好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM