需要有关接口的建议

发布于 2024-12-10 18:32:23 字数 2348 浏览 0 评论 0原文

我只需要一些关于我试图解决的问题的反馈...

这是问题的描述:

我的公司销售一些客户可以在一段时间内付款的产品。客户分为现有客户和新客户。为了让客户购买产品,我们会检查信用度,有时会要求客户存入保证金,该保证金是可退还的。有些客户在我们这里有良好的付款记录,因此我们不需要向他们收取保证金。为了实现评估,我设计了如下解决方案:

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

定义两个实现该接口的类。

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

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

有一个类会根据客户是否与我们有良好的付款历史记录来返回适当的处理器。

另外,我实现了一个 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);
    }
}

接口 IRiskEvaluator 如下:

public interface IRiskEvaluator
{
    Decimal EvaluateRisk();
}

实现此接口的两个类如下:

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

现在

public class ProductRiskEvaluator : IRiskEvaluator
{        

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

调用所有这些都是通过方法完成的。相关代码如下:

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);

这个设计可以吗,还可以进一步改进吗?我发现的一个问题是,由于具有良好付款历史的客户不需要保证金,因此我仍然需要调用方法 CalculateBond 并返回 0 作为保证金价值。这感觉有点不对劲。这可以以某种方式改进吗?如有任何意见/建议,我们将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弥枳 2024-12-17 18:32:23

您可以添加一个布尔值 BondRequired 属性来明确意图,而不是依赖人们来推断“零键没有多大意义;开发人员一定希望该结果代表根本没有键。”

然而,我同意 Magnum 的观点,即这已经比看起来必要的更加复杂,因此向该类型添加更多成员可能不是最好的做法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文