Agent mode proxy for Android design mode

Source: Internet
Author: User
<span id="Label3"></p>I. Overview<p><p><span style="font-size:14px">Proxy mode is also one of the most commonly used design patterns, proxy mode is to provide a new object, the implementation of the real object of the operation, or become the real object of the Double. in daily life is also very common. for example a to rent, in order to save trouble A will find intermediary, intermediary will replace a to filter house, A sitting to enjoy the results of intermediary screening, and pay rent is also handed over to the intermediary, this is a typical daily life in the application of proxy Mode. usually open the Web page, the first open to the general is the text, and pictures and other large resources will delay loading, here is also the use of proxy mode.</span></p></p><p><p><span style="font-size:14px">The composition of the proxy mode:</span></p></p><p><p><span style="font-size:14px">Abstract Subject: An abstraction theme-a common interface for declaring Real-world themes and proxy topics</span></p></p><p><p><span style="font-size:14px"><span style="font-size:14px"> </span>Real Subject: true themes-real objects that need to be referenced by a proxy topic</span></p></p><p><p><span style="font-size:14px"><span style="font-size:14px"> </span>proxy Subject: agent Topic-because Proxysubject references Realsubject and implements the same interface as realsubject, Proxysubject can operate realsubject, You can also provide additional actions, such as Before & After</span></p></p><p><p><br></p></p><p><p><br></p></p><p><p><br></p></p><p><p><span style="font-size:14px"><span style="font-size:14px"></span> proxy mode is commonly used for scenario-based classification:</span></p></p><p><p><span style="font-size:14px"><span style="font-size:14px"> </span>1.Virtual proxy: virtual agent is actually through the proxy mode to consume the resources of the larger object to do a lazy load, that is, when to use this object to create it.</span></p></p><p><p><span style="font-size:14px"><span style="font-size:14px"> </span>2.Remote proxy: Remote Agent is a more classic application, similar to the C/S mode (mainly to intercept and control the remote method call, do proxy firewall and so on).</span></p></p><p><p><span style="font-size:14px"><span style="font-size:14px"> </span>3.Smart Reference proxy: The Smart Reference agent can provide some additional operations to the referenced object, such as implementing the actions inside the intermediary searching and prepare Contract.</span></p></p><p><p><span style="font-size:14px"><span style="font-size:14px"> </span>4.Access proxy; The protection agent can control access to an object and provide a series of rights management when Necessary.</span></p></p><p><p><span style="font-size:14px"><span style="font-size:14px"> </span>5.copy-on-write proxy: copy-on-write (clone) Proxy is actually a branch of virtual proxy, which provides copying (cloning) operations (deferred Copy) only when the object is actually Changed.</span></p></p><p><p><br></p></p><p><p><span style="font-size:14px"><span style="font-size:14px"></span> Advantages and disadvantages of proxy mode:</span></p></p><p><p><span style="font-size:14px"><span style="font-size:14px"></span> advantages:</span></p></p><p><p><span style="font-size:14px"><span style="font-size:14px"> </span>1. The agent acts as the middle layer of the calling and real objects, which reduces the coupling between the modules and the System.</span></p></p><p><p><span style="font-size:14px"><span style="font-size:14px"> </span>2. Can be a small object to proxy a large object, to optimize the system to improve the speed of the purpose</span></p></p><p><p><span style="font-size:14px"><span style="font-size:14px"> </span>3. Provision of realsubject rights management</span></p></p><p><p><span style="font-size:14px"><span style="font-size:14px"> </span>4. Easy to expand, Realsubject and Proxysubject are interface, realsubject change the business as long as the interface is unchanged, Proxysubject can not make any changes.</span></p></p><p><p><span style="font-size:14px"><span style="font-size:14px"></span> disadvantages:</span></p></p><p><p><span style="font-size:14px"><span style="font-size:14px"> </span>1. With merit 1, because the caller and the real object have an additional middle layer, so the time to call the response is increased</span></p></p>Two. Implement<p><p><span style="font-size:14px">Here take a find intermediary rental for demo to build proxy Mode.</span></p></p><p><p><span style="font-size:14px">1. General Agent</span></p></p><p><p><span style="font-size:14px">Define an abstract theme based on the scene, IHouse, provides three ways to obtain housing information, sign contracts and pay Rent.</span></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">/** * Created by Jesse on 15-7-24. */public interface IHouse { void Gethouseinfo (); void Signcontract (); void Payfees ();}</pre></pre><span style="font-size:14px"><span style="font-size:14px">next, define the real theme and implement the Ihouse Interface. add two property of house name and price, fill in the pretext method, the house name and price log out when obtaining the housing information; log out of the contract at the time of signing the contract, pay the rent when the log out of the price <span style="font-size:14px"></span> .</span></span><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">public class House implements ihouse{ private final String TAG = House.class.getSimpleName (); Private String name; Private Double price; Public house (String name, double price) { this.name = name; This.price = price; } @Override public void Gethouseinfo () { log.i (TAG, ' house info-name: ' + name + ' ¥: ' + price); } @Override public void signcontract () { log.i (TAG, "contract:" + name + " signed at" + new Simpledatef Ormat ("HH:mm:ss"). format (systemclock.uptimemillis ())); } @Override public void payfees () { log.i (TAG, "bill:name-" + name + " $-" + price);} }</pre></pre><span style="font-size:14px"><span style="font-size:14px">to define a housing agent, you also need to implement the Ihouse interface and hold the house reference. you can see that the proxy class is actually like a package house, which provides some additional operations, such as when a customer wants to look at a home and the agent retrieves the home information of his inventory. <span style="font-size:14px"></span> Prepare a contract or something before you sign the Contract.</span></span><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">public class Proxyhouse implements ihouse{ private final String TAG = ProxyHouse.class.getSimpleName (); Private IHouse house; Public proxyhouse (IHouse house) { this.house = house; } @Override public void Gethouseinfo () { log.i (TAG, "searching"); House.gethouseinfo (); LOG.I (TAG, "search finished"); } @Override public void signcontract () { log.i (TAG, "prepare contract"); House.signcontract (); } @Override public void payfees () { house.payfees (); }}</pre></pre><span style="font-size:14px"><span style="font-size:14px">for the customer, completely do not have direct interaction with house, here first define a home called Downton Abbey manor, rent 5k, set up a house agent, the Downton Abbey Manor entrusted to the Agent. the customer wants to find a house, sign a contract, pay rent directly to find an agent on the Line. <span style="font-size:14px"></span> </span></span><p><p></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java"> IHouse House = new house ("downton Abbey", "the"); IHouse proxyhouse = new Proxyhouse (house); LOG.I (TAG, "looking for a perfect house"); Proxyhouse.gethouseinfo (); LOG.I (TAG, "thinking"); Proxyhouse.signcontract (); Proxyhouse.payfees (); LOG.I (TAG, "so easy");</pre></pre><br><span style="font-size:14px"><span style="font-size:14px">the process of the entire proxy mode can be shown from the following timing Diagram. <span style="font-size:14px"></span> The client interacts only with the Agent.</span></span><br><br><p><p><span style="font-size:14px">2. Virtual Agent</span></p></p><p><p><span style="font-size:14px"><span style="font-size:14px"></span> virtual agents are described earlier, that is, based on the proxy model and do a lazy load to save memory, but if an object to be used in multiple places without a fixed sequence of time to be sentenced to empty, but also to some extent sacrificing performance (a bit like proxy mode + lazy mode). Here is also a rental example to SHOW.</span></p></p><p><p><span style="font-size:14px"><span style="font-size:14px"></span> This assumes that house is a very large object that is expensive to create, and we change it to initialize when custom needs it. this is where the Proxyhouse is constructed, and the reference to house is Empty. Then the house is initialized, of course, if there are multiple threads concurrency, you can lock or double check locks according to different scenarios to ensure thread Safety.</span></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java"> Public Proxyhouse () { if [null = = house] House = new house ("downton Abbey",); }</pre></pre><pre name="code" class="java"><pre name="code" class="java"> IHouse proxyhouse = new Proxyhouse (); LOG.I (TAG, "looking for a perfect house"); Proxyhouse.gethouseinfo (); LOG.I (TAG, "thinking"); Proxyhouse.signcontract (); Proxyhouse.payfees (); LOG.I (TAG, "so easy");</pre></pre><span style="font-size:14px"><span style="font-size:14px">3. Mandatory Agent</span></span><p><p></p></p><p><p><span style="font-size:14px"><span style="font-size:14px"></span> Force Agent is the opposite of the proxy mode, in general, the proxy mode is through the proxy to find the real object, and the Force agent is through the real object to find the proxy, that is, the real object to specify the agent, of course, The final access is still accessed through proxy Mode. from the name also can see it and other agents of a different, is to enforce the proxy. take the example of the general Agent above, Custom can not see the Entity's house when it only through the proxy to access, but because there is no limit, Custom can also directly bypass Proxyhouse to access house, but the mandatory agent has a limit, custom must pass through Proxyhouse to access house. just like some landlords have trouble, there are tenants directly call to see the room, The landlord gave an intermediary phone call that you contact the Intermediary.</span></p></p><p><p><span style="font-size:14px"><span style="font-size:14px"></span> first, You need to add an interface to the interface to get the Agent.</span></p></p><pre name="code" class="java"><pre name="code" class="java">public interface IHouse { void Gethouseinfo (); void Signcontract (); void Payfees (); IHouse GetProxy ();}</pre></pre><span style="font-size:14px"><span style="font-size:14px">Real objects implement the interface, and in the GetProxy to instantiate the proxy, while in other methods to do proxy judgment, only the use of their own custom agent will be normal. <span style="font-size:14px"></span> </span></span><p><p></p></p><pre name="code" class="java">public class House implements Ihouse{private final String TAG = House.class.getSimpleName (); Private String name; Private Double price; Private IHouse proxy; Public house (String name, double price) {this.name = name; This.price = price; } @Override public void Gethouseinfo () {if (isproxy ()) log.i (TAG, "house info-name:" + name + " ¥: "+ price); else log.i (TAG, "please use correct proxy"); } @Override public void Signcontract () {if (isproxy ()) log.i (TAG, "contract:" + name + "signed At "+ new SimpleDateFormat (" HH:mm:ss "). format (systemclock.uptimemillis ())); else log.i (TAG, "please use correct proxy"); } @Override public void Payfees () {if (isproxy ()) log.i (TAG, "bill:name-" + name + "$-" + pric e); else log.i (TAG, "please use correct proxy"); } @Override Public IHouse getproxy () {if (null = = Proxy) Proxy = new Proxyhouse (this); Return proxy; Private Boolean IsProxy () {if (null = = Proxy) return false; else return true; }}</pre><span style="font-size:14px"><span style="font-size:14px">The following results are returned if the House object is directly manipulated at this time or accessed through a custom-built proxy <span style="font-size:14px"></span> </span></span><p><p></p></p><p><p><br><span style="font-size:14px"><span style="font-size:14px"></span> So we have to use the proxy specified by the real object to be able to access it properly.</span></p></p><pre name="code" class="java"><pre name="code" class="java"> IHouse House = new house ("downton Abbey", "the"); House = House.getproxy (); LOG.I (TAG, "looking for a perfect house"); House.gethouseinfo (); LOG.I (TAG, "thinking"); House.signcontract (); House.payfees ();</pre></pre><p><p><span style="font-size:14px"><strong>But here the mandatory agent has a bug</strong>, the mandatory agent actually does not take effect, custom or can directly access house, for example, I use the following way to access, just create and get the agent through getproxy, But I do not use the agent or directly with the house instance to access, this time can still be normal access. you'll find a way to solve the bug and update it later.</span></p></p><p><p><span style="font-size:14px"></span></p></p><pre name="code" class="java"><pre name="code" class="java"> IHouse House = new house ("downton Abbey", "the"); House.getproxy ();//this is only created by GetProxy proxy log.i (TAG, "looking for a perfect house"); House.gethouseinfo (); LOG.I (TAG, "thinking"); House.signcontract (); House.payfees ();</pre></pre><p><p></p></p><p><p><span style="font-size:14px">4. Dynamic Agent</span></p></p><span style="font-size:14px"><span style="font-size:14px">the above is the first to write a good proxy class, so that the agent relationship is fixed, when the agent of multiple real objects to write multiple proxy classes, and will produce redundant code, scalability and maintainability are not high, The dynamic agent is based on reflection to implement the program in the process of deciding what object to proxy. the core idea of AOP is dynamic proxy. (the Dynamic proxy for Java is used Here.)</span></span><p><p></p></p><p><p><span style="font-size:14px">Since it is a dynamic agent do not need proxyhouse and do not need to implement the Ihouse interface, here write a Proxyhandler implementation Invocationhandler invoke interface, and provide a proxy instance based on proxy to the CUSTOM. the name of the method is printed before the actual object method is called by Reflection.</span></p></p><pre name="code" class="java"><pre name="code" class="java">public class Proxyhandler implements Invocationhandler{private final String TAG = Proxyh Andler.class.getSimpleName (); Object targetobj; public object Newproxyinstance (object targetobj) {this.targetobj = targetobj; return proxy.newproxyinstance (targetobj.getclass (). getclassloader (), targetobj.getclass (). getinterfaces (), this); } @Override public Object Invoke (object proxy, method method, object[] Args) throws Throwable {object ret; LOG.I (TAG, "method name:" + method.getname ()); ret = Method.invoke (targetobj, args); Return ret; }}</pre></pre><pre name="code" class="java"><pre name="code" class="java"> Proxyhandler proxy = new Proxyhandler (); IHouse House = (IHouse) proxy.newproxyinstance ("downton Abbey", "the new house"); LOG.I (TAG, "looking for a perfect house"); House.gethouseinfo (); LOG.I (TAG, "thinking"); House.signcontract (); House.payfees (); LOG.I (TAG, "so easy");</pre></pre><span style="font-size:14px"><span style="font-size:14px">The result shows that the method name is printed before the actual object is actually called, and some other object control can be done Here. <span style="font-size:14px"></span> </span></span><p><p></p></p><p><p><br><span style="font-size:14px"><span style="font-size:14px"></span> This time the sequence diagram of the entire process becomes the following, through the JDK proxy object and the reflection mechanism to support the dynamic Agent's core Functions.</span></p></p><p><p><br></p></p>Three. Summary<span style="font-size:14px">the <span style="font-size:14px">use of proxy mode is still quite a lot of, can reduce the complexity of the object, decoupling the project (especially the dynamic agent aop), etc., The Learning design pattern is actually the most suitable method is to use, in the scenario suitable for the mode of flexibility to use it to be a real master of a pattern.</span></span><br><br><br><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Reprint Please specify the source:</span></span>http://blog.csdn.net/l2show/article/details/46992495<br><p><p>Agent mode proxy for Android design mode</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.