C# 实施更好的战略模式
我有一个支票账户和一个储蓄账户。我正在探索如何使用策略模式实现退出方法 目前,支票帐户和储蓄帐户都从帐户继承。对于储蓄账户,提款不应导致余额降至100美元以下。对于支票账户,提款必须包括支票号码 我对使用这种方法没有信心,因为正如您将在下面看到的,“otherArguments”参数在某个场景中完全无用。我这样做的唯一原因是“展示”战略模式的使用C# 实施更好的战略模式,c#,algorithm,strategy-pattern,C#,Algorithm,Strategy Pattern,我有一个支票账户和一个储蓄账户。我正在探索如何使用策略模式实现退出方法 目前,支票帐户和储蓄帐户都从帐户继承。对于储蓄账户,提款不应导致余额降至100美元以下。对于支票账户,提款必须包括支票号码 我对使用这种方法没有信心,因为正如您将在下面看到的,“otherArguments”参数在某个场景中完全无用。我这样做的唯一原因是“展示”战略模式的使用 (对于那些关心此事的人来说,这是学校项目的一部分,下面是我编写的所有代码,我很好奇是否有更好的方法来完成它) 以下是我迄今为止所做的工作: publi
(对于那些关心此事的人来说,这是学校项目的一部分,下面是我编写的所有代码,我很好奇是否有更好的方法来完成它) 以下是我迄今为止所做的工作:
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;
}
}
在使用策略模式时,您可能必须提供实际上无用的信息。这是负面后果之一。另一种解决方案是通过构造函数将必要的信息传递到策略对象中。这样可以确保策略对象具有所需的最少信息量 如果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;
}
}
有一些代码重复。因为这是家庭作业,你能做的不多。如果这是我的电话,我会立即提取这些字段。哦,我必须使用策略(老板这么说)。另外,我猜我也在“算法”中使用了“支票号码”。嗯,我没有想到以这种方式使用构造函数。为每个撤销操作创建一个撤销器实例,而不是为每个撤销操作创建一个撤销器实例,这似乎比我目前所做的要好得多。“对于那些关心的人来说,这是学校项目的一部分”-你的问题很好。您提供了代码,展示了对问题的基本理解,并展示了您需要帮助的地方。许多人反对的家庭作业问题是“这是我的问题。我不知道从哪里开始。我没有代码。帮我解决它。谢谢!”。。。可以自由地改写为:“这是我的问题。我在课堂上没有集中注意力。我没有读过这本书。请帮我做家庭作业。”愚蠢的问题:为什么当前的平衡是对各种撤回的争论?每个帐户都知道它的余额是多少。@Eric,帐户知道它的余额,但是策略不知道,它不需要策略知道帐户类型是否存在,它只需要最少的信息就可以做到(从而减少耦合)策略模式背后的一个想法是封装两个(或多个)相似类之间的行为差异。因此,在使用策略时,不要使用支票账户、储蓄账户和抽象账户,只需使用一个具体的账户类即可。差异将局限于两种战略的实施。