Understanding the singleton pattern
- Definition:
- There’s only one instance
- Global access
- Main solutions:
- A globally used class that is frequently created and destroyed
- When to use:
- When you want to control the number of instances, save systematic resource release
- How to achieve:
- Check whether the system already has this singleton. If so, return it. If not, create it
Advantages of the singleton pattern:
- Having only one instance in memory reduces memory overhead, especially frequent creation and destruction
- Usage Scenarios:
- Global cache
- Popup window
Definition of policy patterns
- Define a set of algorithms, encapsulate them, and interchangeable them
- Core:
- Separate the use of algorithms from the implementation of algorithms
Publish and subscribe model
- Start by figuring out who the publisher is
- Then add a cache list to the publisher to hold callback functions to notify subscribers
- Finally, a message is published, and the publisher iterates through the cache list, triggering the deposited subscriber callback in turn