Get an object that's no different from new. However, if you block the construction method, you cannot use new to produce the object. The goal is to get an instance of an object.
usage of getinstance ()When writing library code, sometimes a class needs to be used by all other classes, but requires that the class be instantiated only once, is a service class, defined once, and other classes use an instance of the same class.
For example:
Class A
{
Public
A (void);
.....
}
If you declare an object of Class A in every class that needs a, then there will be N classes, each class definition is different, but this class A is a service class and can only be defined once, and defining multiple n classes will result in various memory addresses.
The way to solve this contradiction:
1. Define a global class object, the other class uses this instantiation object together, so that a is instantiated only once, using extern a A to declare once, define a in the source file;
However, there is a problem in this way, that is, the access between projects, such as Pro1 in a class, Pro2 need to use Class A, so that the Class A is only instantiated once, may occur pro1 and Pro2 each instantiation.
2. Define a static instantiation class (Singleton), as follows:
Class A
{
Public
A (void);
static * getinstance ()
{
static * A = NULL;
if (!a)
{
A = new A;
};
return A;
}
......
}
So as long as this header file is included,
A::getinstance ()->
This guarantees that only one instance is instantiated.3 getinstance and newObject 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 consider other needs, such as single-instance mode, inter-hierarchy calls and so on. It is not possible to do a good design directly using new, which requires the use of the indirect use of new, the GetInstance method used by many people. This is a representation of a design style, not just a method name.3.1 1. Use of NEW:As Object _object = new Object (), it is necessary to know that there is a second object, and the second object is often in the current application domain, which can be called directly3.2 2. Use of getinstance:Called at the beginning of the main function, returns an instantiated object, which is static and retains its reference in memory, where there is an area in memory dedicated to static methods and variables that can be used directly, and called multiple times to return the same object.3.3 3. The difference between the two contrasts:Most classes (non-abstract classes/interfaces/classes that block constructor) can be used new,new either by producing a new instance object, or by declaring an object on the stack, with a new object for each part of the call.
GetInstance is a method of a small number of classes, and their implementation is different. GetInstance is common in a singleton pattern (which guarantees that a class has only one instance and provides a global access point to access it), and is used to generate a unique instance, and getinstance is often static.
(1) The object is obtained by getinstance before it is used without its own definition, and no delete is needed after use.
(2) New is bound to generate an object, allocate memory; getinstance () does not have to be created again, it can use an existing reference to you, which is better than new in performance;
(3) New creation can only be used once, while getinstance () can be used across the stack area, or remotely across regions. So getinstance () is usually created by static instance methods.