Measure the test taker's understanding about the inter-process communication principles of the Android system. (1) -- the proxy mode in RPC

Source: Internet
Author: User
Process Communication in the Android system uses a lightweight RPC (Remote Procedure Call remote process call) and aidl (Android interface definination language) specification to generate two processes that can access each other. Code . RPC is implemented in the interface mode, and between the client and the called implementation is implemented in the proxy mode, which is based on the RMI and proxy mode of Java, to flexibly master this lightweight solution, it is necessary to straighten out the basic knowledge. Here we first understand the basics of the proxy mode and the knowledge about the proxy mode, the following Mind Map can be used to describe the actual use of a code example: 1. abstract class role code
 1   Package  Com. magc. proxy;
2
3 /**
4 * @ Author Magc
5 * Common abstract classes of proxy roles and real roles
6 */
7 Public Abstract Class Role {
8
9 // As a common interface between the proxy role and the real role, the proxy role can replace the real role to provide services.
10 Public Abstract Void Service (string user_id );
11
12 }
2. Real role code
 1   Package  Com. magc. proxy;
2
3 /**
4 * @ Author Magc
5 * Real Angle
6 * External access is not allowed
7 *
8 */
9 Public Class Realrole Extends Role {
10
11 /* (Non-javadoc)
12 * @ See COM. magc. Proxy. Role # Service ()
13 * Provide services
14 */
15 @ Override
16 Public Void Service (string user_id ){
17 System. Out. println ( " Real roles serve you ...... " );
18 }
19 // Verify user identity
20 Public Boolean Checkuser (string user_id)
21 {
22 Return True ;
23 }
24
25 }
3. Proxy proxyrole code:
 1   Package  Com. magc. proxy;
2
3 /**
4 * @ Author Magc
5 * Proxy role
6 * Develop interfaces for clients
7 * You can directly reference a real role instance and forward client requests to a real role instance.
8 * Some additional operations can be added before or after the request is forwarded.
9 */
10 Public Class Proxyrole Extends Role {
11 Private Realrole = Null ;
12
13 /* (Non-javadoc)
14 * @ See COM. magc. Proxy. Role # Service ()
15 */
16 @ Override
17 Public Void Service (string user_id ){
18 System. Out. println ( " The proxy role serves you ...... " );
19 // Create a real role instance only when necessary
20 Realrole = New Realrole ();
21 // Additional operations: Authentication
22 System. Out. println ( " Identity Verification ...... " );
23 If ( ! Realrole. checkuser (user_id ))
24 Return ;
25 System. Out. println ( " Find a real role instance to help with the transaction ...... " );
26 Realrole. Service ( " Magc " );
27
28 System. Out. println ( " Thank you for choosing ...... " );
29 }
30
31 }
4. Testing class roletest class code
 1  Package  Com. magc. proxy;
2
3 /**
4 * @ Author Magc
5 * Agent Mode Test
6 * Acts as a client to request and call the proxy interface.
7 * The client can only access the proxy class, but cannot access the real role class.
8 */
9 Public Class Proxytest {
10
11 /**
12 * @ Param ARGs
13 */
14 Public Static Void Main (string [] ARGs ){
15
16 Proxyrole proxy = New Proxyrole ();
17 Proxy. Service ( " Magc " );
18 }
19
20 }
Run the test class. The output result on the console is as follows: errors are inevitable due to the rush of time. I hope I can give some inspiration to you.

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.