Singleton pattern
Singleton pattern): Make sure that a class has only one instance and provides a global access point.
Unit Price Model includesThree parts:Private Constructor,Static variables,Static Method.
Specific Method:
1.Standard Singleton Mode:
/*** @ Time 2014.6.5 */package singleton;/*** @ author C. l. wang **/public class Singleton {private static Singleton uniqueInstance; // static variable private Singleton () {}// private constructor public static Singleton getInstance () {// static method if (uniqueInstance = null) uniqueInstance = new Singleton (); return uniqueInstance ;}}
2. Consider
MultithreadingThree methods:
Synchronous (synchronized) Method, AddSynchronized, Will lead to performance degradation, each call example needs to be synchronized, But the use is simple.
/*** @ Time 2014.6.5 */package singleton;/*** @ author C. l. wang **/public class Singleton {private static Singleton uniqueInstance; // static variable private Singleton () {}// private constructor public static synchronized Singleton getInstance () {// static method if (uniqueInstance = null) uniqueInstance = new Singleton (); return uniqueInstance ;}}
Eager (eagerly) MethodWhen the instance is created at the beginning, the instance space will be occupied when it is not required, that is, the occupied space is too long.
/*** @ Time 2014.6.5 */package singleton;/*** @ author C. l. wang **/public class Singleton {private static Singleton uniqueInstance = new Singleton (); // static variable private Singleton () {}// private constructor public static synchronized Singleton getInstance () {// static method // if (uniqueInstance = null) // uniqueInstance = new Singleton (); return uniqueInstance ;}}
Double-checked locking, Use
Volatile and synchronized (Singleton. class), Reducing time consumption, applicable
Java1.4The above version.
/*** @ Time 2014.6.5 */package singleton;/*** @ author C. l. wang **/public class Singleton {private volatile static Singleton uniqueInstance; // static variable private Singleton () {}// private constructor public static synchronized Singleton getInstance () {// static method if (uniqueInstance = null) {synchronized (Singleton. class) {if (uniqueInstance = null) uniqueInstance = new Singleton () ;}} return uniqueInstance ;}}
3. Use the single-piece Mode
Example:
Code:
/*** @ Time 2014.6.5 */package singleton;/*** @ author C. l. wang **/public class ChocolateBoiler {// chocolate boiler private boolean empty; private boolean boiled; public static ChocolateBoiler uniqueInstance; // static variable private ChocolateBoiler () {// private constructor empty = true; boiled = false;} public static ChocolateBoiler getInstance () {// static method if (uniqueInstance = null) uniqueInstance = new ChocolateBoiler (); return uniqueI Nstance;} public void fill () {// fill in if (isEmpty () {empty = false; boiled = false;} public void drain () {// dumping if (! IsEmpty () & isBoiled () empty = true;} public void boil () {// cook if (! IsEmpty ()&&! IsBoiled () {boiled = true ;}} public boolean isEmpty () {return empty;} public boolean isBoiled () {return boiled ;}}
4.Enum singleton)Mode to ensure thread security.
Code:
/** * @time 2014.6.5 */package singleton;/** * @author C.L.Wang * */public class EnumSingleton {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubeSingleton d1 = eSingleton.INSTANCE;d1.setName(Spike);eSingleton d2 = eSingleton.INSTANCE;d2.setName(Caroline);System.out.println(d1);System.out.println(d2);System.out.println(d1 == d2);}}enum eSingleton {INSTANCE;private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return [ + name + ];}}
Output:
[Caroline][Caroline]true