The proxy mode is the structure mode of the object. The proxy mode provides a proxy object for an object and controls the reference to the original object by the proxy object.
--------------------------------------------------------------------------------
Proxy mode structure
A proxy means that one person or institution takes action on behalf of another person or institution. In some cases, a client does not want or cannot directly reference an object, and the proxy object can play a mediation role between the client and the target object.
The proxy mode class diagram is as follows:
Role in proxy mode:
● Abstract object role: Declares the common interfaces of the target object and the proxy object, so that the proxy object can be used wherever the target object can be used.
● Target object role: defines the target object represented by the proxy object.
● Proxy object role: the proxy object contains references to the target object, so that the target object can be operated at any time. The proxy object provides the same interface as the target object, so that the target object can be replaced at any time. A proxy Object usually performs an operation before or after the client call is passed to the target object, instead of simply passing the call to the target object.
Source code
Abstract object role
[Java]
Package com. bankht. Proxy;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 10:40:16 AM
*
* @ Class description: abstract object role
*/
Public abstract class implements actobject {
// Operation
Public abstract void operation ();
}
Package com. bankht. Proxy;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 10:40:16 AM
*
* @ Class description: abstract object role
*/
Public abstract class implements actobject {
// Operation
Public abstract void operation ();
}
Target object role
[Java]
Package com. bankht. Proxy;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 10:40:46 AM
*
* @ Class description: target object role
*/
Public class RealObject extends actobject {
@ Override
Public void operation (){
// Some operations
System. out. println ("some operations ");
}
}
Package com. bankht. Proxy;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 10:40:46 AM
*
* @ Class description: target object role
*/
Public class RealObject extends actobject {
@ Override
Public void operation (){
// Some operations
System. out. println ("some operations ");
}
}
Proxy object role
[Java]
Package com. bankht. Proxy;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 10:41:06 AM
*
* @ Class description: proxy object role
*/
Public class ProxyObject extends actobject {
RealObject realObject = new RealObject ();
@ Override
Public void operation (){
// You can perform related operations before calling the target object.
System. out. println ("before ");
RealObject. operation ();
// You can perform related operations after calling the target object.
System. out. println ("after ");
}
}
Package com. bankht. Proxy;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 10:41:06 AM
*
* @ Class description: proxy object role
*/
Public class ProxyObject extends actobject {
RealObject realObject = new RealObject ();
@ Override
Public void operation (){
// You can perform related operations before calling the target object.
System. out. println ("before ");
RealObject. operation ();
// You can perform related operations after calling the target object.
System. out. println ("after ");
}
}
Client
[Java]
Package com. bankht. Proxy;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 10:41:32 AM
*
* @ Class description: client: the proxy object delegates the client call to the target object, and can perform specific operations before and after the method of the target object is called.
*/
Public class Client {
Public static void main (String [] args ){
// TODO Auto-generated method stub
AbstractObject obj = new ProxyObject ();
Obj. operation ();
}
}
Package com. bankht. Proxy;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 10:41:32 AM
*
* @ Class description: client: the proxy object delegates the client call to the target object, and can perform specific operations before and after the method of the target object is called.
*/
Public class Client {
Public static void main (String [] args ){
// TODO Auto-generated method stub
AbstractObject obj = new ProxyObject ();
Obj. operation ();
}
}
Run:
[Html]
Before
Some operations
After
Author: m13666425773