A hungry man and full-han singleton patterns in Java and the implementation of the singleton pattern of runtime classes in JDK

Source: Internet
Author: User

First, describe

Singleton mode is a very common design pattern in which a class can have only one object (instance), typically by privatizing the class's constructor to prevent the object from being created outside of the class and providing a unique object to the outside world (this object is created in the Class).
There are two common implementations of the singleton pattern in Java, one of which is the Bad Han way, which is to rest the class with static and initialize it when the class loads, and the other is the full-Han method, which is initialized when the object needs to be used in the program. Once initialized, the object is no longer regenerated. the runtime class in the JDK is actually a singleton pattern, and it uses the A hungry man approach.


Second, the source code

Package Tong.yue.day4_25;import java.io.ioexception;/** * Singleton mode is a very common design pattern in which a class can have only one object (instance), typically by privatizing that class's constructor, To prevent the creation of an object of the class outside of the class and provide a unique object to the outside world (this object is created in the Class). * In Java, there are two common modes of implementation, one is a bad Han way, the class of objects with static rest and initialization at the time of class loading; the other is the full-han approach, which is initialized when the object is needed in the program and is not regenerated once it is initialized. * The runtime class in the JDK is actually a singleton pattern, and it's a a hungry man approach. * @author Administrator * */public class Singleton {public static void main (string[] args) {//A hungry man get the object of the Singleton class Singleton_eager s Ingleton = Singleton_eager.getinstance (); Singleton.print ();//A Hungry man gets the object of the Singleton class Singleton_lazy Singleton_lazy = Singleton_ Lazy.getinstance (); Singleton_lazy.print ();//jdk the use of a singleton object from the runtime class runtime = Runtime.getruntime (); try {// Use the runtime object to call Notepad in Windows to open a notepad. Runtime.exec ("notepad.exe");} catch (IOException e) {System.out.println ("Notepad open failed! "); E.printstacktrace ();}}} A hungry man singleton mode, which creates an object when the class is loaded, and then no longer repeats creating class Singleton_eager {//declared as static type, creating an object when the class is loaded private static Singleton_eager Singleton_eager = new Singleton_eager ();p rivate Singleton_eager () {}public static Singleton_eager GetInstance () {return singleton_eager;} public void print () {System.out.println (Singleton_eager);}} Full-Han Singleton mode, when the program first uses the object of the class to create the object, created only once, and then no longer repeatedly created class Singleton_lazy{private static singleton_lazy Singleton_lazy = null ;p rivate Singleton_lazy () {}public static Singleton_lazy getinstance () {//If you use the object for the first time, create one, or use an already created object if ( Singleton_lazy==null) {singleton_lazy = new Singleton_lazy ();} return singleton_lazy;} public void print () {System.out.println (Singleton_lazy);}}


Third, the implementation of the runtime class of the JDK in a single example

Package tong.yue.day4_25;/** * The Runtime class in the JDK has a singleton mode of a hungry man * @author Tong * */public class Runtime {    private static runtime Currentruntime = new Runtime ();        public static Runtime GetRuntime () {        return currentruntime;    }        Private Runtime () {}}




A hungry man and full-han singleton patterns in Java and the implementation of the singleton pattern of runtime classes in JDK

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.