Introduction to Proxy mode and proxy Mode

Source: Internet
Author: User

Introduction to Proxy mode and proxy Mode
1. Introduction to Proxy Mode

The proxy mode is structured.

The proxy mode provides a proxy object for an object and controls the reference to the original object by the proxy object.

The proxy object must inherit from the abstract topic and control the reference of the original object.

Ii. Simple Example

Abstract topic category

Package proxy;/** abstract Topic */public abstract class Subject {/** Request Method */abstract public void Request ();}

Real theme class, inheriting Abstraction

Package proxy;/** real Theme */public class RealSubject extends Subject {@ Override public void Request () {System. out. println ("real theme implementation Request! ");}}

Proxy topic

Package proxy;/** proxy Topic */public class ProxySubject extends Subject {private RealSubject realSubject; @ Override public void Request () {// preRequest (); // determines whether the request is empty if (realSubject = null) {// instantiate realSubject = new RealSubject ();} // call the response method realSubject of the real topic. request (); // postRequest ();} private void preRequest () {System. out. println ("pre-request proxy topic operations! ");} Private void postRequest () {System. out. println (" the agent subject's own operations after the request! ");}}

Test class

Package proxy;/** proxy topic Test class */public class Test {public static void main (String [] args) {// declare an abstract topic, it is instantiated as the proxy topic Subject subject = new ProxySubject (); // call the response method subject of the proxy topic. request ();}}

Result:

The proxy topic's own operations before the request! Real theme implementation request! The proxy topic's own operations after the request!

 

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.