Design pattern - Singleton
- algboost
- Apr 26, 2015
- 1 min read
Problems to solve
- Application needs one instance of an object. ANd it needs initilazation and global access as well.
- E.g., One executable links a static libary (say core.lib) ; and links to a dynamic libary which links to core.lib as well.
There are potential issues (risks) if there are no singleton designed here.
- Thread safe
Key points in code design
- Lazy initilization (creation at first use)
- Define a private static attribute in the "single instance" class.
- Define a public static accessor function in the class.
Final words
- Balance of exposure and protection for an object is critical for maintaining flexibility.
More reference:
Comments