Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实现银行模块的最佳模型_C#_Oop_Design Patterns - Fatal编程技术网

C# 实现银行模块的最佳模型

C# 实现银行模块的最佳模型,c#,oop,design-patterns,C#,Oop,Design Patterns,我正在用c#实现一个银行模块,其中包括一个储蓄账户、一个支票账户和一个简易储蓄账户。所有账户都有一个所有者和余额,所有账户都可以取款和存款,但取款不能超过余额。直到这里,很容易。现在,我们的储蓄账户有了一种新的方法,即利息和支票账户。一种方法是扣除费用,而EasySavianAccount两者都有。我想到的是使用抽象类帐户: public abstract class Account { protected string owner { get; set; } //always

我正在用c#实现一个银行模块,其中包括一个储蓄账户、一个支票账户和一个简易储蓄账户。所有账户都有一个所有者和余额,所有账户都可以取款和存款,但取款不能超过余额。直到这里,很容易。现在,我们的储蓄账户有了一种新的方法,即利息和支票账户。一种方法是扣除费用,而EasySavianAccount两者都有。我想到的是使用抽象类帐户:

 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,也许组合可以有所帮助

谢谢

我建议

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.

我建议创建一个类
Balance
,实现
iBlance
。所有账户都可以将
draw\deposit
委托给该类,因此它们没有代码重复,但您可以很容易地在其周围添加一些额外的逻辑(即征税、佣金、添加交易等)

这是一个很好的方法!这是四人帮的设计模式吗?或者只是一个更好的实现方法?我基于界面分离原则提出了这个建议,这是一个坚实的设计原则。你可以从中得到一些想法: