On design patterns: When should I use the singleton?

后端 未结 20 2994
失恋的感觉
失恋的感觉 2020-11-22 02:45

The glorified global variable - becomes a gloried global class. Some say breaking object-oriented design.

Give me scenarios, other than the good old logger where it

相关标签:
20条回答
  • 2020-11-22 03:37

    An example with code, perhaps.

    Here, the ConcreteRegistry is a singleton in a poker game that allows the behaviours all the way up the package tree access the few, core interfaces of the game (i.e., the facades for the model, view, controller, environment, etc.):

    http://www.edmundkirwan.com/servlet/fractal/cs1/frac-cs40.html

    Ed.

    0 讨论(0)
  • 2020-11-22 03:40

    I use it for an object encapsulating command-line parameters when dealing with pluggable modules. The main program doesn't know what the command-line parameters are for modules that get loaded (and doesn't always even know what modules are being loaded). e.g., main loads A, which doesn't need any parameters itself (so why it should take an extra pointer / reference / whatever, I'm not sure - looks like pollution), then loads modules X, Y, and Z. Two of these, say X and Z, need (or accept) parameters, so they call back to the command-line singleton to tell it what parameters to accept, and the at runtime they call back to find out if the user actually has specified any of them.

    In many ways, a singleton for handling CGI parameters would work similarly if you're only using one process per query (other mod_* methods don't do this, so it'd be bad there - thus the argument that says you shouldn't use singletons in the mod_cgi world in case you port to the mod_perl or whatever world).

    0 讨论(0)
  • 2020-11-22 03:40

    You use the Singleton design pattern when you want to ensure that a class will have one instance, and that instance will have a global point of access to it.

    So let's say that you have an application that requires to a database to process CRUD operations. Ideally you'd use the same connection object to the database to access the database and perform the CRUD operations.

    Therefore, in order to ensure that the database class will have one object, and that same object will be used through out the application we implement the singleton design pattern.

    Ensure that your constructor is private and that you provide a static method to provide access to the single object of the singleton class

    0 讨论(0)
  • 2020-11-22 03:42

    On my quest for the truth I discovered that there are actually very few "acceptable" reasons to use a Singleton.

    One reason that tends to come up over and over again on the internets is that of a "logging" class (which you mentioned). In this case, a Singleton can be used instead of a single instance of a class because a logging class usually needs to be used over and over again ad nauseam by every class in a project. If every class uses this logging class, dependency injection becomes cumbersome.

    Logging is a specific example of an "acceptable" Singleton because it doesn't affect the execution of your code. Disable logging, code execution remains the same. Enable it, same same. Misko puts it in the following way in Root Cause of Singletons, "The information here flows one way: From your application into the logger. Even though loggers are global state, since no information flows from loggers into your application, loggers are acceptable."

    I'm sure there are other valid reasons as well. Alex Miller, in "Patterns I Hate", talks of service locators and client side UI's also being possibly "acceptable" choices.

    Read more at Singleton I love you, but you're bringing me down.

    0 讨论(0)
  • 2020-11-22 03:42

    Managing a connection (or a pool of connections) to a database.

    I would use it also to retrieve and store informations on external configuration files.

    0 讨论(0)
  • 2020-11-22 03:42

    Shared resources. Especially in PHP, a database class, a template class, and a global variable depot class. All have to be shared by all modules/classes that are being used throughout the code.

    It's a true object usage -> the template class contains the page template that is being built, and it gets shaped, added, changed by modules that are adding to page output. It has to be kept as a single instance so that this can happen, and the same goes for databases. With a shared database singleton, all modules' classes can get access to queries and get them without having to rerun them.

    A global variable depot singleton provides you a global, reliable, and easily usable variable depot. It tidies up your code a great lot. Imagine having all configuration values in an array in a singleton like:

    $gb->config['hostname']

    or having all language values in an array like:

    $gb->lang['ENTER_USER']

    In the end of running the code for the page, you get, say, a now mature:

    $template

    Singleton, a $gb singleton that has the lang array for replacing into it, and all output loaded and ready. You just replace them into the keys that are now present in mature template object's page value, and then serve it out to user.

    The great advantage of this is you can do ANY post-processing you like on anything. You can pipe all language values to google translate, or another translate service and get them back, and replace them into their places, translated, for example. or, you can replace in page structures, or, content strings, as you want.

    0 讨论(0)
提交回复
热议问题