Android design mode-Agent Mode, android design mode --

Source: Internet
Author: User

Android design mode-Agent Mode, android design mode --

1. Definition:

Provides a proxy for other objects to control access to this object.
2. Use:
In some cases, an object is not suitable or cannot directly reference another object, and the proxy object can play a mediation role between the client and the target object.
3. functions:
Proxy objects can play a mediation role between the client and the target object, which plays a role and protects the target object.
4. Division:
Proxy is also divided into remote proxy, virtual proxy, protection proxy and smart pointer;


Below is a simple demo;

The Bank is a simple object, but you do not want to modify it,

Then, the BankProxy is used to access the Bank object to hold the use of the Bank and protect it;

First, the bank interface;

Package com. example. demo. proxy;/*** bank interface * @ author qubian * @ data June 3, 2015 * @ email naibbian@163.com **/public interface BankInterface {public String setBankName (String name ); public void addCount ();}

Next is the object that has been written and needs to be protected;

Package com. example. demo. proxy; /*** Bank Implementation ** @ author qubian * @ data June 3, 2015 * @ email naibbian@163.com **/public class Bank implements BankInterface {/*** needs to be related to the name of the Bank operation */@ Overridepublic String setBankName (String name) {return name + "Bank_001" ;}@ Overridepublic void addCount () {// specific operation }}

Proxy class, specific operations:

Package com. example. demo. proxy;/*** Proxy class, I do not want to access directly, Bank class, that is, you do not want to modify the Bank class * so that I can use the proxy * @ author qubian * @ data June 3, 2015 * @ email naibbian@163.com */public class BankProxy implements BankInterface {private Bank mBank; public void setmBank (Bank mBank) {this. mBank = mBank ;}@ Overridepublic String setBankName (String name) {if (mBank = null) {mBank = new Bank ();} // The proxy class is actually used inside the proxy. // you can add the proxy or your own operation String setName = mBank. setBankName (name); // other operations return setName;} @ Overridepublic void addCount () {if (mBank = null) {mBank = new Bank ();} mBank. addCount (); // other operations }}

Usage:

Package com. example. demo. proxy; public class UserProxy {public void useBank () {BankInterface bank = new BankProxy (); bank. setBankName ("China Merchants bank"); bank. addCount ();}}

In Android, proxies are used in many places:


1. AIDL is used for inter-process communication in Android. In the system, AIDL is used to define a remote service, which is a remote proxy mode. Link


2. In the source code, the ActivityManagerProxy class is a proxy, which is the agent of ActivityManagerNative. The IActivityManager interfaces are used in the ActivityManager class;





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.