Author: sushengmiyan
Address: http://blog.csdn.net/sushengmiyan/article/details/18992741
Open the IQQ project, open the class IMApp in the iqq. app package, find the main method of this class at the program entry, and you can see the following code:
Public static void main (String [] args) {LOG.info (System. getProperty ("java. vm. name ") + System. getProperty ("java. version "); IMApp. me (). startWin (); // display the SwingUtilities Program LOGO. invokeLater (new Runnable () {public void run () {IMApp. me (). startup () ;}}); IMApp. me (). endWin (); // remove startup LOGO}
That is, the main method of the program. The first sentence is to use LOG to record the LOG information. For details about this, you can find information related to log4j and how to use it, this is an open-source project mainly used to debug and output log information for programmers.
Log4j Baidu encyclopedia explanation: http://baike.baidu.com/view/25347.htm
Wikipedia-http://en.wikipedia.org/wiki/Log4j
Apache Official Website: http://logging.apache.org/log4j/1.2/
The following is the application of the singleton mode,
/** Singleton */private static final IMApp instance = new IMApp (); public static IMApp me () {return instance ;}
Call the singleton object me through the IMApp class. Here we will talk more about the single-sample mode, java Singleton mode, because I am also a beginner in the java design mode, so I am not able to understand it ~
There are three Singleton models, such as IQQ, which is a hungry singleton. Its prototype is as follows:
public class Singleton{ private static Singleton singleton = new Singleton(); private Singleton() { } private Singleton() { } public static Singleton getinstance() { return singleton }}
There is also a lazy Singleton with the following code:
public class Singleton { private static Singleton singleton; private Singleton { } public static synchronized Singleton getinstance() { if (singleton = null) { singleton = new Singleton(); } return singleton; }}
Advantages of Singleton mode:
1. There is only one object in the memory, saving memory space
2. Avoid frequent object creation and destruction to improve performance.
3. Avoid multiple usage of shared resources.
4. Global Access
Applicable scenario: because of the advantages of the singleton mode, it is a design mode that is used in programming.
I have summarized the scenarios that I know are suitable for the singleton mode:
1. objects that need to be instantiated frequently and then destroyed.
2. objects that consume too much time or too much resources but are frequently used.
3. stateful tool objects
4. objects that frequently access databases or files
5. scenarios where only one object can be created with special requirements.
Considerations for using Singleton objects
1. Only the method provided by the singleton class can be used to obtain the singleton object. Do not use reflection. Otherwise, an object will be instantiated.
2. Do not perform dangerous operations to disconnect a singleton class object from static reference in the class.
3. Pay attention to thread security when using resource sharing in a multi-threaded use case.
We can see that the singleton mode is used in IQQ, and it is similar to the first Hunger Singleton mode.