为银行模块实施最佳模型

时间:2017-06-20 08:56:54

标签: c# oop design-patterns

我正在c#中实施一个银行模块,其中包含一个储蓄帐户,一个支票帐户和一个简单的储蓄帐户。所有账户都有所有者和余额,所有账户都可以提取和存款,但他们不能提取超过余额。直到这里,非常容易。现在,我们的储蓄账户有一个新方法applyInterest和checkingsAccount方法deductFees和easySavinAccount都有。我想到的是使用抽象类帐户:

 public abstract class Account
{
    protected string owner { get; set; }
    //always use decimal especially for money c# created them for that purpose :)
    protected decimal balance { get; set; }
    public void deposit(decimal money)
    {
        if (money >= 0)
        {
            balance += money;
        }
    }
    public void withdraw(decimal money)
    {
        if (money > balance)
        {
            throw new System.ArgumentException("You can't withdraw that much money from your balance");
        }
        else balance -= money;
    }
}

将由所有3个类继承。是否有适合以更好的方式实现此目的的设计模式?特别是对于easySaveAccount,也许组成可以帮忙吗?

谢谢!

2 个答案:

答案 0 :(得分:2)

我建议

1.implement separate interfaces declaring the methods applyInterest and deductFees.
2.You have already declared the abstract class Account.
3.Now you can implement these interfaces in your classes for savings,checkings and easy saving account.All these classes should
be implementing the abstract class.

答案 1 :(得分:0)

我建议创建一个实现Balance的类IBalance。所有帐户都可以将withdraw\deposit委托给该类,因此他们没有代码重复,但您可以轻松地在其周围添加一些额外的逻辑(即,征税,提交,添加交易等)。