Jonathan
Jonathan

Reputation: 1296

Same template method interface applied on base and derived class

I have a little difficulty to explain the problem clearly in words so i'll start with the code immediately:

public interface ITemplate
{
    bool DoSomething(string a);
}

public class Worker
{
    public void DoSomeWork(ITemplate subcontractor)
    {
        try {
            //Do some work...
            if(subcontractor.DoSomething("hello"))
            {
                //Do some more work
            }
        }
        catch(InvalidOperationException) {/*...*/}
        //catch(Exception2) {/*...*/}
        //catch(Exception3) {/*...*/}
        //catch(Exception4) {/*...*/}
    }
}

public class BaseClass : ITemplate
{
    public void myFunction1()
    {
        Worker worker = new Worker();
        worker.DoSomeWork(this);   
    }

    public bool DoSomething(string a)
    {
        //Some code here
        return true;
    }
}

public class DerivedClass : BaseClass, ITemplate
{
    public void myFunction2()
    {
        Worker worker = new Worker();
        worker.DoSomeWork(this);   
    }

    public bool DoSomething(string a)
    {
        //Some other code here
        return true;
    }
}

With this structure, if i call myDerivedClass.myFunction1(), the worker.DoSomeWork() function will execute DerivedClass.DoSomething(), but i want BaseClass.DoSomething() to be executed in that context, how can i do ?

Maybe it is not possible to achieve what i'm trying to do in that context with the template method design pattern.

The initial problem was that i need a method to execute code, but some part of this code is code is variable according on what object called the method. Is there another way to do that without if(typeOf(subcontractor) == ...) which is not a very good practice, i would like the variable part of the code to be stored and executed inside the caller object.

Could i pass a delegate or something in the parameter instead of sending the whole caller object ?? i'm not very used to work with delegate/etc but i think it might be helpful for my problem, am i right ?

thanks Jonathan

Upvotes: 0

Views: 68

Answers (2)

Maess
Maess

Reputation: 4166

DerivedClass is an ITemplate since it inherits from BaseClass which is an ITemplate. You don't need DerivedClass to directly implement ITemplate.

Also, as said above, the code you posted won't compile.

public interface ITemplate
{
    bool DoSomething(string a);
}

public class Worker
{
    public void DoSomeWork(ITemplate subcontractor)
    {
        //Do some work...
        if(subcontractor.DoSomething("hello"))
        {
            //Do some more work
        }
    }
}

public BaseClass : ITemplate
{
    public void myFunction1()
    {
        Worker worker = new Worker();
        worker.DoSomeWork(this);   
    }

    public bool DoSomething(string a)
    {
        //Some code here
    }
}

public DerivedClass : BaseClass
{
    public void myFunction2()
    {
        Worker worker = new Worker();
        worker.DoSomeWork(this);   
    }
}

Upvotes: 0

odyss-jii
odyss-jii

Reputation: 2729

That is the correct behavior and is how polymorphism works. If you have an instance of the derived class DerivedClass and you have overridden a method, say DoSomething, then all calls to the method DoSomething in the code of the base class will go through the overridden method in the derived class if dealing with an instance of the derived type.

If you wish the code in the base class to be executed, then you need to either not override the method, or call the base class method from the derived class:

public class BaseClass
{
    public virtual void MyMethod() 
    { 
        // Do some stuff...
    }
}

public class DerivedClass : BaseClass
{
    public override void MyMethod()
    {
        // Do something new...

        // Do the stuff in BaseClass.MyMethod()
        base.MyMethod(); 
    }
}

Upvotes: 1

Related Questions