Hey, I have a little story to share with you today. It's like two best friends, one is called "static agent", the other is called "dynamic agent", although they have similar names, but they have very different personalities and characteristics.
Dynamic static proxy ip difference
First of all, let's get to know our friend "Static Agent". It is like an attentive and diligent waiter who waits silently for the arrival of customers. When the customer needs to order food or check out, the waiter will not hesitate to help deal with everything, but when there is no customer, the waiter has nothing to do, just like a closed door, motionless.
The "Dynamic Agent" is a completely different friend, it is like an omnipotent magician who can change all kinds of magic props according to the needs of customers. No matter what service the customer needs, the magician can respond flexibly and make the customer feel very satisfied. Even when the customer is changing his needs, the magician can still serve the customer without stopping, always remaining flexible and energetic.
Well, having said that, you may ask, what exactly is the difference between them? Actually, the key lies in the fact that they handle things differently. It's like two restaurant owners. One likes to prepare things before they happen, while the other is better at improvising. So a "static agent" prepares every service in advance, while a "dynamic agent" prepares it when it's needed.
Difference between static and dynamic proxies
Let me give you a simple example. For example, suppose there is an interface that requires a proxy:
"`java
public interface Subject {
void doSomething().
}
“`
So, first let's see what a "static proxy" does:
"`java
public class StaticProxySubject implements Subject {
private RealSubject realSubject.
public StaticProxySubject(RealSubject realSubject) {
this.realSubject = realSubject;
}
@Override
public void doSomething() {
System.out.println("The static proxy does some preparatory work");;
realSubject.doSomething();
System.out.println("The static agent does some finishing touches");;
}
}
“`
A "dynamic agent" does:
"`java
public class DynamicProxySubject implements InvocationHandler {
private Subject realSubject.
public DynamicProxySubject(Subject realSubject) {
this.realSubject = realSubject;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("Dynamic agent doing some prep work");;
Object result = method.invoke(realSubject, args);
System.out.println("Dynamic agent doing some finishing touches");;
return result;
}
}
“`
Through the above examples, in fact, we can see that "static proxies" need to write the proxy class in advance, while "dynamic proxies" is to dynamically generate the proxy class at runtime. This is the fundamental difference between them!
I hope this little story can help you better understand the difference between "static proxy" and "dynamic proxy". Just like the differences between people, each type of proxy has its own characteristics and advantages and disadvantages, only in-depth understanding, in order to make the appropriate choice according to the actual needs. Wish you all in the programming road, can go farther and farther, the better!