Please explain this pattern when using the abstract method.

I saw 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 do not understand the practical difference why it is not just written as:

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

UPDATE: The above snippet taken from the documentation for Ninjectwhere the interface is not specified, but looking at the actual source, I see that it SimpleProvider<T>implements an interface IProviderthat explains the need for a subtitle and the answers to my question.

+3
source share
5 answers

So the only difference is the type of return value (Object instead of T), which means that the caller requires actors.

, , - object Create(IContext context);

+2

, , T, , .

factory, .

+2

, , SimpleProvider Create(IContext context). , T, .

+1

, , , , =)

0

Keeps that the caller must know T at compile time. It also supports interface consistency throughout the class hierarchy, since the public method is separated from any specific subclass implementations.

0
source

All Articles