A detailed description of the "design mode" proxy mode

Source: Internet
Author: User

Objective

Bo Master is only a junior students, the content of the article inevitably has shortcomings, welcome criticism.

Body

Reprint Please specify source: http://blog.csdn.net/h28496/article/details/46707621

Release date: July 1, 2015

What is proxy mode?

Provides a proxy for other objects to control access to this object. 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.

Proxy mode is often divided into remote agents, virtual agents, protection agents, and so on.

Structure of proxy mode UML class diagram


Role description

① Abstract Interface: The interface that the proxy class and the proxy class all need to implement, define some/some functions;

② is the proxy class: Defines the object being proxied;

③ proxy class: The proxy object contains an object that is proxied inside, so that the target object can be manipulated internally. The proxy class implements an interface that is the same as the proxy class so that the target object can be substituted at any time.

Preliminary implementation of proxy mode

Hypothetical scenario:

The teacher gave the girl 50KG things, need to move into the dormitory building, but they have limited power, can only move 25KG at a time.

So we need to set up a surrogate for girls, that is, boys.

The teacher gave the thing to the boy, the boy gave the girl 25KG of things to move until the end of the move.

(Boys can not enter the female dormitory ...) Can't just help them move. )

Topic Analysis:

First we need an interface that has a method CArray (int weight) that implements the function of moving things.

And then we're going to have a schoolgirl class girl, as a proxy class.

Second, there must be a boy, as a surrogate class.

Finally, there will be a client class.

Icarry.java

Package zhp.proxypattern;/** * Interface for transporting things for the boy class and Girl class implementations. * @author Zhenghaipeng * @since June 18, 2015 */public interface Icarry {public void carry (int weight);}


Girl.java

Package zhp.proxypattern;/** * Girl class, proxy class. * @author Zhenghaipeng * @since June 18, 2015 */public class Girl implements icarry{public final static int max_weight =; @Overridep Ublic synchronized void carry (int weight) {//can think about why there is this sync lock if (weight > Max_weight) {//over 25KG, Girls can't move System.out.println ("I can't move!! "); return;} System.out.println ("I put the" + weight + "kilograms of things finished. ");}}


Boy.java

Package zhp.proxypattern;/** * Proxy class * @author Zhenghaipeng * @since June 18, 2015 */public class Boy implements Icarry {private Girl gi RL = new Girl ();//has a proxy object private int mweight = 0; @Overridepublic void carry (int weight) {mweight + = Weight;while (Mweig HT >) {girl.carry (+); mweight-= 25;} if (mweight! = 0) {girl.carry (mweight);}}}


Client.java

Package zhp.proxypattern;/** * Clients * @author Zhenghaipeng * @since June 18, 2015 */public class Client {public static void main (String [] args) {Girl Girl = new Girl ();p Leasecarry (Girl, 50);//Let the girl move 50KG something System.out.println ("---------------------"); Boy boy = The new Boy ();p leasecarry (Boy, 50);} public static void Pleasecarry (Icarry carry, int weight) {carry.carry (weight);}}


Execution Result:


In this example, the female class is not suitable for use in the weight>25 situation, thus using the proxy class boy to buffer, protected by the proxy class.

Application of proxy mode in Android

Proxy mode is often used in Android development, for example: it can be time-consuming to perform an operation on Android, so you should avoid this kind of time-consuming operation in the main thread. A proxy class can be used to implement time-consuming interfaces, which are given to the proxy class in the main thread, and of course the proxy class will open up a sub-thread.

Hypothetical scenario:

We already have a class uploadpic that implements the Iupload interface, which can be used to upload images.

But in Android is not directly in the main thread to upload pictures to the server.

Of course we can create a new sub-thread to upload, but the code reusability is too low. If you need to modify a parameter later, you have to modify the child threads for each upload.

Using the proxy mode to deal with this problem is a good choice. Better yet, we can perform some other operations, such as checking the network, when the agent is available.

Analyze the problem:

First you have to have an interface: Iupload, the interface defines the upload operation.

There is also a proxy class: Uploadpic, implements the Iupload interface, and provides the basic function of uploading pictures.

There is another proxy class: Proxy_uploadpic, also implements the Iupload interface, which contains a Uploadpic object.

Iupload.java

Package zhp.pattern.proxypattern;/** * Upload interface.  * @author Zhenghaipeng * @since June 18, 2015 */public interface iupload {/** * upload files * @param path to upload file paths */public void upload (String path);}


Uploadpic.java

Package zhp.pattern.proxypattern;/** * Upload a picture class. * @author Zhenghaipeng * @since June 18, 2015 */public class Uploadpic implements Iupload {@Overridepublic void upload (String path) {/ /output: Uploading Zhp.android.debug.Debug.Log (This.getclass (). GetName (), "Uploading!" ");}}

Proxy_uploadpic.java

Package Zhp.pattern.proxypattern;import Android.content.context;import zhp.android.utils.utils_network;/** * proxy class, Used to upload a picture in a sub-thread * @author Zhenghaipeng * @since June 18, 2015 */public class Proxy_uploadpic implements Iupload {Uploadpic uploadpic; Context Context;public Proxy_uploadpic (context context) {This.context = Context;this.uploadpic = new Uploadpic ();} @Overridepublic void Upload (final String path) {//Detects network Boolean iswificonned = Utils_network.getinstance () first. Iswificonnected (This.context); if (!iswificonned) {//no wifi, output: WiFi is not connected, can't upload! Zhp.android.debug.Debug.Log (This.getclass (). GetName (), "WiFi is not connected and cannot be uploaded!" "); return;} Upload the new Thread (new Runnable () {public void run () {uploadpic.upload (Path)}). Start ();}}


Mainactivity.java

Package Zhp.pattern.proxypattern;import Android.app.activity;import android.os.bundle;/** * Client * @author 郑海鹏 * @since 20 June 18, 15 */public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//using proxy uploads, additional network status monitoring Uploadpicture (new Proxy_uploadpic (This), "Sdcard/a.png");} private void Uploadpicture (iupload upload, String path) {upload.upload (path);}}


Execution Result:

No WiFi


Have WiFi


With the proxy mode, we can simply implement the upload function, while attaching other functions. It also becomes very convenient to modify later, just modify it in the proxy class.


Advantages and disadvantages of proxy mode

The proxy class adds some extensions to the Proxied object, adding some other operations to the real target class, and the details of these operations are given to the agent for processing, and you only need to centralize the implementation of the target class functionality. At the same time to facilitate future changes.

Disadvantages

There will be many proxy classes, and the classes become much more.


About agent mode Dynamic agent, I want to put together with reflection. Estimated to be sent next week (^_^)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

A detailed description of the "design mode" proxy mode

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.