实施更好的战略模式
我有一个支票账户和一个储蓄账户。我正在探索如何使用策略模式实现取款方法。
目前,支票账户和储蓄账户都继承自Account。对于储蓄账户,提款不应导致余额低于 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;
}
}
I have a Chequing account and a Saving account. I'm exploring how to implement the withdraw method using a Strategy pattern.
Currently, the Chequing and Saving account both inherit from Account. For Saving account, withdrawals should not cause the balance to dip below 100 dollars. With the Chequing account, withdrawals must include a cheque number.
I'm not confident in using this approach because, as you'll see below, the "otherArguments" parameter is totally useless in one scenario. And the only reason I have it like this is to "show" the use of the strategy pattern.
(for those who are concerned, this is part of a school project, all the code below I wrote and I'm curious about if there is a better way to get it done).
Here's what I've done so far:
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在使用策略模式时,您可能必须提供实际上无用的信息。这是负面后果之一。另一种解决方案是通过构造函数将必要的信息传递到策略对象中。这可确保策略对象具有所需的最少信息量。
如果 100 个最小值是唯一的算法差异,那么更好的解决方案可能是模板方法。
这是示例代码。
有一些代码重复。因为这是家庭作业,所以你能做的并不多。如果这是我的决定,我会立即抽象出这些字段。
In using the Strategy pattern, you might have to supply information that is in fact useless. It's one of the negative consequences. The other solution is to pass in the necessary information into the Strategy object via constructor. This ensures that the Strategy object has the minimal amount of information it requires.
A better solution might be the Template Method though, if the 100 minimum is the only algorithmic difference.
Here's the sample code.
There's some code duplication. Since it's homework, there's not a whole lot you can do. If it was my call though, I'd abstract those fields right out.