Under what circumstances should I use the Singleton class?

Closed as an exact duplicate of this question . But open again, as other Singleton issues are for general use and are not used to access the database

I was thinking of creating an internal Singleton data access class, but I could not convince myself of this choice mainly because the class has no state other than local variables in its methods.

What is the purpose of creating classes like Singletons?
This requires sequential access to the database, which is not convincing since most modern databases can handle concurrency well?
Is it possible to reuse a single connection that could be resolved by pooling? Or is it saving memory by running one instance?

Please enlighten me on this.

+5
source share
11 answers

I found that the singleton pattern is suitable for a class that:

  • Has no state
  • Full of basic "Service Members"
  • You need to tightly control your resources.

An example of this would be a data access class.

, , , , DataReader, singleton, .

, ( ) , ( ), , .

, , Singleton .NET , , . .

+3

, Singleton . DBD/DBI .

,

Rob

+4
+3

" : - ":

ahve . , . ....

Singleton, :

  • ,
  • ,

, - , Singleton, -; , ( ), concurrency (-) .

+3

- , . Singleton - , , , . Singleton, Singleton , .

: java.sun.com

+1
  • ,
  • concurrency
+1

Singleton , - .

- , , "" ? - , .

, . - , ; "", , , , .

, . , Java "singleton", readResolve() Serializable.

Singleton factory, ; , , , API.

Java.

+1

, , .

, . . singleton. ( ).

:

  • IOC, , (StructureMap, Unity, Spring.Net, Castle Windsor, Autofac, Ninject... ).
  • .
  • IOC , .
  • .

. .

+1

.

+1

, ; , , ( ) .

, , ; .

, - , , Singleton, , accessor, , accessor.

, - singleton:

  • Factory: , .
  • : , , . ( bumpo , .)
  • Concurrency ; , , singleton, P/V .
+1

#, , . singleton . , - . , , , . . , , , . / ORM .

# 3.5 , .

0

All Articles