Membership
Main Menu
Forum Boards
Stats
- 20 tutorials
- 74,816 members
- 734,946 forum posts
- 13 blog posts
Tutorials
Design Patterns - Singleton and Singleton Registry
Views: 5702
Introduction
The Singleton pattern is probably the most famous, or should I say infamous, software Design Pattern in existence. If you have read OO PHP Part 2, possibly you will know why it is so hated by some after reading about its implementation, in any case, I'll discuss it briefly later in this chapter.
The Singleton pattern ensures that you are always dealing with the same, single instance, wherever in your application.
The Registry pattern usually utilizes the Singleton pattern (hence “Singleton Registry”) to make the same ‘globalness’ apply to objects who’s classes weren’t necessarily designed to, as well as to provide a central access point for often reused objects.
But in it's purest form, as simply a way for clients to find common objects, a Registry doesn't need to be Singleton. Use of Registry objects local to a specific part, layer or domain of an application is common as well. In this function, a Registry is little more than a well-known hash map to store references to common objects. We won't be going into that right now. It's pretty straight forward anyway.
Problem
- How to ensure that only a single instance of a class is available, and make it easily accessible?
- How to create a well-known service to fetch commonly used objects from, while avoiding excessive passing of objects?
