2017-06-20 3 views
2

私は、貯蓄口座、小切手口座、および簡単な貯蓄口座を含む銀行口座をC#で実装しています。すべての口座に所有者と残高があり、それらのすべてが引き出して入金できますが、残高以上の引き出しはできません。ここまで、とても簡単です。さて、私たちの貯蓄口座には、新しい方法のapplyInterestとcheckingsAccountメソッドdeductFeesとeasySavinAccountの両方があります。私が考えたのは、抽象クラスAccountを使用しています。銀行モジュールに最適なモデルを実装する

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

私はIBalanceを実装するクラスBalanceを作成することをお勧め

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. 
+0

これは素晴らしいアプローチです!これは4人のギャングのデザインパターンですか?それとも、それを実装するより良い方法ですか? –

+0

私はこれをSOLID設計原理の1つである界面分離原理に基づいて提案しました。これからいくつかのアイデアを得ることができます:https://www.codeproject.com/Articles/822791/Developing-MVC-applications-using-SOLID -principles – micky

0

お勧めします。すべてのアカウントはそのクラスにwithdraw\depositを委任しているので、コードの重複はありませんが、簡単に別のロジックを追加できます(税金、遵守、取引の追加など)

関連する問題