Proxy mode of design mode

Source: Internet
Author: User

Proxy pattern, one class represents the functionality of another class. This type of design pattern is part of a structural pattern.

Intent: provides a proxy for other objects to control access to this object.

explanation : It's like a star is usually a broker who takes over the job of managing stars. The general job is to visit a star through a broker.

implementation : Proxy mode has static agent and dynamic agent, dynamic agent and JDK agent and cglib agent


Static Proxy:

public interface Testadd () {

int add (int a, int b);


public class Testaddimpl implements Testadd() {

  int add (int a, int b) {

return a+b;



public class Testaddproxy implements Testadd() {

Private testadd testadd;

  Testaddprox (testadd testadd) {

This. Testadd = testadd;


int add (int a, int b) {

The work that can be done before the concrete implementation

int result = TEstadd.add (A, b);

The work that can be done after the concrete implementation

return result ;



The above code belongs to the static proxy. In the code, we define an interface Testadd, a concrete implementation class TestaddimpL and a proxy class Testaddproxy. before and after we actually call the proxy class, we can do some things.

Disadvantage: The static proxy constructs the corresponding proxy class for each Proxied object, and then writes many similar code repeatedly.

Dynamic Agent:

The JDK dynamic Agent provides the ability to generate dynamic proxy classes using the proxy class and the Invocationhandler interface in the Java.lang.reflect package.

Public class Testaddproxy implements Invocationhandler {

private Object obj;
public Object getproxy(object obj) {
This. obj= obj;
Get proxy Object
Return Proxy.newproxyinstance (Obj.getclass (). getClassLoader (),
Obj.getclass (). Getinterfaces (), this);
Mark 1
public object invoke (object proxy, Method method, object[] args)
Throws Throwable {
This.dobefore ();
Result=method.invoke (target, args);
This.doafter ();
return result;

public void Dobefore () {}

  public void Doafter (){}

By tag 1 we can see that the implementation class that needs to be used to the interface when using the JDK proxy

cglib Dynamic agent (import package before using):

Public class testaddproxy implements methodinterceptor{

Private enhancer enhancer = new enhancer ();

Public Object GetProxy (Class clazz) {

Set up classes that need to create subclasses

Enhancer.setsuperclass (Clazz);

Enhancer.setcallback (this);

Dynamically creating subclass instances with bytecode technology

return Enhancer.create ();


Implementing the Methodinterceptor interface method

public object intercept (object obj, Method method, object[] args,

Methodproxy proxy) throws Throwable {

This.dobefore ();

Object result = Proxy.invokesuper (obj, args);

this.doafter ();

return result;


 public void Dobefore () {}

Public void doafter (){}


Cglib is implemented for the class to implement the proxy, his principle is to create a subclass of the specified target class, and override the method implementation enhancements, but because of the inheritance, so the final decorated class can not be proxied

Dynamic Proxy is a proxy class implementation object that dynamically generates a specific delegate, and it does not need to implement proxy classes one at a to each delegate class.

Proxy mode of design mode

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