Often, a system only needs to create one instance of a class, and that instance will be accessed throughout the program. Examples would include objects needed for logging, communication, database access, etc. You could pass such an instance from method to method, or assign it to each object in the system. However, this adds a lot of unnecessary complexity.
Or, you could make that single instance the responsibility of the class, and then query the class for the instance.
The singleton pattern is one of the simplest design patterns: it involves only one class which is responsible to instantiate itself, to make sure it creates not more than one instance; in the same time it provides a global point of access to that instance. In this case the same instance can be used from everywhere, being impossible to invoke directly the constructor each time.
Filled Under
Design Patterns
What is Singleton Pattern : Definition
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.