سؤال

لدي حساب chaquing وحساب حفظ. أنا أستكشف كيفية تنفيذ طريقة الانسحاب باستخدام نمط استراتيجية.

حاليًا ، يرث حساب Chequing and Saving كلاهما من الحساب. لتوفير الحساب ، يجب ألا تتسبب عمليات السحب في انخفاض الرصيد إلى أقل من 100 دولار. مع حساب chaquing ، يجب أن تتضمن عمليات السحب رقم شيك.

لست واثقًا من استخدام هذا النهج لأنه ، كما سترى أدناه ، فإن معلمة "الوراثيات الأخرى" عديمة الفائدة تمامًا في سيناريو واحد. والسبب الوحيد الذي جعلني مثل هذا هو "إظهار" استخدام نمط الإستراتيجية.

(بالنسبة لأولئك الذين يشعرون بالقلق ، هذا جزء من مشروع مدرسي ، كل الكود أدناه الذي كتبته وأنا فضولي بشأن ما إذا كانت هناك طريقة أفضل لإنجازه).

هذا ما فعلته حتى الآن:

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

هناك بعض تكرار الكود. نظرًا لأنه من واجباته ، فليس هناك الكثير مما يمكنك القيام به. إذا كانت مكالمتي رغم ذلك ، فسأجرأ تلك الحقول مباشرة.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top