Skip to main content
added 56 characters in body
Source Link
TorbenPutkonen
  • 7.9k
  • 20
  • 28

There are already links on this page to reasons why singletons should not be used. I will not repeat those.

is it a good way to provide a singleton object from 3rd party library?

No. Your singleton has an undocumented internal state which affects the processing of the input parameters. On the first invocation, the getInstance(String connectionString) methods returns a connection to the database requested by the caller. On subsequent invocations it returns a connection to the database requested by the first caller, regardless of what connection string is provided to it. You could document it but it doesn't fix the issue of this being a very error prone solution: your method does something unexpected if something that cannot be verified during run time happened before it was called.

There are already links on this page to reasons why singletons should not be used. I will not repeat those.

is it a good way to provide a singleton object from 3rd party library?

No. Your singleton has an undocumented internal state which affects the processing of the input parameters. On the first invocation, the getInstance(String connectionString) methods returns a connection to the database requested by the caller. On subsequent invocations it returns a connection to the database requested by the first caller. You could document it but it doesn't fix the issue of this being a very error prone solution: your method does something unexpected if something that cannot be verified during run time happened before it was called.

There are already links on this page to reasons why singletons should not be used. I will not repeat those.

is it a good way to provide a singleton object from 3rd party library?

No. Your singleton has an undocumented internal state which affects the processing of the input parameters. On the first invocation, the getInstance(String connectionString) methods returns a connection to the database requested by the caller. On subsequent invocations it returns a connection to the database requested by the first caller, regardless of what connection string is provided to it. You could document it but it doesn't fix the issue of this being a very error prone solution: your method does something unexpected if something that cannot be verified during run time happened before it was called.

Source Link
TorbenPutkonen
  • 7.9k
  • 20
  • 28

There are already links on this page to reasons why singletons should not be used. I will not repeat those.

is it a good way to provide a singleton object from 3rd party library?

No. Your singleton has an undocumented internal state which affects the processing of the input parameters. On the first invocation, the getInstance(String connectionString) methods returns a connection to the database requested by the caller. On subsequent invocations it returns a connection to the database requested by the first caller. You could document it but it doesn't fix the issue of this being a very error prone solution: your method does something unexpected if something that cannot be verified during run time happened before it was called.