sgtz
sgtz

Reputation: 9019

How to create instance of inherited in static base method?

From an instance, I might do this.

 var obj= Activator.CreateInstance(GetType());

Not sure how to get typeof of the inherited class in a static base method though.

Is this the best way forward?

 public static Method<T>() where T : SomeBase, new()

Upvotes: 4

Views: 1979

Answers (3)

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59131

There is no such thing as a derived static method. So there is no way to create a static factory method that returns a different type depending on which derived class you call it on.

As Lonli-Lokli suggested, you should use the Abstract Factory design pattern.

public interface ISomething
{
    void DoSomething();
}

public class SomeClass : ISomething
{
    public virtual void DoSomething() { Console.WriteLine("SomeClass"); }
}

public class SomeDerivedClass : SomeClass
{
    private int parameter;

    public SomeDerivedClass(int parameter)
    {
        this.parameter = parameter;
    }

    public virtual void DoSomething()
    {
        Console.WriteLine("SomeDerivedClass - {0}", parameter);
        base.DoSomething();
    }
}

public interface IFactory
{
    public ISomething Create();
}

public class SomeClassFactory : IFactory
{
    public ISomething Create() { return new SomeClass(); }
}

public class SomeDerivedClassFactory : IFactory
{
    public ISomething Create() { return new SomeDerivedClass(SomeParam); }

    public int SomeParam { get; set; }
}

Pros of Abstract Factory vs static Factory methods:

  • It is much more flexible, allowing a new implementation of your factory logic (which can be as complicated as you want) for every implementor of the abstract factory. You could have more than one factory per class, if you wanted.
  • Since you aren't calling a static method, it is much easier to replace at runtime. This is quite useful for injecting mocks in unit tests.

The pros are huge. Abstract Factories are superior to static factory methods in every way, even if you could get static methods to work the way you want them to.

Cons of Abstract Factory vs static Factory methods:

  • Users of the abstract factory must have a factory instance to create your derived types.
  • You have to write a new abstract factory implementation for each derived class.

The cons are very marginal.

It is extremely easy for a user to instantiate a factory to create a single object:

MyClass myClass = new MyClassFactory().Create();

As for code duplication in the factory implementation: Saving the implementer a tiny bit of typing is pointless. It is a goal in programming to write code that can be read, understood, and easily modified. There is no programming goal to save paper or keystrokes :)

Upvotes: 2

Steve Rowbotham
Steve Rowbotham

Reputation: 2868

You could make the base class generic and close the generic in the derived class.

public abstract class CreatorOf<T> where T : CreatorOf<T>
{
    public static T Create()
    {
        return (T)Activator.CreateInstance(typeof(T));
    }
}

public class Inheritor : CreatorOf<Inheritor>
{
    public Inheritor()
    {

    }
}


public class Client
{

    public Client()
    {
        var obj = Inheritor.Create();
    }
}

There are some who consider this to be an "anti-pattern", but I believe there are circumstances where it is an acceptable approach.

Upvotes: 4

Lonli-Lokli
Lonli-Lokli

Reputation: 3766

Maybe you should better tryto use abstract factory pattern? http://en.wikipedia.org/wiki/Abstract_factory_pattern

Upvotes: 3

Related Questions