In general, design patterns fall into three broad categories:
Create five types of models: Factory method mode, abstract Factory mode, singleton mode, builder mode, prototype mode.
Structure mode, a total of seven kinds: Adapter mode, adorner mode, proxy mode, appearance mode, bridging mode, combined mode, enjoy the meta-mode.
There are 11 types of behavioral Patterns: Strategy mode, template method mode, observer mode, iteration sub-mode, responsibility chain mode, Command mode, Memo mode, state mode, visitor mode, mediator mode, interpreter mode.
The singleton pattern ensures that there is only one instance of a class, and that it provides this instance to the system by itself, and the singleton pattern is divided into lazy and a hungry man types:
A hungry man is thread-safe, but relatively time-saving:
1 classsingleton1{2 Private StaticSingleton1 instance =NewSingleton1 ();3 4 PrivateSingleton1 () {}5 6 Public StaticSingleton1 getinstance () {7 returninstance;8 }9}
Lazy if not locking is not thread-safe, but relatively wasteful running time:
1 classsingleton2{2 Private StaticSingleton2 instance =NULL;3 4 PrivateSingleton2 () {}5 6 Public Static Singleton2 getinstance () {//Add synchronized keyword to the method is thread-safe7 if(NULL==instance)8Instance =NewSingleton2 ();9 returninstance;Ten } One}
In addition, a hungry man also has a way of using an inner class implementation:
1 classsingleton3{2 Static classinner{3 StaticSingleton3 instance =NewSingleton3 ();4 }5 6 PrivateSingleton3 () {}7 8 Public StaticSingleton3 getinstance () {9 returninner.instance;Ten } One}
In the development, the use of a hungry man-style more.
Single-Case mode