Single instance without singleton pattern

时间:2015-07-28 15:47:32

标签: c#

Kindly suggest me the best pattern which will suits the below need.

Class Base
{
    List<string> a;
    List<string> b;   
    public Base()
    {
        //"Calling Base"
    }
}
Class Der : Base
{
    GetListA()
    {
        return a;
    }
    GetListB()
    {
        return b;
    }
}
Class Der1 : Base
{
    GetListA()
    {
        return a;
    }
    GetListB()
    {
        return b;
    }
}

Main()
{
    Der1 obj1 = new Der1();
    Der obj= new Der();
    obj.GetListA();
    obj.GetListB();
    obj1.GetListA();
    obj1.GetListB();
}

when I use obj1 or obj2, currently the base class constructor is getting called every time a new object is created.

Base class is generating say 10k records. I want that to be generated only once and keep it for others to reuse it.

Its purely web server application and my application wont accept any static variables/object or singleton class. Without that I need to perform this task.

Is there any way to achieve this??

2 个答案:

答案 0 :(得分:0)

Something similar to using a static variable is to use the Application property

You could use a property like this to void creating multiple instances of your class

public SomeClass Instance
{
     get
     {
          if (Application["SomeClassInstance"] == null)
          {
               Application["SomeClassInstance"] = new SomeClass();
          }
          return (SomeClass)Application["SomeClassInstance"];
     }
}

答案 1 :(得分:0)

activity