The proxy mode involves 1. abstract role: Declares the common interfaces of real objects and proxy objects. 2. proxy role: the proxy object role contains a reference to the real object to operate on the real object. At the same time, the proxy object provides the same interface as the real object so that it can replace the real object at any time. At the same time, the proxy object can append other operations when performing real object operations, and want to encapsulate real objects. 3. Real role: the real object represented by the proxy role is the final object to be referenced. 4. the customer needs to call the request Method in RealSubject. Now the ProxySubject is used to proxy the Realsubject class, which also achieves the goal and encapsulates other methods (preRequest, afterRequest ), other problems can be solved. 5. If you want to use the proxy mode as described above, the real role must already exist and act as the internal attribute of the proxy object. However, in actual use, I. A real role must correspond to a proxy role. If a large number of roles are used, the class will be very inflated. In addition, if you do not know the real role in advance, how do you use the proxy? This problem can be solved through the dynamic proxy class of Java.
/*** Proxy. java * proxy ** function: Todo ** ver Date Author * ── ─ * 2011-6-8 Leon ** copyright (c) 2011, TNT All Rights Reserved. */package proxy;/*** classname: proxy * function: Todo add function * reason: todo add reason ** @ author Leon * @ version * @ since ver 1.1 * @ date 2011-6-8 */abstract class subject {public abstract void request ();} class realsubject extends subject {@ overridepublic void request () {// todo auto-generated method stubsystem. out. println ("this is my real request... ") ;}} public class proxy extends subject {/** proxy will contain references to realsubject **/private realsubject; @ overridepublic void request () {// todo auto-generated method stub // before calling the real (optional) This. prerequest (); If (realsubject = NULL) {// call the real role realsubject = new realsubject ();} realsubject. request (); // after calling the real (optional) This. afterrequest ();} private void prerequest () {system. out. println ("before real request ..... ");} private void afterrequest () {system. out. println ("after the real request ...... ");} public static void main (string... ARGs) {subject = new proxy (); Subject. request ();}}