2

I have DataAccess class which is derived from DataAccessor. DataAccessor class is the DB base class which I use in all all projects.

Instance method is the helper to create new instance of the DataAccess class. I would like to move Instance method to DataAccessor base class and create new insatnces of derived classes from base class. How to do that?

public class DataAccess : DataAccessor
{

    public static DataAccess Instance
    {
        get
        {
            return new DataAccess();
        }
    }
}

public abstract class DataAccessor 
{
}

2 Answers 2

3
public class Base<T> where T : new()
{
    public static T Instance
    {
        get { return new T(); }
    }
}

public class Derived : Base<Derived>
{
}
Sign up to request clarification or add additional context in comments.

6 Comments

Interesting. Can you point me at an example somewhere of something like this being used?
I can use this class to create integers. int foo = Base<int>.Instance; What about that?
@iandotkelly, personally the only thing I can think of, off the bat, is a generic singleton implementation. But we all know how bad they are :)
@Moo-Juice: this is not Singleton, this is Factory. Singleton is when you can only call new once.
@GFK, I am aware of that. iandotkelly asked why you might use something like this, obviously in the singleton case there would be a private static member and a check.
|
3

Have you considered the Abstract Factory Pattern?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.