实施更好的战略模式

时间:2010-10-25 01:46:40

标签: c# algorithm strategy-pattern

我有一个Chequing帐户和一个Saving帐户。我正在探索如何使用策略模式实现撤销方法。

目前,Chequing和Saving帐户都从Account继承。对于储蓄账户,提款不应导致余额低于100美元。使用Chequing帐户,提款必须包含支票号码。

我对使用这种方法没有信心,因为正如您将在下面看到的那样,“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;
    }
}

1 个答案:

答案 0 :(得分:1)

在使用策略模式时,您可能必须提供实际上无用的信息。这是负面后果之一。另一种解决方案是通过构造函数将必要的信息传递到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;
    }
}

有一些代码重复。既然是家庭作业,那么你可以做很多事情。如果这是我的电话,我会把这些字段抽象出来。