Static proxy for the proxy role Java design pattern

  • 2020-04-01 01:52:58
  • OfStack

      Java dynamic proxy pattern
Proxy: a role performs specific work on behalf of another role.
    For example: the relationship between the producer, the middleman and the customer;
                      Customers do not buy products and direct contact with the manufacturer, also do not know how the product is produced, the customer only deal with the middleman, and the middleman can be on the product some packaging, for some after-sales service.

      The proxy pattern has three roles: 1. Abstract topic role 2. Proxy topic role 3

     
So let's do a static proxy reality.
Let me take a tank.


Abstract theme role: Moveable


package com.gjy.proxy;
    public interface Moveable {
 void move();
}

      Proxy subject role: TanktimeProxy

package com.gjy.proxy;
public class TanktimeProxy implements Moveable{
  private Moveable t;

  public TanktimeProxy(Moveable t) {
   super();
   this.t = t;
  }

  @Override
  public void move() {
   long time1 = System.currentTimeMillis();
   System.out.println("time1="+time1);
   t.move();
   long time2 = System.currentTimeMillis();
   System.out.println("time2="+time2);
   System.out.println(" Run-time is :"+(time2-time1));
  }
}

      Proxy object: Tank

package com.gjy.proxy;
public class Tank implements Moveable{
  @Override
  public void move() {
   System.out.println("TanK moving........");
  }

}

      Testing:

package com.gjy.proxy;
public class TestTank {
  public static void main(String[] args) {
   Tank t = new Tank();
   Moveable move = new TanktimeProxy(t);
   move.move();

  }
}
   

      Next I want to add diary before and after TanK's move() method:

      I have to write another class to realize this work:


package com.gjy.proxy;
public class TanklogProxy implements Moveable{
  private Moveable t;

  public TanklogProxy(Moveable t) {
   super();
   this.t = t;
  }

  @Override
  public void move() {
   System.out.println("start move........");
   t.move();
   System.out.println("end move......");
  }
}

Testing:

package com.gjy.proxy;
public class TestTank {
 public static void main(String[] args) {
   Tank t = new Tank();
   Moveable move = new TanktimeProxy(t);
   Moveable movet = new TanklogProxy(move);
   movet.move();

  }
}

In this way, I added the energy work of diary and time statistics before and after the Tank's move() method by proxy. Since both TanktimeProxy and TanklogProxy realized the connection of Moveable port, TanklogProxy could proxy TanktimeProxy and vice versa. The proxy order of Tank could be exchanged.

If I want to move in Tank () method is to use a callback can add more before and after the work, whether it is going to write more agents subject role, this sample will make the code generation too obesity, not easy to maintain, and that is there any way I can handle that, case answer is ok, we can dynamically to generation agent subject role, all to the agent by proxy objects, that is the dynamic proxy.

At the end of the article, I will share some jokes from programmers: IBM and Boeing 777
The Boeing 777 is the first aircraft ever designed and built entirely in computer virtual reality, using equipment supplied entirely by IBM. Before the test flight, the President of Boeing warmly invited the technical director of IBM to take part in the test flight, but the director said, "oh, it's a great honor, but it's my wife's birthday, So..." .
Boeing's chief engineer was furious. "I didn't tell you the date, chicken!"


Related articles: