Use
Agent mode (proxy)
Provides a proxy for other objects to control access to this object.
structure
diagram-proxy mode structure diagram
Subject : Defines the public interface of Realsubject and proxy, so that proxies can be used wherever realsubject is used.
AbstractclassSubject {
Public AbstractvoidRequest ();
}
Realsubject : Defines the real entity represented by the Proxy.
classRealsubjectextendsSubject {
@Override
PublicvoidRequest () {
SYSTEM.OUT.PRINTLN ("real request");
}
}
Proxy: Saving a reference allows the agent to access the entity and provides an interface that is the same as the Subject interface, so that the proxy can be used instead of the entity.
classProxyextendsSubject {
PrivateRealsubject Real;
@Override
PublicvoidRequest () {
if(NULL= = Real) {
Real =NewRealsubject ();
}
Real. Request ();
}
}
Application Scenarios
1. Remote Agent
Provides a localized representation for a remote object in a different address space.
2. Virtual Agent
Create expensive objects as needed. It is the real object that takes a long time to store the instantiation.
3. Security Agent
Used to control the access rights of real objects.
4. Smart Guide
is when a real object is called, the agent handles something else.
Points
The proxy mode describes an indirect level of access to an object.
A remote agent can hide the details of an object in different address spaces.
A virtual agent can optimize the cost of creating objects as needed.
Both security agents and smart guides allow access to objects while processing other transactions.
JAVA design mode proxy mode