Structure Mode in Java: proxy (proxy Mode)

Source: Internet
Author: User

Proxy mode provides a proxy object for an object and controls the reference to the source object by the proxy object. An agent is an action taken by one person or institution on behalf of another person or institution. In some cases, the customer does not want or cannot directly reference an object. The proxy object can serve as an intermediary between the customer and the target object. The client cannot identify the proxy topic object and the real topic object. The proxy mode does not know the real proxy object, but only holds an interface of the proxy object. At this time, the proxy object cannot be created as the proxy object, the proxy object must have other roles created and passed in on behalf of the system.

Example:

1 interface demo {
2 Public void action ();
3}
4
5 class realdemo implements demo {
6 @ override
7 public void action (){
8 system. Out. println ("this is a real implementation class ");
9}
10}
11
12 class proxydemo implements demo {
13 private demo real;
14 public proxydemo (){
15 system. Out. println ("this is the proxy class ");
16 real = new realdemo ();
17}
18 @ override
19 public void action (){
20 system. Out. println ("Agent started ");
21 Real. Action ();
22 system. Out. println ("Agent ended ");
23}
24}
25
26 public class test {
27 public static void main (string [] ARGs ){
28 demo = new proxydemo ();
29 demo. Action ();
30}
31}

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.