Appliance design mode in Java - Singleton

xiaoxiao2021-04-08  411

Design Patterns in Java --Singleton author: Liu Zhan from: IBM This article describes the basic concepts Singleton design pattern, and a simple analysis of its function and purpose, lists several methods commonly implemented Singleton and A detailed Java code is given. Basic Concept Singleton is a creative model that ensures only an instance and provides a global access point to access it. For some classes, it is important to ensure that only one instance is very important. For example, if some are, the database connection or Socket connection is subject to certain limits, and you must have only one connection to the existence of a connection at the same time. An example is given, and the duplicate element is not included in the set. Add to SET The object must be unique. If the duplicate value is added to the set, it only accepts an instance. In the format using the Singleton mode in JDK to implement this feature, you can view the internal static class of Java.util.collections. Singletonset Original code. In fact, Singleton is one of the easiest but also one of the most widely used patterns. It can be seen everywhere in JDK. Simple analysis To implement Singleton mode, we need a static variable, you can remember without creating objects Whether it has been generated. Static variables or static methods can be directly called without a specific instance, such variables or methods do not change because of the instantiation of the class. In the structure of the classes, it can be seen in the structure of Figure 1. At, UNIQUEINSTANCE is this independent static variable, which can remember if the object has been instantiated, and the variable is judged in the static method instance. If there is no instantiation, a new object is generated. If it has been instantiated, Another new object is still returned. Figure 1: Singleton mode structure The implementation of the Singleton mode is usually three. 1. Implementing the Singleton using a static method to use a static method to monitor the creation of the instance. To prevent more than one instance, we'd better declare the constructor as private. This prevents the customer programmer from creating an instance by any way we provide, if you do not declare the constructor as Private, The compiler will automatically synchronize a default Friendly constructor with a smart automatic synchronization. This implementation method is the most common, that is, the standard implementation of the structure in Figure 1. Public class singleleton {private static Singleton S; Private Singleton () {}; / ** * Class method of the access the singleton instance of the class. * / Public static Singleton getInstance () {if (s == null) s = new singleleton (); returnof ;}} // Test class class singleletontest {public static void main (string [] args) {Singleton S1 = singleton.getInstance (); Singleton S2 = Singleton.getInstance (); if (S1 == S2) System.out. Println ("S1 Is The Same Instance With S2"); Else System.Out.println ("S1 IS Not The Same Instance with S2");

}}} SINGLETONTENTANTAST Run S2 is: S1 is The Same Instance With S2 This proves that we only created an instance. II. Second. Different Singleton is embedded in the class as a flag, each time you enter the constructor The problem is checked. The problem is that the constructor does not return the type, if it is determined to create an instance success or not. One method is to invoke a function to check if it is successful, then returns a value from the static variable, but this is not Elegant and easy to happen. The better approach is to create a class that can throw an exception when you create more than one instance. This class is just calling a parent method. The advantage is to use your own anomalous type, error information clearer: class SingletonException extends RuntimeException {public SingletonException (String s) {super (s);}} class Singleton {static boolean instance_flag = false; // true if 1 instance public Singleton () {if (instance_flag) throw new SingletonException ("Only One Instance Allowed"); Else Instance_Flag = true; // set flag for 1 instance}} // Test class public class singletontest {static public void main (string argv []) {Singleton S1, S2; // Create One InceTance - this Should Always Work System.out.Println ("Creating One Instance"); Try {S1 = New Singleton ();} catch (singletonexcection e) {system.out.println (E.getMessage ()); } // Try to create anothe R spooler --SHOULD FAIL System.Out.println ("CREANG TWO Instance"); try {s2 = new singleleton ();} catch (singletonexception e) {system.out.println (E.GetMessage ());

转载请注明原文地址:https://www.9cbs.com/read-132743.html

New Post(0)