JAVA and mode 10th-proxy Mode

Source: Internet
Author: User

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

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.