Under what circumstances is a single case mode used
Conditions for using a single case pattern
There is an important requirement for using a single case pattern:
Singleton mode should be used when a system requires only one instance of a class. Conversely, if a class can have several instances coexisting, then there is no need to use a singleton class. But experienced readers may see a lot of examples of improper use of a single example, which is not easy to do, and here are some of these.
Example One
Q: One of my systems needs some "whole" variables. After learning the single example pattern, I found that I could use a single example class to hold all the "whole" variables. Is this the right thing to do?
A: This is contrary to the intention of the single case model. The singleton pattern should only be used when there is a real "single instance" requirement.
A well-designed system should not have so-called "full" variables, which should be placed in the class corresponding to the entity they describe. Extracting these variables from the entity classes they describe and placing them in an unrelated single instance class causes these variables to produce erroneous dependencies and coupling relationships.
Example Two
Q: One of my systems needs to manage the connection to the database. After learning the single case pattern, I found that a single instance class could be used to wrap a connection object and close the connection object in the Finalize () method. In this case, the Finalize () object is invoked when an instance of the Singleton class is not referenced, so the Connection object is freed. How wonderful it is.
A: It is not appropriate to do so. Do not use singleton mode unless there is a single instance requirement. Here connection objects can coexist with several instances at the same time, and do not need to be a single instance.
There are a number of error-using cases in single mode that are similar to this example, and are not appropriate to attempt to manage the lifecycle of shared resources using a single case pattern.
State of a single instance class
A single case class with a state
A single instance class can be stateful (stateful), and a single case object with a state is generally a mutable (mutable) Single Instance object.
A mutable single Case object with state is often used as a State Library (repositary). For example, a single object can hold an int-type attribute to provide a system with a numeric unique serial number as a billing number for a traffic system. Of course, a single instance class can hold one aggregation, allowing multiple states to be stored.
Single case class with no State
On the other hand, a single instance class can also be stateless (stateless) and used only as an object that provides a tool function. Since it is necessary to provide a tool function, there is no need to create multiple instances, so it is appropriate to use a singleton pattern. A single instance class that has no state is the invariant (immutable) Single case class; for invariant patterns, readers can refer to the "invariant (immutable)" chapter of this book.
Decentralized systems for multiple JVM systems
The EJB container has the ability to call an instance of an EJB across several JVM. Because a single instance object is not an EJB, a single instance class is limited to one JVM. In other words, if the EJB still needs to refer to the same singleton class after it crosses the JVM, the Singleton class is instantiated in several JVMs, causing instances of multiple singleton objects to appear. A Java EE application system may be distributed in several JVMs, which does not necessarily require an EJB to cause instances of multiple singleton classes to appear in different JVMs.
If this single example class is not stateful, then there is no problem. Because there is no difference between an object without a state. But if the single example class is stateful, then the problem comes. For example, if a single instance object can hold an int-type attribute to provide a system with a unique serial number, the user will see the same number several times as the billing number for a particular traffic system.
In any decentralized system that uses EJB, RMI, and Jini technologies, you should avoid the use of stateful, single-case patterns.
Multiple class Loaders
There will be multiple classloader in the same JVM, and two instances will appear when two class loaders load the same class at the same time. In many Java EE servers that allow several servlet engines within the same server, each engine has a separate classloader that is insulated from objects loaded with different ClassLoader.
For example, a Java EE system is located in the Java server has two servlet engine: one as the Intranet to the company's Web site managers use, and another to the company's external customers to use. Both share the same database, and both systems need to invoke the same single instance class. If the single example class is a stateful single class, then the status of the single Instance object that the intranet and extranet users see is different. Unless the system has a coordinating mechanism, the use of stateful single classes should be avoided in this case.