A single-instance design pattern and multiple design patterns for Java design patterns

Source: Internet
Author: User

Singleton design Pattern: The construction method is privatized, the static property and method are defined inside the class, and the static method is used to get the instantiated object of this class; no matter how many instanced objects are generated externally, there is essentially only one instanced object

A hungry man single-case design

classsingleton{Private Static FinalSingleton INSTANCE =NewSingleton ();//produces an instantiated object    PrivateSingleton () {}//Encapsulation Construction Method     Public StaticSingleton getinstance () {returnINSTANCE;//get instantiated Object    }     Public voidprint () {System.out.print ("Hello word"); }} Public classtest{ Public Static voidMain (String args[]) {Singleton s= Singleton.getinstance ();//regardless of how many times the getinstance () method is called, the resulting instantiation object is the sameS.print (); }}

Lazy Style design mode

classsingleton{Private Static FinalSingleton INSTANCE;//defines an object of this class that is not instantiated    PrivateSingleton () {}//Encapsulation Construction Method     Public StaticSingleton getinstance () {if(INSTANCE = =NULL) {INSTANCE=NewSingleton (); }        returnINSTANCE;//get instantiated Object    }     Public voidprint () {System.out.print ("Hello word"); }} Public classtest{ Public Static voidMain (String args[]) {Singleton s= Singleton.getinstance ();//regardless of how many times the getinstance () method is called, the resulting instantiation object is the sameS.print (); }}

Multiple design Patterns: privatization of construction methods, resulting in a specific number of objects

classsex{PrivateString sex; Private Static FinalSex man =NewSex ("Male"); Private Static FinalSex WOMAN =NewSex ("female"); Privatesex (String sex) { This. Sex =sex; }     Public StaticSex getinstance (intch) {        Switch(CH) { Case1:                returnMan ;  Case2:                returnWOMAN; default:                return NULL; }    }     PublicString toString () {returnsex; }} Public classtest{ Public Static voidMain (String args[]) {Sex s= Sex.getinstance (1);    System.out.println (s); }}

A single-instance design pattern and multiple design patterns for Java design patterns

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.