1. General single case mode
This type of flaw: inability to deal with multithreaded issues
Package Com.boonya.pattern.singleton;
/**
* Single Example
* <li> file name: singleton</li>
* <li> file Description: $ Single Example Class </li>
* <li > Content Summary: single example creation </li>
* <li> Completion date:2013-9-27</li>
* <li> Sample writing: Boonyachengdu@gmail.com</li>
* <li> application scenario: frequently used objects and values unchanged, like components such as objects </li>/public
Class Singleton
{
/**
* Instantiate/
private static Singleton regardless of whether it is invoked singleton=new Singleton ();
/**
* Ensure external cannot be instantiated, that is, invalid with new keyword
* *
private Singleton () {
System.out.println ("Singleton constructor ");
}
/**
* must ensure synchronization, otherwise multi-threaded values are not uniform, but synchronized keyword use can degrade system performance
* * Public
static /*synchronized*/ Singleton getinstance () {return
Singleton;
}
}
2, lazy load single case mode
This type of flaw: System performance drops significantly when using multiple threads
Package Com.boonya.pattern.singleton;
/**
* Single Example
* <li> file name: lazysingleton</li>
* <li> file Description: $ Single Example class </li>
* <li> Content Summary: single example creation </li>
* <li> Completion date:2013-9-27</li>
* <li> Sample writing: Boonyachengdu@gmail.com</li>
* <li> application scenario: frequently used objects and values unchanged, like components such as objects </li>/public
Class Lazysingleton
{
/**
* Call time to instantiate/
private static Lazysingleton Singleton=null;
/**
* Ensure external cannot be instantiated, that is, invalid with new keyword
* *
private Lazysingleton () {
System.out.println ("Singleton Constructor ");
}
/**
* must ensure synchronization, otherwise multi-threaded values are not uniform (thread execution may be judged to be null)/public
static synchronized Lazysingleton getinstance () {
if (singleton==null) {
singleton=new Lazysingleton ();
}
Return singleton
}
}
3, strengthen (plus) single case
This type of flaw: When using the reflection mechanism, the private constructor of the Singleton class is forcibly invoked to generate multiple instances
Package Com.boonya.pattern.singleton;
/**
* Single Example
* <li> file name: plussingleton</li>
* <li> file Description: $ Single Example class </li>
* <li> Content Summary: single example creation </li>
* <li> Completion date:2013-9-27</li>
* <li> Sample writing: Boonyachengdu@gmail.com</li>
* <li> application scenario: frequently used objects and values unchanged, like components such as objects </li>/public
Class Plussingleton
{
/**
* Ensures that external cannot be instantiated, that is, invalid with the new keyword * *
private Plussingleton () {
System.out.println ("Singleton constructor");
}
/**
* Internal class JVM initial swap call
/
private static class singletonobject{
private static Plussingleton singleton=new Plussingleton ();
}
/**
* No need to consider multithreading
issues
/public static Plussingleton getinstance () {
return Singletonobject.singleton
}
}
4. Serialization of single cases
This type of flaw: serialization and deserialization can potentially break a single instance
Package Com.boonya.pattern.singleton;
Import java.io.Serializable; /** * Single Example * <li> file name: singleton</li> * <li> File Description: $ Single Example Class </li> * <li> Content Summary: Single case creation </ li> * <li> Completion date:2013-9-27</li> * <li> sample writing: boonyachengdu@gmail.com</li> * <li> Application scenario: frequent The object used and the value is unchanged, like the object serialization operation such as the component needs to be cautious </li>/@SuppressWarnings ("Serial") public class Serializablesingleton implements S
erializable {private String context;
Public String GetContext () {return context;
public void SetContext (String context) {This.context = context;
/** * Whether or not the call will be instantiated/private static Serializablesingleton instance=new Serializablesingleton ();
/** * Ensure external cannot be instantiated, that is, use the New keyword invalid/private Serializablesingleton () {System.out.println ("Singleton constructor");
context= "Serializablesingleton";
/** * Get a Single Instance object/public static Serializablesingleton getinstance () {return instance; /** * Prevents new instances from being generated and always returns the current object (this method cannot be removedOtherwise serialization and deserialization will be wrong) * * Private Object Readresolve () {return instance;
}
}
5. Performance and Precautions
(1), frequently used objects and values unchanged, such as components such as the proposed use of a single example. Lazy loading is recommended here and then instantiated to reduce the performance of new instantiated objects when the JVM initializes static, and to reduce the execution of GC mechanisms.
(2) Considering whether it will be used in multiple threads, the use of a single example in multithreading is a serious performance.
(3), use a single example, try not to use serialization operation, the danger is difficult to predict, the best is the specific problem specific analysis.