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