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!