StarTutorial.com: Understanding Design Patterns - Singleton
The StarTutorial.com site has continued their introductory look at design patterns and using them in PHP with a new article covering the Singleton pattern.
[The Singleton pattern] ensures a class has only one instance, and provides a global point of access of it.
They use a more real-world example to illustrate the use of a singleton: organizing patients at a doctor's office using a ticketing system. In their example, the system makes use of a "TickerPrinter" object to represent the ticketing system. The issue is that the printer has to check every time to see if it needs more paper or if the ink is out in the first example. They refactor this to use a singleton, creating a getInstance
method to return the same instance of a "TicketPrinter" object each time it's called rather than having to create a new one and manage it individually.