Design Mode learning 7-Proxy: proxy Mode

Source: Internet
Author: User
1. Proxy: proxy mode 1.1. Overview

L The proxy concept can be understood as: there is an intermediate layer between the starting point and the destination, meaning the proxy. Example: proxy server.

L scenario: There is an image viewing application where each image is large and loading consumes a lot of resources. We hope that a thumbnail is displayed on the interface. You can click the thumbnail, how can I compile this application to load actual images?

L Using proxy is a good method

1.2. Solution: use proxy Mode

Use proxy mode, such as writing code:

// Image Proxy: displays the thumbnail.

Class imageproxy {// proxy class

Private iimage;

Public imageproxy (iimage ){

This. iimage = iimage;

}

Public void displayimage (){

System. Out. println ("display thumbnail ");

}

Public void mousepress () {// mouse response, click the thumbnail

Iimage. displayimage ();

}

}

Interface iimage {

Public abstract void displayimage ();

}

Class imagedisplay implements iimage {// actual work class

Public void displayimage (){

System. Out. println ("show actual image"); // Resource Consumption

}

}

 

 

Public class proxy1 {

Public static void main (string ARGs []) {

Imageproxy proxy = new imageproxy (New imagedisplay (); // It can be assembled.

Proxy. displayimage (); // The client is only dealing with the proxy when calling.

Proxy. mousepress ();

}

}

 

1.3. Proxy mode Summary

L provides a proxy for other objects to control access to this object. For objects with high overhead, they are created only when they are used, this principle can save us a lot of valuable Java memory.

L examples

L 1. Users of different levels of authorization mechanisms have different access rights to the same object. You can write a proxy class to check user permissions and decide how to access them.

L 2. A client cannot directly operate on an object, but must interact with the object.

L as mentioned in the previous example

 

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.