Search code examples
c#oopclass-designabstract-classabstract-methods

Please explain this pattern when using abstract method


I've seen the following pattern used in many places:

abstract class SimpleProvider<T> 
{
    public object Create(IContext context) 
    {
        return CreateInstance(context);
    }

    protected abstract T CreateInstance(IContext context);
}

I don't understand the practical difference, why is it not just written as:

abstract class SimpleProvider<T> 
{
    public abstract T Create(IContext context);
}

UPDATE: The above snippet it taken from the documentation for Ninject where no interface is specified, but looking at the actual source I can see that SimpleProvider<T> implements interface IProvider which explains the need for the sub-call and answers my question.


Solution

  • So the only difference is the return type (Object instead of T), which would mean a cast is required by the caller.

    The only reason I can think of to do this is if they were implemented an interface which had object Create(IContext context);