18th Chapter, Agent mode
Proxy mode, also known as the delegation mode, is one of the structural design patterns. is one of the most widely used models.
1. Definition
Provides a proxy for other objects to control access to this object.
2. Usage Scenarios
When there is difficulty in accessing an object or accessing an object directly, it can be accessed indirectly through a proxy object, in order to ensure the transparency of client use, the delegate object and the proxy object need to implement the same interface.
3.UML class Diagram
(1) Subject: Abstract Topic class , declaring real topics and common interface methods, which can be abstract classes or interfaces.
(2) Realsubject: Real Topic class (delegated Class), especially the implementation of specific business logic methods.
(3) Proxy: Agent Class (delegate Class), which holds a reference to the real topic class, in the interface method implemented by invoking the corresponding interface method execution in the real topic class, as a proxy role.
4. Simple implementation
Examples in the book: an example of the process of Wang's lawsuit. Then need agent attorney agent, simple procedure: Submit the application –> to prove –> start defending –> litigation completed.
Litigation Interface Class:
publicinterface ILawsuit { /** * 提交申请 */ void submit(); /** * 进行举证 */ void burden(); /** * 开始辩护 */ void defend(); /** * 诉讼完成 */ void finish();}
The specific litigants are:
Public class xiaomin implements ilawsuit{ @Override Public void Submit() {//Wang applies for arbitrationSystem.out.println ("The employer is due to pay arrears at the end of this application! "); }@Override Public void Burden() {//Wang submits evidenceSystem.out.println ("This is the contract and the bank wages of the past year water!" "); }@Override Public void Defend() {//IrrefutableSystem.out.println ("There is no evidence, no need to say anything!" "); }@Override Public void Finish() {//ResultsSystem.out.println ("The lawsuit succeeds, the judgment boss now seven days the settlement salary!" "); }}
Acting Lawyer:
Public class Lawyer implements ilawsuit{ PrivateIlawsuit Mlawsuit;//hold a reference to a specific agent Public Lawyer(Ilawsuit lawsuit) { This. Mlawsuit = lawsuit; }@Override Public void Submit() {mlawsuit.submit (); }@Override Public void Burden() {Mlawsuit.burden (); }@Override Public void Defend() {mlawsuit.defend (); }@Override Public void Finish() {mlawsuit.finish (); }}
Start arbitration:
publicclass Client { publicstaticvoidmain(String[] args) { //构造出诉讼人小民 new XiaoMin(); //构造一个代理律师,并将小民传递进去 new Lawyer(xiaomin); //律师提交申请 lawyer.submit(); //律师进行举证 lawyer.burden(); //律师代小民辩护 lawyer.defend(); //完成诉讼 lawyer.finish(); }}
Results:
老板年底拖欠工资,特此申请仲裁!这是合同书和过去一年的银行工资流水!证据确凿,不需要再说什么!诉讼成功,判决老板即日起七天内结算工资!
We can also delegate other people, just need to implement Ilawsuit. The proxy mode above is also called a static proxy, that is, the class file of the proxy classes before the code is run already exists. On the contrary, of course there will also be dynamic agents, the following with dynamic proxy implementation of the above example:
Java provides a convenient dynamic proxy interface Invocationhandler, which we can implement:
public class dynamicporxy implements invocationhandler { private Object obj; //a reference to the proxy class public dynamicporxy (Object obj) {this . obj = obj; } @Override public Object invoke (Object Proxy, method, object[] args) throws Throw Able {//invokes the method of the proxy class object Object result = Method.invoke (obj, args); return result; }}
Here we invoke the specific proxy method by invoking the method.
Modified Client class:
Public classClient { Public Static void Main(string[] args) {//Construct a legal personIlawsuit Xiaomin =NewXiaomin ();//1. Static proxy //Construct an attorney and pass the Wang in //ilawsuit lawyer = new lawyer (xiaomin); //-------------------------------------- //2. Dynamic Agents //Construct a dynamic agentDynamicporxy proxy =NewDynamicporxy (Xiaomin);//Get the ClassLoader of the agent-type small peopleClassLoader loader = Xiaomin.getclass (). getClassLoader ();//dynamic construction of a surrogate lawyerIlawsuit lawyer = (ilawsuit) proxy.newproxyinstance (loader,Newclass[]{Ilawsuit.class}, proxy);//Lawyer Submit applicationLawyer.submit ();//lawyer to proveLawyer.burden ();//lawyer Daixiaomin DefenseLawyer.defend ();//Completion of litigationLawyer.finish (); }}
As a result, it can be seen that the dynamic agent handles n multiple proxy classes through a proxy class, which is essentially decoupling the agent from the agent. Relatively static agents can only be used for the implementation of a given interface proxy, if the interface is different then need to redefine the different proxy classes, more complex, but static agents more consistent with the object-oriented principle. Which method to use, according to personal preferences.
5.Android proxy mode in the source code to implement 1.ActivityManagerProxy proxy class
Activitymanager is a class that manages and maintains information about activity in Android, and in order to isolate it from Activitymanagerservice, it effectively reduces the coupling between them, In this middle of the Activitymanagerproxy proxy class is used, all access to Activitymanagerservice is converted to access to the proxy class, This activitymanager is decoupled from the activitymanagerservice.
6. Summary 1. Advantages
(1) Decoupling the agent from the agent.
(2) The proxy object acts as an intermediary between the client and the target object, which can be used to protect the target object.
2. Disadvantages
Basically no shortcomings, really want to say that the shortcomings of the design pattern is a common problem: the increase of the class.
"Android source design mode analysis and actual combat" reading notes (18)