Static proxy mode
What is a static proxy?
The proxy model, as its name implies, is to find a class that can proxy its own work to complete the work of the target class. This proxy class can verify and finish the work while completing the work. Take selling used cars as an example. When your time is limited, you can find an agent to help you sell your car. When the agent helps you sell your car, he can help you complete the formalities such as transfer, cancellation and so on. This is the role of the agent.
Static proxy is a kind of agent customized for the target class. If we need to complete other kinds of work, we need to redesign the proxy class to complete the proxy work.
How to implement static proxy?
Example:
Suppose we have a document management class, through which we can read and write documents. In order to verify the permissions before reading and writing, we design a static proxy class to replace him to complete the work.
Permission class: privilege.java
package com.proxypattern;
public class Privilege {
//You have permission when key is "root"
private String key;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
Logger.java
package com.proxypattern;
public class Logger {
public void writeLog(String log){
System.out.println("Log output:" + log);
}
}
Target Class Interface (Document Management Interface): Document Manager. Java
package com.proxypattern;
public interface DocumentManager {
public void readDocument();
public void writeDocument();
}
Target class: Document ManagerImpl. Java
package com.proxypattern;
public class DocumentManagerImpl implements DocumentManager {
@Override
public void readDocument() {
System.out.println("Read document...");
}
@Override
public void writeDocument() {
System.out.println("Write document...");
}
}
Agent class: Document Manager Proxy. Java
package com.proxypattern;
/**
* The proxy class, as its name implies, replaces the Document Manager to accomplish tasks.
* In static proxy, the proxy class implements the interface of the target class, and obtains the entry objects needed by the proxy class in the constructor, such as permission validation and log.
* In the implementation of the interface method, access validation and other cut-in operations are added.
*/
public class DocumentManagerProxy implements DocumentManager{
private DocumentManager documentManager;
private Logger loggeer;
private Privilege privilege;
public DocumentManagerProxy(DocumentManager documentManager, Logger loggeer, Privilege privilege) {
this.documentManager = documentManager;
this.loggeer = loggeer;
this.privilege = privilege;
}
@Override
public void readDocument() {
if (this.privilege.getKey().equals("root")){
this.loggeer.writeLog("Privilege Verification Passed!");
this.documentManager.readDocument();
this.loggeer.writeLog("Read successful!");
}else{
System.out.println("Error:No permission, read failed!");
this.loggeer.writeLog("No permission, read failed");
}
}
@Override
public void writeDocument() {
if (this.privilege.getKey().equals("root")){
this.loggeer.writeLog("Privilege Verification Passed!");
this.documentManager.readDocument();
this.loggeer.writeLog("Write successfully!");
}else{
System.out.println("Error:Write failed without permission!");
this.loggeer.writeLog("Write failed without permission");
}
}
}
Test:
package com.proxypattern;
import org.junit.Test;
import sun.rmi.runtime.Log;
/**
* @author zdxie
* @Date 8:28 a.m. 2017/9/6
* @Description Test proxy class
*/
public class TestStaticProxy {
@Test
public void testDocument(){
DocumentManager manager = new DocumentManagerImpl();
Logger logger = new Logger();
Privilege privilege = new Privilege();
privilege.setKey("key");
DocumentManagerProxy proxy = new DocumentManagerProxy(manager,logger,privilege);
proxy.readDocument();
}
}
Advantages and disadvantages of static proxy mode
Advantage:
1. Can complete all kinds of entry operations to the target class.
2. Realize the task instead of the target class.
Disadvantages:
1. If the method of the target interface is changed, the proxy class should be changed accordingly.
2. A proxy class corresponds to only one target class. Other tasks need to redesign the proxy.