understand the single case pattern in Java
Welcome to join my QQ group 1543150073 for more Gava and career information. I'll help you.
Single example mode is simply: The same object, do not go to new multiple objects. Reduce memory consumption.
& in Java design patterns, see the introduction of about 23 kinds of design patterns. Like Factory mode, factory method mode, single case mode, appearance (façade) mode, observer observer mode, bridge mode are more commonly used, different projects have different design direction, can refer to the design patterns are not the same, no destiny, Only the above models are used more. Cade) mode, observer observer model
The benefit of the design pattern is to provide a common solution to the problem, reduce duplication of effort and improve development efficiency.
Below, Antioch today to say one of the "single case Mode", in fact, it is in the actual project development, it still uses more &
The single case pattern, which is that the entire application is running, classes that use singleton patterns need to be instantiated only once (new once), always the same object (instance), thus reducing the overhead of memory and improving development efficiency, as in general public classes, such as reading a configuration file class, A class that contains other classes in the program to invoke all public see constants, and so on, we can all define a "single case Mode" class, below Antioch to give an example to test, I believe, friends see Antioch below the example and summary, should be clear.
There are about 23 design patterns to introduce. Like the factory code as follows
For design Patterns in Java, see //Below is a single case pattern class
public class Mysingleton
{There are about 23 design patterns to introduce. Like a factory
Privatestatic final Mysingleton instance= new Mysingleton ();
Privatemysingleton () CADE) mode, observer observer model
{
}
Publicstatic to the introduction of about 23 kinds of design patterns are many. Like Factory final Mysingleton getinstance () {
return INSTANCE;
} &
}
The following is a class Cade pattern that is not a single case pattern, and the Observer observer the model
public class Nosingleton
{
Public Nosingleton () mode, factory method mode, single case mode, appearance (Fa
{}
private int id;
private String name; mode, factory method mode, single case mode, appearance (Fa
public int getId () {
return ID;
}CADE) mode, observer observer model
public void setId (int id) {
This.id = ID;
}CADE) mode, observer observer model
Public String GetName () {
return name;
There are about 23 design patterns to introduce. Like a factory
public void SetName (String name) {
THIS.name = name;
} nbsp; for design Patterns in Java, see
......
}
There are about 23 design patterns to introduce. Like a factory
There are about 23 design patterns to be introduced into the test class . Like a factory
public class Testsingleton
{
nbsp; for design Patterns in Java, see //date200810301627
//authtor Network space-time
cade) mode, observer observer Die
public static void Main (string[] args)
{
Mode, factory method mode, single case mode, appearance (Fa system.out.println ("========= =============================== ");
system.out.println (" Below is the use of single case mode, To invoke an instance of the class ");
system.out.println ("========================================"); nbsp; for design Patterns in Java, see
for (inti=0;i<6;i++)
{
Intm=i+1, there are about 23 design patterns to introduce. Like a factory
System.out.println ("The first" +m+ "the second" + "the object value is" +mysingleton.getinstance ());
}
Cade) mode, the Observer Observer Mode SYSTEM.OUT.PRINTLN ("========================================");
System.out.println ("Invoke an instance of a class without using a singleton mode");
System.out.println ("========================================"); nbsp; for Java design patterns, see
for (inti=0;i<6;i++)
{
int n=i+1 mode, factory method mode, single case mode, appearance (Fa
Nosingleton nosingletion=new Nosingleton ();
System.out.println ("The first" +n+ "the second" + "the object value is" +nosingletion);
There are about 23 design patterns to introduce. Like a factory
}
}
nbsp; for Java design patterns, see
Test results:
CADE) mode, observer Observer takes 1 minutes to see ">
nbsp; for Java design patterns, see
According to the results, Antioch summarized as follows
There are about 23 design patterns to introduce. Like the factory 1. Class Mysingleton, designed by a single example pattern, defines a private constructor, a staticfinal type of constant instance, whose value is to return an instance of a Mysingleton. Then define a getinstance () static method to return a instance, which is a constant instance or value, the first Newmysingleton () object, so in the test class, Loop 6 times, We are always printing the same object, which is fully positive, singleton mode, only once (instantiated once), reduce the cost of developing memory space in the heap nbsp; for Java design patterns, see
&2. A class with no single case mode, we are in the test class, loop 6 times, we are always printing the different objects, which means that we have new in the heap memory six times, resulting in 6 objects, it is shown that this is a need to occupy memory resources, affecting the efficiency of the
nbsp; for design Patterns in Java, look at 3. In the class on the single example mode, in the test, Antioch removed the final final state type limit, 6 times, we printed the same object always, here that the final is dispensable here, then why add final?
nbsp for Java Design patterns, it's safer to look at the final use here, to ensure that other methods of your class, other code, do not