Design Pattern _ Singleton pattern
Singleton Pattern Ensure a class has one instance, and provide a global point of access to it. Make sure that a class has only one instance, and instantiate it and provide the instance to the entire system.
There is only one emperor. First, let's think about how to obtain an object, which is generally implemented through new. Advanced Reflection allows us to prohibit external creation of objects. Answer: set the constructor to private
Advantage: reduces memory spending. An object needs to be created frequently and destroyed. This avoids the multi-usage of sub-classes, sets global access points, and optimizes and shares resource access. Disadvantage: poor scalability. I think it is its advantage. Use Case: generate a unique serial number. A shared access point or shared data tool class is required for the entire project.
Comparison between hungry Chinese and lazy Chinese
Public class Emperor {private final static Emperor emperor = new Emperor (); private Emperor () {} public static Emperor instance () {return emperor;} public void order () {System. out. println (the purpose of my emperor );}}
Public class Emperor {private final static Emperor emperor = null; private Emperor () {} public static synchronized Emperor instance () {if (emperor = null) {return new EmperorII () ;}else {return emperor ;}} public void order () {System. out. println (the purpose of my emperor );}}
Comparison: the main problem to be considered in the hungry Chinese Style is the efficiency problem. When classes are loaded to instantiate objects, memory is required, which naturally increases the JVM burden. The Lazy Chinese Style solves the above problem and delays loading, however, you must note the thread synchronization problem in multiple threads.
The following is an application in the previous project. Unfortunately, I didn't understand it at the time. Now I understand it.
Import java. io. IOException; import java. io. inputStream; import java. util. properties; // Singleton mode: This mode ensures that there is only one instance for a class in the system, and this instance is easy to be called by the outside world. The advantage is to save system resources // if you want to have only one class object in the system, you can choose to use the singleton public class Myproperties extends Properties {private static Myproperties mypro = null; // The core of the Instance/*** provided externally is the privatization of the constructor */private Myproperties () {// ratio db. read all the configurations in properties, // obtain the resource file db in the classpath class path through the reflection instance of the class. properties, and create a stream InputStream is = this. getClass (). getResourceAsStream (/db. properties); try {this. load (is); // transforms the document into a properties structure, from the input stream} catch (IOException e) {e. printStackTrace (); LogUtil. log. error (e. toString ();} finally {try {is. close ();} catch (IOException e) {e. printStackTrace (); LogUtil. log. error (e. toString ();} // close the stream}/*** provides a method for obtaining a unique instance. * @ return */public static Myproperties getMyproperties () {if (mypro = null) {mypro = new Myproperties () ;}return mypro ;}}
/*** Method for obtaining database connections * @ return: returns the obtained Connection */public Connection getConnection () {try {con = DriverManager. getConnection (Myproperties. getMyproperties (). getProperty (url), Myproperties. getMyproperties ();} catch (SQLException e) {e. printStackTrace (); LogUtil. log. error (e. toString ();} return con ;}
I 've been playing LOL for the last two days. I 've learned a little bit about it and it's not actually used. Fortunately, the status has been adjusted recently. keep going!