18th Chapter, Agent mode
Proxy mode, also known as the Trust 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 you can't or don't want to visit an object directly or have difficulty interviewing an object, you can access it indirectly through a proxy object to ensure the transparency of the client's use. The entrusted object and the proxy object need to implement the same interface.
3.UML class Diagram
(1) Subject: Abstract Theme Class . Declares a real topic and a common interface method, which can be an abstract class or interface.
(2) Realsubject: Real Topic class (entrusted Class). In particular, run a detailed business logic approach.
(3) Proxy: Agent Class (entrusted Class), this class holds a reference to the real topic class. It acts as an agent by invoking the corresponding interface method in the real topic class in the interface method it implements.
4. Simple implementation
Example in the book: an example of the process of Wang's lawsuit. Then need to act as a lawyer agent, simple procedure: Submit an application –> the proof –> start the defense –> litigation completed.
Litigation Interface Class:
publicinterface ILawsuit { /** * 提交申请 */ void submit(); /** * 进行举证 */ void burden(); /** * 開始辩护 */ void defend(); /** * 诉讼完毕 */ void finish();}
Detailed litigants:
Public class xiaomin implements ilawsuit{ @Override Public void Submit() {//Wang applies for arbitrationSystem.out.println ("The boss is in arrears at the end. hereby apply for arbitration. "); }@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() { //irrefutable System.out.println ( "There is no proof, no need to say anything!"
"); } @Override public void finish() { //结果 System.out.println("诉讼成功。判决老板即日起七天内结算工资!"); }}
Acting Lawyer:
Public class Lawyer implements ilawsuit{ PrivateIlawsuit Mlawsuit;//hold a reference to a detailed proxy 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:
老板年底拖欠工资,特此申请仲裁!这是合同书和过去一年的银行工资流水!证据确凿,不须要再说什么!
诉讼成功,判决老板即日起七天内结算工资!
The same we can act as other people, just need to realize 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.
So 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 agent interface Invocationhandler. Let's implement it:
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 detailed proxy method through the Invoke method.
The modified client class:
Public classClient { Public Static void Main(string[] args) {//Construct a legal personIlawsuit Xiaomin =NewXiaomin ();//1. Static proxy //Construction of an acting lawyer. 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 ();//Completed litigationLawyer.finish (); }}
The result is the same. It can be seen that the dynamic agent handles n multiple proxy classes through a proxy class, in fact, the agent and the agent are decoupled.
Relatively static proxy can only be a given interface for the implementation of the class proxy, assuming that the interface is different then need to define a different proxy class. is more complex, but static proxies are more consistent with the object-oriented principle. Use whichever method you prefer, depending on your preferences.
5.Android proxy mode in source code implements 1.ActivityManagerProxy proxy class
Activitymanager is a class that manages and maintains information about activity in Android, in order to isolate it from Activitymanagerservice, effectively reducing the coupling between them, In the middle of the use of the Activitymanagerproxy proxy class, all of the access to activitymanagerservice into the proxy class access to ask, This activitymanager is decoupled from the activitymanagerservice.
6. Summary 1. Strengths
(1) Decoupling the agent from the agent.
(2) The proxy object acts as an intermediary between the client and the target object, which can 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 addition of the class.
"Android source code design mode analysis and actual combat" reading notes (18)