当前位置:网站首页>07 singleton mode

07 singleton mode

2022-07-06 02:38:00 zzyzxb

The singleton pattern / Singlet mode : Create pattern , Create a class that produces only one object instance ---- Use private constructor to realize the definition of class .

One : Basic concept and implementation of singleton class

GameConfig,Scott Meyers“ Make the interface or type easy to use correctly , Difficult to misuse ”
Only one class belonging to this class can be created, which is called singleton class .
(2) Problems that a singleton class can cause in multithreading
A good solution to multithreading creation GameConfig The method of class object problem is in main Main function ( Program execution entry ), Before creating any other threads ,
Do it first GameConfig::getInstance(); Let's put this alone GameConfig Class object is created .
(3) Hungry and lazy
(4) Memory release problem of singleton class object
(5) Single piece class definition 、UML Figure and another implementation method
Single piece design pattern definition : Ensure that only one instance of a class exists , At the same time, it provides a global method that can access the instance (getInstance Member functions ).
Troubled problems : There are two problems in the cross reference between multiple singleton classes . Log、GameConfig—— Do not reference other singleton objects in the destructor of singleton class .

原网站

版权声明
本文为[zzyzxb]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060233502700.html