Today, I will repeat the Singleton mode to share my previous questions with you. The Singleton mode is one of the simplest design patterns, but it has some problems and defects. The following describes some issues that need to be paid attention to when using the order example mode. In some cases, the use of Singleton mode can save resources, but the intention of Singleton mode is not just to save resources.
Today, I will repeat the Singleton mode to share my previous questions with you. The Singleton mode is one of the simplest design patterns, but it has some problems and defects. The following describes some issues that need to be paid attention to when using the order example mode.
1 .? Must the instance pointer be set to static?
Because the GetInstance method uses the instance pointer and the GetInstance method is static, the pointer must be static. otherwise, GetInstance cannot access the instance pointer. This ensures that the instance pointer of the same memory zone is provided to other objects.
2. why don't we simply use the hunger style instead of the lazy style?
First of all, lazy is a typical example of time-for-space. it is to judge every time an instance is acquired to see if it is necessary to create an instance, which wastes the time for judging. Of course, if no one has ever used it, it will not create an instance, which is to save space. The hunger style is typical in exchange for space for time. that is to say, when a class is loaded, an instance is created. no matter whether you use it or not, you do not need to judge it every time you call it, saves running time.
In this case, some methods must be better than the other methods. they have their own advantages. The key depends on your choice of time and space efficiency.
3. is Singleton only used to save resources?
The first thing to note is that in some cases, using the Singleton mode can save resources, but the intention of Singleton mode is not only to save resources, if you use Singleton mode only to save resources, it may cause abuse of Singleton mode. The Singleton mode is used to ensure that there is only one instance throughout the application for specific purposes. For example, in a windows operating system, when multiple threads need to simultaneously create, open, and modify a file, the File Manager is designed in Singleton mode. All file operations must be performed on the same unique instance to avoid confusion.
4. what are the disadvantages of Singleton mode?
- It is difficult to expand because the GetInstance static function cannot generate a subclass instance. To expand, only the class is rewritten.
- Implicit use causes the class structure to be unclear. For example, sometimes you do not know that A class A is A singleton class. when you read Class B, you may first read its header file or the content in the class view, from here, you cannot know the relationship between A and B, because Class B uses the so-called GetInstance function of class A during implementation and cannot read this line, you will know the dependencies between Class B and Class.
- Causes program memory leakage. Many people call GetInstance to generate a unique instance, but new is always encapsulated in GetInstance and forget to release the memory.
5. under what circumstances cannot I use the Singleton mode?
The Singleton mode is easy to use, but it is also the most abuse mode in all design modes. You cannot use the Singleton mode when your class wants to be well extended .?
Maybe you do not have to make sure that there is only one instance at the beginning of your program. if you only use it to save resources, use it with caution, because with the delay of time, your program may need to be extended.
This article is available at http://www.nowamagic.net/librarys/veda/detail/1776.