Summary is one of the simplest design patterns, but for Java developers, it has a lot of defects. In this month's column, David Geary explores how monologically mode and how to deal with these defects in the face of multithreading, classLoaders and serialization. Single-case models are suitable for a class that only one instance, such as window manager, print buffer pool, and file system, which are examples of prototype. Typically, those objects are accessed through different objects of a software system, so a global access pointer is required, which is a well-known single example mode. Of course, this is only in your case where you no longer need any examples. Single-size mode is used in the previous paragraph. You can:
Make sure that only one instance is established to provide a global access pointer to an object allows multiple instances to be in the future, although the singular design pattern is as shown in the figure below without affecting the client. It is the simplest design pattern, but there is a lot of defects for careful Java developers. This article discusses the single example mode and reveals those defects. Note: You can download the source code of this article from Resources. Single-case mode in the "Design Mode" book, the author describes the singleton mode: Make sure a class has only one instance and provides a global access pointer to it. The following figure illustrates a class diagram of a single example mode. (Figure 1) Sample mode class map is as seen in the above figure, this is not a single case mode. The single case in this figure holds a static reference to the unique single example instance and returns a reference to that instance from the static GetInstance () method. Example 1 shows an implementation of a classic single example mode. Example 1. Example classic single mode public class ClassicSingleton {private static ClassicSingleton instance = null; protected ClassicSingleton () {// Exists only to defeat instantiation} public static ClassicSingleton getInstance () {if (instance == null) {instance =. NEW classicsingleton ();} Return Instance;}} The implementation of the single sample mode in Example 1 is easy to understand. The Classicsingleton class maintains a static reference to a separate single example instance, and returns that reference from the static method getInstance (). There are a few places that make us interested in the Classicsingleton class. First, Classicsingleton uses a well-known lazy instantiation to create a reference to the single-class class; the result, the instance of this single case is created when the getInstance () method is first called. This tip ensures that the example of a single case is only established when needed. Second, pay attention to the Classicsingleton implements a protected constructor so that the client cannot instantiate an instance of a Classicsingleton class. However, you will be surprised perfectly legal code below: public class SingletonInstantiator {public SingletonInstantiator () {ClassicSingleton instance = ClassicSingleton.getInstance (); ClassicSingleton anotherInstance = new ClassicSingleton (); ...}} This code fragment can why the front Create an instance without inheriting the Classicsingleton and the Classicsingleton class constructor? The answer is that the Protected constructor can be called by their subclasses and other types in the same package. Because Classicsingleton and SingletonStantantiator are located in the same package (default package), the SingletonStantiator method creates an instance of Clasicsingleton. In this case, there are two solutions: First, you can make Classicsingleton's constructor to change private (private), which only Classicsingleton can call it; however, this means that Classicsingleton does not have a subclass.