Android design mode-proxy mode

Source: Internet
Author: User

1. Definition:

Provides a proxy for other objects to control access to this object.
1. Function:
In some cases, an object does not fit or cannot directly reference another object, and a proxy object can act as an intermediary between the client and the target object.
3. Function:
A proxy object can act as an intermediary between the client and the target object, which plays a role and protects the target object.
4. Division:
Agents are also divided into remote agents, virtual agents, protection agents and smart pointers;


A simple demo is written below;

Where the bank is a simple object, but does not want to modify it accordingly,

Then, use Bankproxy Agent to access the Bank object, to hold the bank's use, and to protect the role, etc.;

The first is the interface of the bank;

Package com.example.demo.proxy;/** * Bank interface * @author Qubian * @data June 3, 2015 * @email [EMAIL protected] * */public Interfa Ce bankinterface {public string setbankname (string name);p ublic 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 [EMAIL protected] * */public class Bank implements bankinterface{/** * needs to be related to the name of the banks */@Overridepublic string Setbankname (string name) {return name+ "bank _001 ";} @Overridepublic void Addcount () {//Specific operation}}

Proxy classes, specific operations:

Package com.example.demo.proxy;/** * Proxy class, I do not want to directly access the Bank class, that is, do not want to modify the bank class * So I can use proxy * @author Qubian * @data June 3, 2015 * @email [email Protected] * */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 ();} In the agent's internal is actually used by the proxy class//Only can add the agent can also add their own operation string SetName =mbank.setbankname (name);//other Operations return setName;} @Overridepublic void Addcount () {if (mbank==null) {mbank = new Bank ();} Mbank.addcount ();//Other Operations}}

Use:

Package Com.example.demo.proxy;public class Userproxy {public void Usebank () {Bankinterface Bank =new bankproxy (); Bank.setbankname ("China Merchants Bank"); Bank.addcount ();}}

So, there are a lot of places in Android that use proxies:


1. The aidl used in the process communication between Android, the system uses AIDL to define a remote service, is a remote proxy mode; link


2, the source code in the Activitymanagerproxy class is an agent, it is the agent of activitymanagernative, their common interface Iactivitymanager,activitymanager is the use of it;





Android design mode-proxy mode

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.