Proxy-static proxy (proxy mode) in Design Mode)

Source: Internet
Author: User
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 ();}}

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.