当前位置:网站首页>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 )
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 :
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 ):
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 ?
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 ..
边栏推荐
- Limit summary (under update)
- [cloud native learning notes] deploy applications through yaml files
- JMeter response assertion
- Comprehensive comparison of the most popular packet capturing tools in the whole network
- Prototype mode -- clone monster Army
- Read all text from stdin to a string
- Create a multithreaded thread class
- JMeter parameterization
- Oauth2.0 introduction
- [performance tuning basics] performance tuning standards
猜你喜欢

The difference between RPC and restful

Am, FM, PM modulation technology

The JS method parameter passed a number beginning with 0. A magical problem occurred and bothered me for a long time

Builder mode -- Master asked me to refine pills

Agency mode -- Jiangnan leather shoes factory

Appium desktop introduction

yeb_ Back first day

Background of master data construction

After 5 months' test, it took 15K to come for an interview. When I asked, it was not worth even 5K. It was really

A/b test helps the growth of game business
随机推荐
JMeter basic learning records
database/sql
Subnet partition operation
Foundations of Cryptography
Page replacement of virtual memory paging mechanism
The difference between RPC and restful
Golang daily question
Leetcode (455) - distribute cookies
红象云腾完成与龙蜥操作系统兼容适配,产品运行稳定
The JS method parameter passed a number beginning with 0. A magical problem occurred and bothered me for a long time
B站带货当学新东方
Static routing job
Introduction to interval DP
Self signed certificate generation
After a few years in the testing industry, do you still know a little?
Splicing audio files with ffmpeg-4.3
Role of wait function
Network flow 24 questions (round table questions)
Steps of JMeter performance test
opds sql组件能不能将流程参数通过上下文传给下一个组件