Java-based creation design mode-singleton Mode
I. Description
The Singleton mode ensures that a class has only one instance object and must be created automatically on the instance object. The constructor of this object is not provided externally and the instance is provided to the entire system. The Singleton mode is equivalent to the instance objects shared by the entire system. It is of great significance for concurrent access in java.
The Singleton mode is divided into two types: the hungry and full modes. The hungry mode creates and initializes the unique instance objects of the class when the class is loaded, the full Chinese Mode creates the object only when the system needs to use the instance of this class.
Ii. Advantages and Disadvantages of Singleton Mode
Advantage: in Single-Column mode, a client can only call one public interface when calling a class instance, which provides a shared object for the entire system. Because only one object exists in the system memory, system resources can be saved, which undoubtedly improves the efficiency compared with the classes of objects that require frequent creation and destruction. In addition, the class itself controls the instantiation process, and the class has the corresponding scalability to change the instantiation process.
Disadvantages: classes that implement the singleton mode cannot be inherited by other classes after instantiation; and a class only has a unique object, so the responsibilities of this Singleton class will be too heavy, to some extent, it violates the "single responsibility principle". Class instances cannot be changed after initialization and are not suitable for scenarios where class instances change frequently. if they are in a distributed system, when the singleton class in the system is replicated to different virtual machines, an instance object is created in each virtual machine, and it is difficult to distinguish which one runs under the specific Virtual Machine. Furthermore, because instances in singleton mode are shared, additional code is required to ensure the access security during concurrent access.
Iii. Source Code
Package tong. yue. day4_25; import java. io. IOException;/*** the singleton mode is a very common design mode, that is, a class can only have one object (instance), usually by private the constructor of this class, to prevent the creation of the class objects outside the class, and provide a unique object to the outside world (this object is created in this class ). * The Singleton mode in java has two common implementation methods: one is the cool mode, that is, the Class Object uses static rest and initialization during class loading; * The other is the full Chinese formula. This object is initialized only when it is used in the program. Once initialized, this object will not be regenerated. * The Runtime class in jdk is also a singleton mode, and it uses the ELE. Me mode. * @ Author Administrator **/public class Singleton {public static void main (String [] args) {// gets the Singleton_eager singleton = Singleton_eager.getInstance () of the singleton_eager (); singleton. print (); // obtain the object Singleton_lazy singleton_lazy = Singleton_lazy.getInstance (); singleton_lazy.print () ;}/// the hungry singleton_lazy.print, create an object when the class is loaded. Then, the class Singleton_eager {// declared as static type will not be created again. When the class is loaded, create an object private static Singleton_eager single Ton_eager = new Singleton_eager (); private Singleton_eager () {}public static Singleton_eager getInstance () {return singleton_eager;} public void print () {System. out. println (singleton_eager) ;}// full-Chinese Singleton mode. When the program uses this class object for the first time, it creates an object only once, then, the class Singleton_lazy {private static Singleton_lazy singleton_lazy = null will not be created again. synchronization is required for the Creation Method of private Singleton_lazy () {}// Singleton_lazy object, this is because the security and uniformity of multi-thread concurrent access must be ensured. Public synchronized static Singleton_lazy getInstance () {// if this object is used for the first time, create one. Otherwise, use the created object if (singleton_lazy = null) {singleton_lazy = new Singleton_lazy ();} return singleton_lazy;} public void print () {System. out. println (singleton_lazy );}}Running result:
Iv. Summary
The constructor and public method of the above two schemes are static, so this class instance is shared. The object has been created when the class is loaded in the hungry Chinese style. The created instance is directly returned instead of being created during each call. This saves time, but occupies space. If the instance itself is static, it will stay in the memory.
The lazy method does not create an object when the class is loaded. Instead, it is created only once when the program uses the object of this class. Each time the program uses this instance object, it determines whether it exists, if it exists, it is loaded. If it does not exist, it is created, which will inevitably affect the speed of the program running. The most important thing is that, in the case of concurrency, the lazy mode is insecure. If two threads: thread 1 and thread 2, call the getInstance () method at the same time, if thread 1 first enters the if statement and determines that the object does not exist, the thread switches when it is ready to create the object. Then thread 2 controls and determines that if statement has no object, it will also be created, two instances will be created, so the getInstance () method in the lazy Singleton mode must be added with the synchronization controller synchronized to ensure the security of concurrent access.