Design pattern-singleton pattern

Source: Internet
Author: User

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


 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.