Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
It is common in application design to have a collection of objects that need to be unique but at the same time are not singletons. For example, consider fonts. Because an application may use many different fonts, the object used to encapsulate a font will probably be instantiated multiple times and therefore is not a singleton. At the same time, there is no need for multiple instances all encapsulating the same exact font. It makes sense to control the creation of font instances so that whenever a specific typeface is requested, the same instance is returned.
One approach is to use class methods to ensure that all instances of an object are unique. For example, if an NSImage is obtained using the +imageNamed: class method, the same instance is returned every time a particular name is requested. Associative Storage, Chapter 19, “Associative Storage,” is commonly used to keep track of the named instances. One possible implementation for a method like this uses an NSMutableDictionary to track each object as it is created: