Java static Proxy

Source: Internet
Author: User

Java static Proxy

Static proxies are often used to augment legacy business logic. For example, a class that holds a two-party package and calls some of these methods. Then for some reason, such as logging, printing method execution time, but it is not good to write these logic to the two-party package method. So you can create a proxy class implementation and the two-party method the same way, by having the proxy class hold the real object, and then invoke the proxy class method in the original code to achieve the purpose of adding the business logic that we need.

This is actually an implementation of proxy mode, through the encapsulation of real objects, to achieve extensibility.

A typical proxy pattern typically has three roles, which is called a proxy three-factor * *

Package Singleton;

public class Testproxy {
public static void Main (string[] args) {
Subject Subject = new Proxysubject ();
Subject.request ();
}
}

Abstract Roles

Abstract class Subject {
public abstract void request ();
}

Real characters

Class Realsubject extends Subject {
@Override
public void request () {
System.out.println ("from real subject");
}

}

Agent role

Class Proxysubject extends Subject {
Private Realsubject realsubject;//Proxy role internal references the real role

@Override
public void request () {
This.prerequest ();//The operation that was attached before the real role operation
if (null = = Realsubject) {
Realsubject = new Realsubject ();
}
Realsubject.request ();//Real role lock complete things
This.postrequest ();//The operation attached after the real role operation
}

private void Prerequest () {
SYSTEM.OUT.PRINTLN ("Pre request");
}

private void Postrequest () {
System.out.println ("POST request");
}

}

Operation Result:

Pre Request
From Real subject
POST request

You can also use interfaces to represent abstract roles

Common interface
public interface Action {    public void doSomething();}
Real objects
public class RealObject implements Action{ public void doSomething() { System.out.println("do something"); }}
Proxy Object
public class Proxy implements Action { private Action realObject; public Proxy(Action realObject) { this.realObject = realObject; } public void doSomething() { System.out.println("proxy do"); realObject.doSomething(); }}

Run code

    new Proxy(new RealObject());    proxy.doSomething();

This proxy mode is also the simplest, is to hold the Realobject reference through proxy, and a layer of encapsulation.

Advantages and disadvantages of static proxies

First look at the advantages of proxy mode: Extend the original function, do not invade the original code.

Then look at the disadvantages of this proxy mode:

If there is such a demand, there are 10 different realobject, at the same time we have to go to proxy method is different, than to proxy method: DoSomething, Doanotherthing, dotwoanotherthing, add Agent before, The original code might be like this:

realObject.doSomething();realObject1.doAnotherThing();realObject2.doTwoAnother();

To solve this problem, we have a plan one:

To create different proxy classes for these methods, the following proxy code is:

proxy.doSomething();proxy1.doAnotherThing();proxy2.doTwoAnother();

Of course, there are two options:

By creating a proxy that holds different realobject, implements the Action1, Action2, and Action3 interfaces to make the code look like this:

proxy.doSomething();proxy.doAnotherThing();proxy.doTwoAnother();

So your proxy model will turn out this way:

There is no doubt that just to extend the same functionality in the scenario one, we will repeatedly create multiple logic the same, just realobject referencing different proxies.

In program two, it will cause the expansion of proxy, and this kind of expansion is often meaningless. In addition, if the method signature is the same, it is more necessary to introduce additional judgment logic at the time of invocation.

Package Test;

public class Testproxy {
public static void Main (string[] args) {
Action1 action1 = new Proxysubject ();
Action1.requestfirstthing ();
Action2 action2 = new Proxysubject ();
Action2.requestsecondthing ();
Action3 Action3 = new Proxysubject ();
Action3.requestthirdthing ();

}
}

Interface Action1 {
void Requestfirstthing ();
}

Interface Action2 {
void Requestsecondthing ();
}

Interface Action3 {
void Requestthirdthing ();
}

Class RealSubject1 implements Action1 {
@Override
public void requestfirstthing () {
System.out.println ("from Real Subject1");
}

}

Class RealSubject2 implements Action2 {

@Override
public void requestsecondthing () {
System.out.println ("from Real Subject2");
}
}

Class RealSubject3 implements Action3 {

@Override
public void requestthirdthing () {
System.out.println ("from Real subject3");
}
}

Class Proxysubject implements Action1, Action2, Action3 {
Private RealSubject1 realsubject1;//Proxy role internal references the real role
Private REALSUBJECT2 realsubject2;//Proxy role internal references the real role
Private REALSUBJECT3 realsubject3;//Proxy role internal references the real role

@Override
public void requestfirstthing () {
This.prerequest ();//The operation that was attached before the real role operation
if (null = = RealSubject1) {
RealSubject1 = new RealSubject1 ();
}
Realsubject1.requestfirstthing ();//Real role lock complete things
This.postrequest ();//The operation attached after the real role operation
}

private void Prerequest () {
SYSTEM.OUT.PRINTLN ("Pre request");
}

private void Postrequest () {
System.out.println ("POST request");
}

@Override
public void requestthirdthing () {
This.prerequest ();//The operation that was attached before the real role operation
if (null = = RealSubject2) {
RealSubject2 = new RealSubject2 ();
}
Realsubject2.requestsecondthing ();//Real role lock complete things
This.postrequest ();//The operation attached after the real role operation

}

@Override
public void requestsecondthing () {
This.prerequest ();//The operation that was attached before the real role operation
if (null = = RealSubject3) {
RealSubject3 = new RealSubject3 ();
}
Realsubject3.requestthirdthing ();//Real role lock complete things
This.postrequest ();//The operation attached after the real role operation
}
}

Operation Result:

Pre Request
From Real Subject1
POST request
Pre Request
From Real Subject3
POST request
Pre Request
From Real Subject2
POST request

Java 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.