Ion Todirel wrote:
how about using an abstract class? why is that method called CreateInstance, are all those classes supossed to be singletons?
They weren't supposed to be singletons, just regular instances.
This is not allowed:
public abstract class DoThisBase {
public abstract void DoThis();
public abstract static DoThisBase CreateInstance();
}
This is also not allowed:
public abstract class DoThisBase {
public abstract void DoThis();
public virtual static DoThisBase CreateInstance() {
throw new NotImplementedException();
}
}
This is allowed.
public abstract class DoThisBase {
public abstract void DoThis();
public static DoThisBase CreateInstance() {
throw new NotImplementedException();
}
}
But then, to override CreateInstance in an implementation, we must hide the base class' static method with the "new" modifier.
public class DoThisImpl : DoThisBase {
public override void DoThis() {
}
public new static DoThisBase CreateInstance() {
return new DoThisImpl();
}
}
That defeats the purpose of putting the static method in the abstract base class though.