This project decomposes the common written interview questions of major factories, traces the source to the underlying implementation principles of data structures and algorithms, and knows what they are.
Establish a knowledge structure system for easy search, welcome more like-minded friends to join the project AlgorithmPractice, (issues and pull requests are welcome).
11. For more design patterns, please go directly to the design pattern section on my github to find out, thank you for being so skilled, and come to pay attention to me!
In some scenarios, we need a class to provide only one instantiated object, so we make the constructor of the class private .
How do we get objects of this class after private? We provide a getInstance function externally to output private objects.
So where did this private object come from? If one defines the object at the beginning of the class, and then returns it through the getInstance function, we call it the hungry man mode (because it is too hungry, all the food/instance must be new to wait for me). If it is defined at the beginning, but when it is used later, new in the getInstance function and return, it becomes the lazy man mode.
Why is there a hungry man mode and a lazy man mode? The difference between the hungry man mode and the lazy man mode is when the new object is new. We know that the new object is a process that consumes resources and time. It needs CPU to schedule and allocate memory, especially for large objects, nested objects, which are often loaded. It's very slow, and if we load it when we actually use it, it will save time.
So, should I just use the lazy man mode? No, the lazy man mode is not used well, it will cause security problems, and multiple objects will appear, which violates the original intention of the singleton mode and further causes thread safety issues.
So how can it be used to ensure safety? It is guaranteed by locking synchronized, but locking will result in performance loss. What if this loss is reduced and the operation is safe? Introducing DCLSingletom, but is DCLSingletom necessarily safe? In fact, it is not. Because of the rearrangement of instructions, there is a certain chance that an error will occur when using the object (object attribute value is null).
Better suggestion? Use enumeration and internal fatigue to implement singleton mode.
Note :
When using synchronized, remember to cooperate with volatile to prohibit instruction rearrangement
The emergence of the agency model is mainly based on the inheritance and rewrite characteristics of the development language
The proxy mode is to add some conventional operations before and after the real execution class. Some public methods that everyone will use. Common applications such as unified exception handling, unified log, unified error return, etc., all use the proxy mode to make the real The execution class is only related to the business itself, without excessive consideration of other situations.
Code display:
The proxy interface, the proxy class and the proxy class all need to implement it, and override its methods
publicinterfaceProxyInterface{
voidwork();}
Proxy class
publicclassProxyimplementsProxyInterface{
ProxyInterface p;publicProxy(ProxyInterface r){
// TODO Auto-generated constructor stub
p = r;}@Overridepublicvoidwork(){
// TODO Auto-generated method stub
System.out.println("i am Proxy, i am help real's work,work begin");
p.work();
System.out.println("i am Proxy, work end");}}
Real execution class
publicclassRealimplementsProxyInterface{
@Overridepublicvoidwork(){
// TODO Auto-generated method stub
System.out.println("i am real, i am doing work");}}
Achieve effect
publicclassProxyTest{
@TestpublicvoidtestProxy(){
ProxyInterface r =newReal();
ProxyInterface p =newProxy(r);
p.work();}}
Note :
In design, the proxy class needs to consider more than the proxy class.
The observer mode is equivalent to the eyes in the game. The observer inserts an eye in the method of the observed. When the method of the observed is executed, the observer will receive the information and initiate the response method.
Therefore, a complete observer model includes: the observed and the observer.
The observer should have a method specifically to load the observer, and another method to notify the observer
Therefore, the observer should also have a callback method, which is waiting for the observer to trigger
The essence of template design pattern is a fixed algorithm framework
The parent class has a fixed template, but the specific implementation steps of the open template allow subclasses to rewrite
Code
father
publicabstractclassTemplateDemo{
publicfinalvoidtemplate(){
method1();method2();method3();}publicvoidmethod1(){
System.out.println("father class first stop");}publicvoidmethod2(){
System.out.println("father class second stop");}publicvoidmethod3(){
System.out.println("father class third stop");}}
Subclass
publicclassSubTemplateextendsTemplateDemo{
@Overridepublicvoidmethod1(){
System.out.println("sub class first step");}}