preface
The proxy pattern, also known as the delegate pattern, is a kind of structural design pattern. In real life, there are many scenarios similar to the agent mode, such as the Internet agent, litigation and so on.
define
Provide a proxy for other objects to control access to that object.
role
- Subject: Abstract topic class that declares the common interface methods of the real topic and the proxy.
- RealSubject: Real topic class, the real topic represented by the proxy class. The client indirectly calls the methods of the real topic class through the proxy class.
- Proxy: A Proxy class that holds a reference to a real topic class and executes it by invoking the corresponding interface method of the real topic class in the interface method it implements.
- Client: indicates the client class.
type
Categorize from a coding perspective
- Static agent
- A dynamic proxy
Classification by scope of application
- Remote agent: provides a local representation of an object in different address Spaces so that the system can
Server
Partial implementation hiding. - Virtual proxies: Use a proxy object to represent a resource-intensive object and create it only when it is really needed.
- Security proxy: Used to control access to real objects. This is used when the real object has different access permissions.
- Intelligence guidelines: When calling a real object, the proxy handles other things, such as counting references to the computer’s real object, and automatically releasing it when it’s not referenced. Or when an actual object is accessed, check to see if it can already be locked to ensure that other objects cannot change it.
Simple implementation of the static proxy pattern
Scene description
I haven’t been back to Harbin for many years and I miss the taste of qiulin red sausage. But I have been very busy because of work can not get away, can not personally back to Harbin to buy, so please in Harbin friends to help me buy qiulin red sausage.
- Abstract Topic classAbstract topic classes have interface methods common to real topic classes and proxies
buy
public interface IShop {
void buy();
}
Copy the code
- Real subject class: The purchaser
Giants
That’s me. I made it happenIShop
Interface providedbuy
Methods.
public class Giants implements IShop {
@Override
public void buy() {
System.out.println("Buy"); }}Copy the code
- The proxy classThe proxy class I’m looking for also needs to be implemented
IShop
Interface, and to hold the proxied, inbuy
Method called by the proxiedbuy
Methods.
public class Purchasing implements IShop {
private IShop mShop;
public Purchasing(IShop shop) {
this.mShop = shop;
}
@Override
public void buy() { mShop.buy(); }}Copy the code
- Client class: The code of the client class contains the real topic class (proxee), and ultimately calls the methods implemented by the real topic class (proxee).
public class Client { public static void main(String[] args) { IShop giants = new Giants(); IShop purchasing = new Purchasing(giants); purchasing.buy(); }}Copy the code
Simple implementation of dynamic proxy pattern
Scene description
The above example is a static proxy that already has a class compile file for the proxy class before the code runs; Dynamic proxies, on the other hand, are objects that dynamically generate proxy classes through reflection at bring runtime and determine who exactly to proxy. We don’t need to know who the agent is during the coding phase, and who the agent will be aware of when the code runs.
- Create dynamic proxy classes
/** * Implement Java dynamic proxy interface InvocationHandler, Public class DynamicPurchasing implements InvocationHandler {private Object implements InvocationHandler; public DynamicPurchasing(Object obj) { this.obj = obj; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object result = method.invoke(obj, args);if(method.getName().equals("buy")){
System.out.println(Giants are buying, buying, buying);
}
returnresult; }}Copy the code
- Modify the client class code as follows: call
Proxy.newProxyInstance
To generate the dynamic proxy class, callpurchasing
的buy
Method will callmDynamicPurchasing
的invoke
Methods.
Public class Client {public static void main(String[] args) {Giants = new Giants(); // create DynamicPurchasing mDynamicPurchasing = new DynamicPurchasing(giants); // create giants ClassLoader ClassLoader loader = giants.getClass().getClassloader (); // Create Proxy Class IShop purchasing = (IShop) proxy.newProxyInstance (loader, new Class[] {ishop.class}, mDynamicPurchasing); purchasing.buy(); }}Copy the code
advantages
- The real subject class is the implementation of actual business logic, not other than the job.
- Real subject categories can change at any time; But because it implements a common interface, the proxy class can be used without modification.