Agent role Java design mode static proxy detail _java

Source: Internet
Author: User

Java Dynamic Proxy mode
Agent: A role represents a role in order to achieve certain specific energy work.
For example: Manufacturers, middlemen, customers, the relationship between the three
Customers do not buy products and directly with the manufacturer to deal with, also use do not know how the product is produced, the customer only with the middleman, and the middleman can travel some packaging products, for some after-sale services.

The agent mode has three roles: 1. Abstract theme role 2. Agent Theme Role 3. Inter-agent role


Face down we have a static proxy reality.
I take a tank for example.


Abstract Theme role: Moveable

Copy Code code as follows:

Package com.gjy.proxy;

public interface Moveable {
void Move ();
}


Agent Theme role: Tanktimeproxy
Copy Code code as follows:

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 ("When the runtime is:" + (time2-time1));
}
}


Inter-real agents: Tank
Copy Code code as follows:

Package com.gjy.proxy;

public class Tank implements moveable{

@Override
public void Move () {
System.out.println ("TanK moving ...");
}

}


Test:
Copy Code code as follows:

Package com.gjy.proxy;

public class Testtank {
public static void Main (string[] args) {
Tank t = new Tank ();
Moveable move = new Tanktimeproxy (t);
Move.move ();

}
}

I want to add a diary around the tank Move () method:

I have to write another class to realize this work:

Copy Code code as follows:

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 ...");
}
}


Test:
Copy Code code as follows:

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 ();

}
}


So I through the agent in the tank Move () method before and after adding a diary and statistical energy, because tanktimeproxy,tanklogproxy are realistic moveable mouth, So tanklogproxy can be agent Tanktimeproxy, in turn, they can be exchanged for the tank of the agent order.

If I want to add more energy to the tank move () method with the adjustment, whether it is to write more agent theme role, this sample will make the code generation too fat, not easy to maintain, that there is no way to deal with it, the case can be, we can dynamically to genetic agent theme role, To proxy all the agents, this is the dynamic agent.

The end of the article to share some of the programmer's jokes quotes: IBM and Boeing 777
The Boeing 777 is the first ever aircraft to be designed and manufactured entirely in computer virtual reality, and the equipment used is entirely supplied by IBM. Before the test flight, Boeing's president was very enthusiastic to invite IBM's technical director to participate in the test flight, but the director said: "Ah, very honored, but the day is my wife's birthday, so ...".
Boeing's total load is angry: "Coward, I haven't told you the date of the test!"

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.