نمط طريقة القالب-منع استدعاءات الطريقة المباشرة في الفئات المشتقة

StackOverflow https://stackoverflow.com//questions/12662776

سؤال

لست متأكدا مما إذا كنت أفهم نمط طريقة القالب بشكل صحيح.

هنا هو بلدي مبسطة تنفيذ الطبقة الأساسية:

public abstract class AlgorithmBase
{
    protected void AlgorithmMethod()
    {
        if(!OnSimulationStart())
        {
            OnSimulationEnd(false);
            return;
        }

        if(!DoSomeStep())
        {
            OnSimulationEnd(false);
            return;
        }

        OnSimulationEnd(true);
    }

    protected abstract bool OnSimulationStart();
    protected abstract bool DoSomeStep();
    protected abstract void OnSimulationEnd(bool result);
}

بقدر ما أفهمها ، تعرف الطبقة الأساسية تدفق الخوارزمية وتديرها.المشكلة هي أنه في المشروع الحقيقي لدي العديد من الأساليب المجردة وسيكون من الجميل إذا كان بإمكاني بطريقة أو بأخرى منع الاتصال المباشر لهم في الطبقات المشتقة.إنه غير قابل للقراءة عندما تدير أكثر من فئة تدفق الخوارزمية.

هل كانت مفيدة؟

المحلول

يمكن استخدام خدعة تستند إلى التنفيذ الصريح للواجهة لمنع الاستدعاءات العرضية للطريقة التي يتطلبها تنفيذ الخوارزمية الأساسية.ومع ذلك ، فهو إجراء أمان يمكن كسره ، ولكن هناك احتمالات كبيرة بأن يعرف المطور ما سيفعله.

واجهة تعلن الأساليب المطلوبة من قبل AlgorithmMethod:

public interface IAlgorithmMethodImpl
{
    bool OnSimulationStart();
    bool DoSomeStep();
    void OnSimulationEnd(bool result);
}

قاعدة الطبقة المجردة التي تستخدم هذه الواجهة ، مرت في منشئ لها ، لاستدعاء الأساليب المطلوبة:

public abstract class AlgorithmBase
{
    protected AlgorithmBase(IAlgorithmMethodImpl impl)
    {
        Impl = impl;
    }

    // can be a property reasonable cases; however, a field 
    // fits well into our scenario
    private IAlgorithmMethodImpl Impl; 

    protected void AlgorithmMethod()
    {
        if(!Impl.OnSimulationStart())
        {
            Impl.OnSimulationEnd(false);
            return;
        }

        if(!DoSomeStep())
        {
            Impl.OnSimulationEnd(false);
            return;
        }

        Impl.OnSimulationEnd(true);
    }

    // no abstract method declarations here — they are provided by 'Impl'
}

ثم فئة الخوارزمية المحددة التي ترث من AlgorithmBase يستخدم تنفيذ واجهة صريحة لتغليف تنفيذ الأساليب اللازمة (مثل مع أساليب مجردة أعلن في القاعدة) فئة مع منع استدعاءها عن طريق الخطأ:

public class MySuperAlgorithm : AlgorithmBase, IAlgorithmMethodImpl
{
    public MySuperAlgorithm()
        // pass a reference to this instance as the class 
        // that implements the required methods
        : base(this) 
    {
    }

    // explicit implementation of IAlgorithmMethodImpl
    bool IAlgorithmMethodImpl.OnSimulationStart() { ... implementation ... }
    bool IAlgorithmMethodImpl.DoSomeStep() { ... implementation ... }
    void IAlgorithmMethodImpl.OnSimulationEnd(bool result) { ... implementation ... }
}

وميزة هذا النهج-إلى جانب منع استدعاء عرضي من أساليب التنفيذ-هو أنه يمكنك اختيار ما إذا كان تغليف التنفيذ في سليل ، أو لتحللها إلى فئة منفصلة.

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