Examples of proxy mode programs for Java design patterns

Source: Internet
Author: User

Proxy mode:

The so-called agent is one person or one body acting on behalf of another person or another body. In some cases, a client does not want or cannot refer directly to an object, whereas a proxy object can act as an intermediary between the client and the target object.

Proxy mode is a structural design pattern. You can use proxy mode when the client does not want to invoke the subject object directly, and you want the subject object's behavior to be pre-and post-processing or later processed.

The three roles of the proxy mode are: Topic abstract class, actual topic class, proxy class.

Topic abstract class:

Package Com.yihai.spring.proxy;public Abstract class Subject {//Topic abstract classes Define the behavior of the subject object, and the following example defines the request behavior of the Subject object in the topic abstract class. public abstract void request ();

Actual Subject class:

Package com.yihai.spring.proxy;//The actual topic class inherits the abstract topic class subject, which implements the behavior request in the abstract topic class. The actual subject class is the target object in AOP. public class Realsubject extends Subject {@Overridepublic void request () {System.out.println ("realsubject--Actual subject Class");}}
Proxy class:

The Package com.yihai.spring.proxy;/** * Proxy class inherits the abstract theme class, which is associated with the actual subject class. The * Prerequest and Postrequest methods are defined in the proxy class, and the request * method in the actual subject class is controlled. The proxy class corresponds to the Proxyfactorybean * class in Spring AOP, which is used to generate proxy objects. */public class Proxysubject extends Subject {//proxy class association actual subject Class private realsubject realsubject; @Overridepublic void Request () {prerequest ();//A custom method that implements some logical if (realsubject==null) {realsubject = new Realsubject () that you want to complete before the request);} Realsubject.request ();p ostrequest ();//Custom methods that implement some logic}//custom methods that you want to complete after a request: the logical private void prerequest () that was completed before the agent { SYSTEM.OUT.PRINTLN ("Business logic output------------completed before--------------Agent");} Custom method: The agent finishes after the logical private void Postrequest () {System.out.println ("--------------the agent completes the business logic output-------------");} Main method public static void main (string[] args) {Proxysubject proxysubject = new Proxysubject ();p roxysubject.request ();}}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Examples of proxy mode programs for Java design patterns

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.