Single Pattern is a very simple mode, this is my first understanding and can be used. Sometimes the complexity of the model is not itself, but because of his application purpose. In the first time, I faced a pattern is often full of confusion. Why is a simple call to do so complicated? Why do you have so many classes, just to open a file.
Usually to learn a model is an acceptance, approve, and understanding. Accept: Understand the structure of the pattern, understand the meaning of the instance; recognize the role and feasibility of this mode in the actual project; understanding: Apply mode to the development process.
The application purpose of the pattern is nothing more than to reduce the degree of coupling between the modules between time and space, to improve the demultiplexability of the module, and reduce the probability of error.
Single-piece mode
Singleton mode is a simpler mode. The following code can create an example of a SINGLTON mode, which is a class that write system logs.
Public class logwriter {// Statement a static variable, type itself private static logwriter _INSTANCE = null; // Privatizes the constructor of the class, so that this class cannot be created by the outside private logwriter ()} // Static method, instance of creating classes public static logwriter getInstance () {if (_instance == null) {_instance = new logwriter ();} return_instance;} // The following implementation // ....
The caller cannot get Singleton's instance, the example of call is as follows:
Logwriter log = logwriter.getInstance (); log.debug ("Hello World");
Actual application
It is easy to imagine that single-piece mode is suitable for the following: an object in the entire system is unique, or there is a fixed number. For example, database connections, configuration files, etc ...
The mode is a programmer's agreed term, and the language can become the foundation of thinking. With such a language, some complex concepts become easy to communicate. For example, the designer as long as it is said, a certain class is a single mode, a Singleton, and the programmer should at least understand the call mode of this class without any detailed description.