There is an example of a proxy.
The pupils lent 10 yuan to the middle school students. It's been one months. Middle school students still do not pay the money, so the pupils to achieve the interface of debt collection.
The pupils rewrote the way to money
public void Yaoqian () {
System.out.println ("Give me Money");
Middle school students do not give "do not give you how to", the pupils can not beat AH.
At this time the pupil found me, I am a rich sense of justice college students.
I also realized the interface of debt collection.
I have a member of a pupil variable (is to help him collect debt.) )
I also rewrote the method of getting money.
public void Yaoqian () {
SYSTEM.OUT.PRINTLN ("hit a high school student, you don't have to pay the money");
System.out.println ("Oh, you agree to pay back money");
Pupil. Yaoqian ();
}
So the pupils happily got the money.
In this example.
Elementary school students to money operation is the purpose of operation (real operation), so he inherited the debt collection interface, the realization of the money method, but the implementation of difficulties.
I want to help him to ask for money, so also realized the debt collection interface, Agent role is first to wipe the barrier, and then invoke the purpose of operation (real operation).
There are three main roles for proxy mode:
1 abstract roles (interfaces), containing abstract methods. (Debt collection interface, to Money method.) )
2 proxy role implements XXX, the proxy object role contains a reference to the real object, so that you can manipulate the real object, while the proxy object is the same interface as the real object so that it can replace the real object at any moment. While the agent is able to perform operations on the real object again, additional actions are attached, which is equivalent to encapsulating the real object.
I am the agent role, to help him to ask for money, so also realized the debt collection interface. Also contains a member object for a pupil object. The agent role is to first wipe the barrier and then invoke the purpose of the real role (real action).
3 Real role: The real object represented by the proxy role is the object we will eventually refer to.
There is also an example of a proxy design pattern. The threading operation in Java. The thread class and the Runnable interface.
Thread
Extends Object
Implements Runnable
Create a new thread in Java using a method that implements the Runnable interface:
New Thread (New Runnable () {public void run () {//TODO auto-generated method stubfor (;;) {System.out.println ("runnable");}}). Start ();
An anonymous inner class implements Runnable is used here. It's a real character.
The agent role is thread.
The section of thread is defined as follows:
You can see that it is the use of proxy mode. The benefits of implementing the Runnable interface are many.
New Thread (xxxiimplements Runnable R) A
New Thread (xxxiimplements Runnable R) b
New Thread (xxxiimplements Runnable R) c
A,b,c an instance of shared R.
Not subject to single inheritance restrictions.
The proxy mode of Java design pattern