Java Design Patterns

Source: Internet
Author: User

1, Singleton design mode: A class only instantiates an object. There are two ways of a hungry man and lazy-type.

1  Packagepack;2  Public classTest13 {4      Public Static voidMain (string[] args)5     {6System.out.println (single.getinstance () = =single.getinstance ());7     }8 }9 //single-instance design pattern of the A Hungry Man program, a class that allows only one object to be generatedTen class Single One { A     Private Static FinalSingle s=NewSingle (); - Single () {} -      Public StaticSingle getinstance () the     { -         returns; -     } -      PublicString str; +      Publicstring Fun (String str) -     { +         returnstr; A     } at } - //Singleton design pattern of the lazy program, a class only allows to generate an object, the object is lazy loading - //A synchronous way to solve the problem of lazy multi-threaded access security, but it will be more inefficient - classSingle1 - { -     Private StaticSingle1 s=NULL; in Single1 () {} -      Public StaticSingle1 getinstance () to     { +         if(s==NULL) -         { the             //A lazy lock is a class of byte-code *             synchronized(Single1.class) $             {Panax Notoginseng                 if(s==NULL)  -s=NewSingle1 (); the             } +         } A         returns; the     } +}

2, Template Method design mode: When defining a function, part of the function is determined, part is indeterminate, and the identified part is used in the indeterminate part, then this is to expose the indeterminate part, which is implemented by subclasses of the class.

Java Design Patterns

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.