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