Domanda

Ho un account Chequing e un account di risparmio. Sto esplorando come implementare il metodo di prelievo usando un modello di strategia.

Attualmente, l'account Chequing e Salvaling eredita entrambi dal conto. Per il risparmio del conto, i prelievi non dovrebbero causare il saldo al di sotto di 100 dollari. Con il conto Chequing, i prelievi devono includere un numero di assegno.

Non sono fiducioso nell'uso di questo approccio perché, come vedrai di seguito, il parametro "Altro Argumenti" è totalmente inutile in uno scenario. E l'unica ragione per cui ho così come questo è "mostrare" l'uso del modello di strategia.

(Per coloro che sono preoccupati, questo fa parte di un progetto scolastico, tutto il codice di seguito che ho scritto e sono curioso di sapere se c'è un modo migliore per farlo).

Ecco cosa ho fatto finora:

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;
    }
}
È stato utile?

Soluzione

Nell'uso del modello di strategia, potrebbe essere necessario fornire informazioni in realtà inutili. È una delle conseguenze negative. L'altra soluzione è di trasmettere le informazioni necessarie nell'oggetto strategico tramite il costruttore. Ciò garantisce che l'oggetto strategico abbia la quantità minima di informazioni necessarie.

Una soluzione migliore potrebbe essere il metodo del modello, se il minimo 100 è l'unica differenza algoritmica.

Ecco il codice di esempio.

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

C'è qualche duplicazione del codice. Dal momento che sono i compiti, non c'è molto che puoi fare. Se fosse la mia chiamata, però, astrarrei quei campi.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top