2

当座預金口座と普通預金口座を持っています。戦略パターンを使用して撤回メソッドを実装する方法を検討しています。

現在、当座預金口座と普通預金口座はどちらも Account を継承しています。普通預金口座の場合、引き出しによって残高が 100 ドルを下回ってはなりません。当座預金口座では、引き出しには小切手番号が含まれている必要があります。

以下に示すように、「otherArguments」パラメーターは 1 つのシナリオではまったく役に立たないため、このアプローチを使用する自信はありません。そして、私がこのようにした唯一の理由は、戦略パターンの使用を「示す」ことです。

(心配している人のために、これは学校のプロジェクトの一部であり、以下のコードはすべて私が書いたものであり、それを実現するためのより良い方法があるかどうか知りたいです).

これまでに行ったことは次のとおりです。

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

1 に答える 1

1

Strategy パターンを使用する場合、実際には役に立たない情報を提供する必要がある場合があります。悪影響の一つです。もう 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;
    }
}

コードの重複があります。宿題なので、できることはあまりありません。それが私の電話だった場合、私はそれらのフィールドをすぐに抽象化します。

于 2010-10-25T01:52:31.567 に答える