开发者

Create instance of the class from base class

开发者 https://www.devze.com 2023-03-23 08:01 出处:网络
I have DataAccess class which is derived from DataAccessor. DataAccessor class is the DB base class which I use in all all projects.

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 abstr开发者_开发问答act class DataAccessor 
{
}


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

public class Derived : Base<Derived>
{
}


Have you considered the Abstract Factory Pattern?

0

精彩评论

暂无评论...
验证码 换一张
取 消