Each interview needs to look at the design pattern, each time is very good understanding, but the actual development of no application always forget. Now summarize it.
23 Different design modes
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.
In fact, there are two types: concurrency mode and thread pool mode.
23 Design patterns, I mainly from the concept, principles, advantages and disadvantages and application scenarios summarized.
Start with a single-instance model
One: Singleton mode
concept : Simply put, there is only one instance of a class in an application, and you have no way to go to new because the constructor is private and is generally used by getinstance () to get their instances.
Pros : Singleton mode prevents other objects from instantiating copies of their own singleton objects, ensuring that all objects have access to unique instances.
Cons : Although the number is small, there will still be some overhead if every time an object requests a reference to check for an instance of the class. This problem can be resolved by using static initialization.
Attach Demo
1. Normal mode lazy mode (thread insecure)
1 Public classSingleton {2 3 Private StaticSingleton Singleton;4 5 PrivateSingleton () {6 }7 8 Public StaticSingleton getinstance () {9 if(Singleton = =NULL) {TenSingleton =NewSingleton (); One } A returnSingleton; - } -}
2. Lazy mode (thread safe)
1 Public classSingleton {2 Private StaticSingleton instance; 3 PrivateSingleton () {}4 Public Static synchronizedSingleton getinstance () {5 if(Instance = =NULL) { 6Instance =NewSingleton (); 7 } 8 returninstance; 9 } Ten}
3. a hungry man type
1 public class Singleton { 2 private static Singleton instance = new Singleton (); 3 private Singleton () {} 4 public Static Singleton getinstance () { 5 Span style= "COLOR: #0000ff" >return instance; 6 } 7 }
4. Static Inner class
1 Public classSingleton {2 Private Static classSingletonholder {3 Private Static FinalSingleton INSTANCE =NewSingleton (); 4 } 5 PrivateSingleton () {}6 Public Static FinalSingleton getinstance () {7 returnsingletonholder.instance; 8 } 9}
5. Enumeration
1 Public enum Singleton { 2 INSTANCE; 3 Public void Whatevermethod () { 4 } 5 }
This approach is advocated by effective Java author Josh Bloch, which not only avoids multi-threaded synchronization problems, but also prevents deserialization from recreating new objects, which can be a very strong barrier, but Personally, because 1.5 in the inclusion of the Enum feature, written in this way can not help people feel unfamiliar.
6. Double check lock
Java Design Pattern Learning