Analysis of Android source code design patterns and practices (18th)

Source: Internet
Author: User

Analysis of Android source code design patterns and practices (18th)
Chapter 2 proxy Mode

The proxy mode is also called the delegate mode, which is one of the structural design modes. Is one of the widely used models.

1. Definition

Provides a proxy for other objects to control access to this object.

2. Use Cases

When it is difficult to directly access an object or access an object, a proxy object can be used for indirect access. To ensure the transparency of the client, the delegate object and proxy object must implement the same interface.

3. UML class diagram

(1) Subject:Abstract topic categoryTo declare the real topic and common interface methods. This class can be an abstract class or interface.

(2) RealSubject:Real themes(Delegated class), especially to execute specific business logic methods.

(3) Proxy:Proxy(Delegate class), which holds a reference to the real topic class, and calls the corresponding interface method execution in the real topic class in the implemented interface method to act as a proxy.

4. Simple implementation

Example in the book: the procedure of small litigation is used as an example. You need to represent a legal representative. The procedure for litigation is as follows: submit an application, submit proof, start a defense, and complete the lawsuit.

Litigation interface:

Public interface ILawsuit {/***** submit application */void submit ();/***** for proof */void burden (); /***** start defense */void defend ();/***** litigation completed */void finish ();}

The specific legal representative is Min:

Public class XiaoMin implements ILawsuit {@ Override public void submit () {// small people apply for arbitration System. out. println ("the boss is in arrears at the end of the year and hereby applies for arbitration! ") ;}@ Override public void burden () {// small people Submit Evidence System. out. println (" this is the contract and the Bank's salary flow for the past year! ") ;}@ Override public void defend () {// tiezheng mountain System. out. println (" the evidence is conclusive and you don't have to say anything! ") ;}@ Override public void finish () {// result System. out. println (" the lawsuit is successful, and the boss is ordered to settle his salary within seven days from now! ");}}

ATTORNEY:

Public class Lawyer implements ILawsuit {private ILawsuit 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:

Public class Client {public static void main (String [] args) {// construct the legal representative ILawsuit xiaomin = new XiaoMin (); // construct an attorney, and pass the small people to ILawsuit lawyer = new Lawyer (xiaomin); // The lawyer submits the application for lawyer. submit (); // lawyer. burden (); // lawyers defend lawyer on behalf of the minor. defend (); // complete the lawsuit lawyer. finish ();}}

Result:

My boss is in arrears at the end of the year. I hereby apply for arbitration! This is the contract and the Bank's salary for the past year! The evidence is conclusive and you don't have to say anything! The lawsuit was successful, and the boss was ordered to settle his salary within seven days from now!

We can also act on behalf of others, just implement ILawsuit. The above proxy mode is also called static proxy, that is, the class file of the proxy class already exists before the code is run. On the contrary, there will also be dynamic proxies. The following uses dynamic proxies to implement the above example:

Java provides a convenient dynamic proxy interface InvocationHandler, which we can implement:

Public class DynamicPorxy implements InvocationHandler {private Object obj; // public DynamicPorxy (Object obj) {this. obj = obj ;}@ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {// call the method Object result = Method of the proxy class Object. invoke (obj, args); return result ;}}

In this example, the invoke method is used to call the specific proxy method.

The modified Client class:

Public class Client {public static void main (String [] args) {// construct the legal representative ILawsuit xiaomin = new XiaoMin (); // 1. static proxy // construct an attorney and pass the representative into it // ILawsuit lawyer = new Lawyer (xiaomin); // ----------------------------------------/2. dynamic proxy // construct a dynamic proxy DynamicPorxy proxy = new DynamicPorxy (xiaomin); // obtain the ClassLoader loader = xiaomin. getClass (). getClassLoader (); // dynamically construct a Proxy lawyer ILawsuit lawyer = (ILawsuit) Proxy. newProxyInstance (loader, new Class [] {ILawsuit. class}, proxy); // a lawyer submits an application for lawyer. submit (); // lawyer. burden (); // lawyers defend lawyer on behalf of the minor. defend (); // complete the lawsuit lawyer. finish ();}}

The result remains unchanged. It can be seen that the dynamic proxy processes N more proxy classes through one proxy class. Its essence is to decouple the proxy and the proxy. Relatively speaking, static proxy can only act as proxy for the Implementation classes under the given interface. If the interfaces are different, different proxy classes need to be redefined, which is complicated, but static proxy is more in line with the object-oriented principle. Which method is used based on your preferences.

5. Implement the proxy mode in the Android Source Code 1. ActivityManagerProxy proxy class

ActivityManager is a class for Android to manage and maintain Activity-related information. To isolate it from ActivityManagerService and effectively reduce the coupling between the two, the ActivityManagerProxy proxy class is used, all access to ActivityManagerService is converted into access to the proxy class, so that ActivityManager is decoupled from ActivityManagerService.

6. Summary 1. Advantages

(1) decouple 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

There are basically no shortcomings. The shortcoming is the common problem of the design model: the increase in classes.

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.