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