我有一个支票账户和一个储蓄账户我正在探索如何使用策略模式实现撤回方法。
目前,支票账户和储蓄账户都是从账户继承的对于储蓄账户,提款不应导致余额降至100美元以下对于支票账户,提款必须包括支票号码。
我对使用这种方法没有信心,因为正如您将在下面看到的,“otherArguments”参数在一个场景中完全没有用处我这样做的唯一原因是“展示”战略模式的使用。
(对于那些关心的人来说,这是学校项目的一部分,下面的代码都是我写的,我很好奇是否有更好的方法来完成它)。
我到目前为止所做的是:
public abstract class Account
{
public double Balance{get; set;}
public WithdrawStrategy Withdrawer
{
get; set;
}
public abstract void withdraw(double currentBalance, double amount, object otherArguments);
}
public class Chequing: Account
{
public Chequing()
{
Withdrawer= new ChequingAccountWithdrawer();
}
public override void withdraw(double currentBalance, double amount, object otherArguments)
{
if (null != Withdrawer)
{
double balance = Withdrawer.withdraw(currentBalance, amount, otherArguments);
Balance = balance;
}
}
}
public class Saving: Account
{
public Saving()
{
Withdrawer= new SavingAccountWithdrawer();
}
public override void withdraw(double currentBalance, double amount, object otherArguments)
{
if (null != Withdrawer)
{
double balance = Withdrawer.withdraw(currentBalance, amount, otherArguments);
Balance = balance;
}
}
}
public interface WithdrawStrategy
{
double withdraw(double currentBalance, double amount, object otherArguments);
}
public ChequingAccountWithdrawer: WithdrawStrategy
{
public double withdraw(double currentBalance, double amount, object otherArguments)
{
string cheqNum = otherArguments.ToString();
if (!string.IsNullOrEmpty(cheqNum))
{
currentBalance -= amount;
}
return currentBalance;
}
}
public SavingAccountWithdrawer: WithdrawStrategy
{
public double withdraw(double currentBalance, double amount, object otherArguments)
{
if (currentBalance - amount > 100) //hard code for example's sake
{
currentBalance -= amount;
}
return currentBalance;
}
}
最佳答案
在使用策略模式时,您可能必须提供事实上无用的信息。这是负面后果之一另一种解决方案是通过构造函数将必要的信息传递到Strategy对象中。这确保了strategy对象所需的信息量最小。
如果100最小值是唯一的算法差异,那么更好的解决方案可能是模板方法。
这是示例代码。
public abstract class Account
{
public double Balance{get; set;}
public abstract void withdraw();
}
public class Chequing: Account
{
public override void withdraw()
{
//It's not clear where the values for your constructor come from, but
//you get the idea.
WithdrawStrategy withdrawer= new ChequingAccountWithdrawer();
/////////////////////////////////////////////////////////////
double balance = Withdrawer.withdraw();
Balance = balance;
}
}
public class Saving: Account
{
public override void withdraw()
{
//Same idea here.
WithdrawStrategy withdrawer= new SavingAccountWithdrawer();
/////////////////////////////////////////////////////////////
double balance = Withdrawer.withdraw();
Balance = balance;
}
}
public interface WithdrawStrategy
{
double withdraw();
}
public ChequingAccountWithdrawer: WithdrawStrategy
{
private readonly int balance;
private readonly double amount;
private readonly string number;
public ChequingAccountWithdrawer(double aBalance, double aAmount, string aNumber) {
balance = aBalance
amount = aAmount
number = aNumber
}
public double withdraw()
{
if (number)
{
currentBalance -= amount;
}
return currentBalance;
}
}
public SavingAccountWithdrawer: WithdrawStrategy
{
private readonly int balance;
private readonly double amount;
public SavingAccountWithdrawer(double aBalance, double aAmount) {
balance = aBalance
amount = aAmount
}
public double withdraw()
{
if (currentBalance - amount > 100) //hard code for example's sake
{
currentBalance -= amount;
}
return currentBalance;
}
}
有一些代码重复。既然是家庭作业,你就没什么可做的了不过,如果是我的要求,我会马上把这些字段抽象出来。