getting db connection through singleton class

前端 未结 6 1781
Happy的楠姐
Happy的楠姐 2020-11-30 02:38

I have created an singleton class, this class returns a database connection. So my question is this connection is also satisfying singleton criteria?
If no, than how I c

6条回答
  •  孤街浪徒
    2020-11-30 03:26

    Singleton means that the class that you have made can be instantiated only once. So if you want that to happen, do two things:

    1. Make the constructor private.(This is to prevent other classes from accessing it.)
    2. instantiate the class as:

      get
      {
       if(instance == null) //important coz, the class will be instantiated only on the first call
       {
         instance = new singletonDb;
       }
       return instance;
      }
      

提交回复
热议问题