Java proxy mode one: static proxy

Source: Internet
Author: User

Proxy mode:

The role of proxy mode is to provide a proxy for other objects to control access to specific objects .

In some cases, one customer does not want or directly refer to another object, whereas a proxy object can act as a mediator between the client and the target Object: by proxy object Reference.

The role that agent mode typically involves:

  abstract Role : a common interface for declaring real objects and proxy objects. Can be an abstract class or an interface.

  Proxy Role : The proxy object role contains a reference to the real object, allowing you to manipulate the real object while 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 attach other actions when performing the real object operation, which is equivalent to encapsulating the real object.

  real role : The real object represented by the proxy role, complete the actual Operation.

Example 1:

Abstract class Roles:

/**/publicabstractclass  Subject {      Publicabstractvoid  request ();

  Real Characters:

/**/publicclassextends  subject{    @Override  public void Request () {        System.out.println ("from Real Subject");}    }

  Agent Role:

/** Proxy Role * Contains references to real objects, and encapsulation functions*/ public classProxysubjectextendsSubject {//the Proxy Role object contains a reference to the real object inside    PrivateRealsubject realsubject; @Override public voidRequest () {//operations that are attached before a real object operationprerequest (); if(NULL==Realsubject) {realsubject=NewRealsubject (); }                //what real characters do.realsubject.request (); //actions attached after a real role operationpostrequest (); }        Private voidprerequest () {System.out.println ("Pre Request"); }    Private voidpostrequest () {System.out.println ("Post Request"); }}

  Client:

 public class Client {    publicstaticvoid  main (string[] Args) {                //  Implement the proxy class        new  proxysubject ();        Subject.request ();    }}

Example 2:

Abstract class Roles:

// Interface Interface clothfactory{    void  productcloth ();}

  The proxy class (that is, the real role):

// the proxy class, which is the real class class Implements clothfactory{    @Override    publicvoid  productcloth () {        System.out.println ("a batch of garments produced by the Nike factory");}    }

  Proxy class:

class Implements clothfactory{    clothfactory cf;     // When you create a proxy object, you actually pass in a proxy class object     public proxyfactory (clothfactory cf) {        this. CF= cf;    }    @Override    publicvoid  productcloth () {        System.out.println (" The proxy class begins Execution. Charge of agency fees ");        Cf.productcloth ();    }}

  Client:

 public class testclothproduct {    publicstaticvoid  main (string[] Args) {         // Real class creation        New nikeclothfactory ();         // proxy class creation        New proxyfactory (nike);         // proxy class to complete real class work         proxy.productcloth ();    }}

Problem:

The consequences of static proxies Are:

If there is a large number of interfaces, then the proxy class and the real class need to be paired, resulting in a rapid expansion of the class. This requires the use of a Java dynamic Agent.

 

Java proxy mode one: static proxy

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.