Design Mode-Proxy)

Source: Internet
Author: User

Provide a replacement or placeholder for another object to control access to this object.

Composition of proxy mode:

Topic interface (Subject ):The common method for extracting real themes and proxies can also be used in place of real themes.

RealSubject: the class that actually processes requests.

Proxy: the Proxy class of the real topic. The requester indirectly calls the real topic by calling the Proxy object.

A simple example of proxy mode:

(Topic Interface Class) Subject. java

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vcd4kphbyzsbjbgfzcz0 = "brush: java;"> package xu. neng. Proxy; public interface Subject {public void request ();}
(Positive theme class) RealSubject. java

Package xu. neng. Proxy; public class RealSubject implements Subject {@ Overridepublic void request () {System. out. println ("dare to call me !!! ");}}

(Proxy class) Proxy. java

Package xu. neng. proxy; // The interface must inherit from the Proxy object. public class Proxy implements Subject {RealSubject realSubject; public Proxy () {this. realSubject = new RealSubject ();} // pass in the Proxy object public Proxy (RealSubject realSubject) {this. realSubject = realSubject;} // call method @ Overridepublic void request () {realSubject. request ();}}

(Test class, that is, Client) Test. java

Package xu. neng. Proxy;/*** test Proxy mode. (Equivalent to the Client in the UML diagram) ** @ author neng **/public class Test {public static void main (String [] args) {// I want to call RealSubject to play Proxy proxy = new Proxy (new RealSubject (); proxy. request ();}}


Proxy Mode Applications:


It is commonly used in iOS development. The proxy mode is more like an idea. It uses intermediate means to control the access to a real object and can also be packaged.

Firewall Proxy: controls access to network resources and protects themes from attacks by bad customers.
Intelligent reference Proxy: When a topic is referenced, additional actions can be performed. Such as reference count.
Cache Proxy: provides temporary storage for overhead computation results. Multiple customers are allowed to share the results to reduce computing or network latency. (In essence, you can do other things)
Synchronous Proxy: Provides secure access to topics when multiple threads are used.
Copy proxy during write: used to control (Delay) object replication until the customer really needs it.


Reference an interesting article: http://yangguangfu.iteye.com/blog/815787


Reprinted please indicate the source: http://blog.csdn.net/xn4545945



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.