实施更好的战略模式

发布于 2024-09-29 01:32:11 字数 2149 浏览 4 评论 0原文

我有一个支票账户和一个储蓄账户。我正在探索如何使用策略模式实现取款方法。

目前,支票账户和储蓄账户都继承自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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人九事 2024-10-06 01:32:11

在使用策略模式时,您可能必须提供实际上无用的信息。这是负面后果之一。另一种解决方案是通过构造函数将必要的信息传递到策略对象中。这可确保策略对象具有所需的最少信息量。

如果 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;
    }
}

有一些代码重复。因为这是家庭作业,所以你能做的并不多。如果这是我的决定,我会立即抽象出这些字段。

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.

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;
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文