java-design mode-Singleton mode-a hungry man mode, lazy mode

Source: Internet
Author: User

//-------------------------------------------------------------A hungry man mode--start------------------------------------------------ ----------- PackageCom.study.DesignPattern01;/*** Create a single instance of a Hungry man mode *@authorZlhome * Some objects, we only need one, if more, then it can lead to inconsistent data, excessive resources and so on, such as: Configuration file, tool class, thread pool, cache, log Object*/ Public classSingleton {//1, the construction method of privatization (such a class can not be instantiated)    PrivateSingleton () {System.out.println ("Instantiating the Singleton class"); }        //2, instantiate a singleton object, the object is static (so it will only be instantiated once), private (security, external cannot directly singleton.instance call)    Private StaticSingleton instance =NewSingleton (); //3, provide a static method (static method, the class can be called directly), to obtain a singleton     Public StaticSingleton getinstance () {returninstance; }}//--------------------------------------------------------------------------------------------- PackageCom.study.DesignPattern01;ImportOrg.junit.After;ImportOrg.junit.AfterClass;ImportOrg.junit.Before;ImportOrg.junit.BeforeClass;Importorg.junit.Test; Public classsingletontest {@BeforeClass Public Static voidSetupbeforeclass ()throwsException {System.out.println ("JUnit begins Beforeclass ..."); } @AfterClass Public Static voidTeardownafterclass ()throwsException {System.out.println ("JUnit after Afterclass ..."); } @Before Public voidSetUp ()throwsException {System.out.println ("JUnit begins before ..."); } @After Public voidTearDown ()throwsException {System.out.println ("JUnit after ..."); } @Test Public voidTest () {System.out.println ("JUnit begins ..."); Singleton instance=singleton.getinstance (); Singleton Instance1=singleton.getinstance (); System.out.println ("Is equal:" + (instance==Instance1)); }}//-------------------------------------------------------------A hungry man mode-end------------------------------------------------ -----------//============================================================ Lazy Mode-Start ======================================= ===================== PackageCom.study.DesignPattern01; Public classSingleton1 {//1. Set the construction method to private (so that the class cannot be instantiated externally)    PrivateSingleton1 () {System.out.println ("Instantiating Singleton1"); }        //2, affirm the single Case object    Private StaticSingleton1 instance; //3. Provide a static method (static method belongs to class) for external invocation     Public StaticSingleton1 getinstance () {if(instance==NULL) {System.out.println ("Instantiating Singleton1 objects for the first time"); Instance=NewSingleton1 (); }        returninstance; }}//============================================================ PackageCom.study.DesignPattern01;ImportOrg.junit.After;ImportOrg.junit.AfterClass;ImportOrg.junit.Before;ImportOrg.junit.BeforeClass;Importorg.junit.Test; Public classsingleton1test {@BeforeClass Public Static voidSetupbeforeclass ()throwsException {System.out.println ("JUnit begins Beforeclass ..."); } @AfterClass Public Static voidTeardownafterclass ()throwsException {System.out.println ("JUnit after Afterclass ..."); } @Before Public voidSetUp ()throwsException {System.out.println ("JUnit begins before ..."); } @After Public voidTearDown ()throwsException {System.out.println ("JUnit after ..."); } @Test Public voidTest () {System.out.println ("JUnit begins ..."); Singleton1 instance=singleton1.getinstance (); Singleton1 Instance1=singleton1.getinstance (); System.out.println ("Is equal:" + (instance==Instance1)); }}//============================================================ Lazy mode-end ======================================= =====================

Design Patterns
Higher reliability, easier understanding, better scalability ' easier to maintain
1. Single Case mode:
1) Single case background, situation:
Some objects, we only need one, if more, then it can lead to inconsistent data,
Take up too much resources and so on, such as:
Configuration files, tool classes, thread pools, caches, log objects
2) Principle:
Instantiating an object is implemented by constructing a method (the program class is not written, the program class has a default constructor),
The singleton allows only one instance to be obtained, so

A Hungry man mode:
Instantiate object when class is loaded (hungry, active instance object)
(1) The implementation of a single example to rewrite the construction method: The construction method is rewritten as private, after rewriting, it can not be directly instantiated (not allowed to create an instance directly outside)
(2) Create a unique instance of the class (the class goes to the new instance, and the instance is a static[variable, the method adds the static and then becomes the class all, not the instance object must be created to invoke], so that the instance can be called directly from the class [class name. Instance variable name])
(3) For the sake of security, external direct access to member variables is not allowed, so (2) it needs to be optimized to make the static instance of a class private, and then it cannot be accessed directly after the class name. Instance variable name, so provide a method of the class get, The Get method of the class also needs to be static to be called by the class name. Method Name.

Lazy mode:
Class loading is just a declaration, the invocation of the Singleton when the instantiation (lazy, not active to the instance object)
(1) Declare a singleton, and not instantiate a singleton object
(2) in the Get method to judge, if there is no instance of the object, the singleton object is instantiated and returned

Contrast:
Lazy mode: Class loading fast, getting objects slow, thread-safe
A Hungry man mode: class loading slow, getting objects fast, thread insecure

Summary: Static modified variables, method data classes, can be directly called through the class

java-design mode-Singleton mode-a hungry man mode, lazy mode

Related Article

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.