The so-called single case pattern is a class has and only one instance;
The single case model, also known as the single piece mode, the list mode, may be the most widely used design pattern. The intention is to ensure that a class has only one instance and provides a global access point to access it, which is shared by all program modules. There are many places need such functional modules, such as the system log output, GUI application must be a single mouse, modem connection needs one and only need a telephone line, the operating system can only have a window manager, a PC with a keyboard.
There are many ways to implement a single case pattern, and in C + + it can even be done directly with a global variable, but the code is not elegant. Using global objects ensures easy access to instances, but it is not guaranteed to declare only one object-that is, a local instance of the same class can still be created except for a global instance.
Object Instantiation method, is also more, the most common method is to use the new directly, and this is the most common, if you want to take into account other needs, such as single-instance mode, inter-hierarchy call and so on.
The direct use of new can not achieve good design, this time need to use the indirect use of new, that is GetInstance method. This is a representation of a design style, not just a method name
1. Use of NEW:
As Object _object = new Object (), you must know that there is a second object, and that the second object is often in the current application domain.
That can be called directly.
2. Use of getinstance:
Called at the start of the primary function, returns an instantiated object that is static and retains its reference in memory, in which a region of memory is reserved for static methods and variables.
Can be used directly, the call returns the same object multiple times.
3. Comparison between the two:
Most classes (Non-abstract/interface/masked constructor classes) can be new,new by producing a new instance object, or declaring an object on the stack, and each part of the call
It's all about a new object.
GetInstance is a method with few classes, and their implementations are different.
GetInstance is common in a singleton pattern (which guarantees that a class has only one instance and provides a global access point to it), and is used to generate a unique instance, and getinstance is often static.
(1) The object can be obtained by getinstance without its own definition, and no delete is needed after use;
(2) New must be generated to create an object, allocating memory, getinstance () does not necessarily have to be created again, it can put an existing reference to you to use, which is superior to new in efficiency;
(3) The new creation can only be used once, and getinstance () can be used across the stack area or remotely across regions. So getinstance () is usually the creation of static statically instance methods.
Summarize:
GetInstance This method is used in a singleton pattern, in order to avoid wasting memory, until it needs to instantiate the class to instantiate it, so use getinstance to get the object,
As for other times, that is, for simplicity, in order not to let the program in the instantiation of objects, do not use the new keyword every time, simply provide a instance method, do not have to perform this class on the initialization, so do not waste system resources. The single case pattern protects against data conflicts and saves memory space.
[HTML] view plain copy//usages, add the getinstance () method to the defined new class, the return value is an object of the class, and this method is often applied to the Java singleton pattern in the public class oauthconstant {public static synchronized Oauthconstant getinstance () {if (instance = null) Instance = new Oauthconstant (); return instance; The getinstance () function of this class is used in the main function to get the class object that is currently instantiated by the system, and if the current system has not yet instantiated the object of this class, call the constructor//instantiate an object and return oauthconstant.ge Tinstance ();