当前位置:网站首页>Handwritten RPC the next day -- review of some knowledge

Handwritten RPC the next day -- review of some knowledge

2022-06-24 21:19:00 datoooooou

Learning goals :

Shallow copy and deep copy
A dynamic proxy
Simple builder mode (Builder)


Learning content :

Deep copy and light copy

Thank you for your good article :https://blog.csdn.net/riemann_/article/details/87217229

This is the deep copy I saw when I brushed the questions , I didn't know much, so I went to primary school .
First, when discussing deep and shallow copy, we discuss reference copy and object copy , Reference copy It means that an object and its copy point to the same address value , That is, modifying one will affect the other ;
( Steal a big guy's picture ) Insert picture description here
Another copy is called Object Copy , That is, an object and its copy point to different address values , In fact, this is what we call a copy in our daily life , Steal another picture :
 Insert picture description here
Both deep copy and shallow copy belong to the content of object copy , The main difference between them is :

Shallow copy All variables of the copied object contain the same values as the original object , All references to other objects still point to the original object . That is, the shallow copy of the object will be correct for “ Lord ” Object to copy , But it doesn't copy the objects in the main object .” The object in it “ Will be shared between the original object and its copy .

In short , Shallow copy copies only the objects under consideration , Instead of copying the object it references .

and Deep copy Is a single copy of the entire object , A deep copy copies all the attributes , And copies the dynamically allocated memory that the attribute points to . A deep copy occurs when an object is copied with the object it references . Deep copy is slower and more expensive than shallow copy .

It can be understood that the shallow copy only copies the surface , A deep copy is a copy of everything related to the object .

In the way of implementation , Shallow copies can generally be made directly through clone Method realization , To implement deep copy, you can rewrite the Object Of clone Method , It should be noted that the original method is protected, Need to be changed to public; Another approach is based on serialization Serialized To achieve , The specific process is not very clear , If you have a chance to use it later, you can fill in .

A dynamic proxy

Reference blog :https://www.cnblogs.com/gonjan-blog/p/6685611.html

The proxy pattern It is commonly used. java Design patterns , It is characterized by the same interface between agent class and delegate class , The agent class is mainly responsible for preprocessing messages for the delegate class 、 Filtering messages 、 Forward message to delegate class , And post processing of messages, etc . There is usually an association between a proxy class and a delegate class , An object of a proxy class is associated with an object of a delegate class , The object itself of the proxy class does not really implement the service , Instead, it calls the methods associated with the objects of the delegate class , To provide specific services .

Static proxy It refers to that the programmer or tool has proxy classes before compilation 、 Surrogate class 、 The interface is determined , That is, the .class The file has been generated , Then the proxy class can hold the object of the proxy class , At the same time, it calls the methods in the interface overridden in the proxy class , You can also add content as needed . The most straightforward is Spring Aspect oriented programming in (AOP), We can do something before a pointcut , Do something after a pointcut , This cut-off point is just one method at a time . The classes of these methods must be proxied , Some other operations are cut in during the agent process .

A dynamic proxy The proxy class is created when the program is running , That is, it is not determined before compilation .

The realization of dynamic agent : stay java Of java.lang.reflect A Proxy And a class InvocationHandler Interface , Through this class and this interface, you can generate JDK Dynamic proxy class ( There are interfaces for implementation , And CGLIB Dynamic agents are different ) And dynamic proxy objects . A simple implementation is as follows ( Piracy map ):
 A simple implementation of dynamic proxy
InvocationHandler The interface needs to implement invoke() Method , To indicate the action when the method of the proxy object is called .

Builder pattern (Builder)

Because it is simply applied to , Just put its role in JAVA I've learned a simple way to use , As follows :
Reference blog :https://blog.csdn.net/ShuSheng0007/article/details/86619675

Use scenarios

When the number of constructor parameters of a class exceeds 4 individual , And some of these parameters are optional , It will be very troublesome to use it directly , Consider which constructor to call, and so on , At this point, you can consider using the constructor pattern

JAVA A simple implementation method in

For example, we now have the following computer class Computer, among cpu And ram Is a required parameter , Others 3 One is an optional parameter , So how do we construct an instance of this class ?
 Insert picture description here
The above implementation code is :

public class Computer {
    private final String cpu;// must 
    private final String ram;// must 
    private final int usbCount;// Optional 
    private final String keyboard;// Optional 
    private final String display;// Optional 

    private Computer(Builder builder){
        this.cpu=builder.cpu;
        this.ram=builder.ram;
        this.usbCount=builder.usbCount;
        this.keyboard=builder.keyboard;
        this.display=builder.display;
    }
    // Omit getter
    ...
    public static class Builder{
        private String cpu;// must 
        private String ram;// must 
        private int usbCount;// Optional 
        private String keyboard;// Optional 
        private String display;// Optional 
       
        public Builder(String cup,String ram){
            this.cpu=cup;
            this.ram=ram;
        }

        public Builder setUsbCount(int usbCount) {
            this.usbCount = usbCount;
            return this;
        }
        public Builder setKeyboard(String keyboard) {
            this.keyboard = keyboard;
            return this;
        }
        public Builder setDisplay(String display) {
            this.display = display;
            return this;
        }        
        public Computer build(){
            return new Computer(this);
        }
    }
}

The methods called by the client are as follows :

Computer computer=new Computer.Builder(" Intel "," samsung ")
        .setDisplay(" samsung 24 " ")
        .setKeyboard(" Logitech ")
        .setUsbCount(2)
        .build();

My general understanding is to separate some necessary and non necessary parameters , It is convenient to create instances .


Learning output :

Today, the simplest rpc The frame is done , But the last thing about dynamic proxies and thread pools is not very clear , Go and finish it tomorrow, and then continue to do it , Hope to be in 3 Let's finish this project by month , I really want to recite the stereotype ..

原网站

版权声明
本文为[datoooooou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211317188248.html