Java设计模式之代理模式
PROXY (Object Structural)
Purpose
Allows for object level access control by acting as a pass through
entity or a placeholder object.
Use When
1 The object being represented is external to the system.
2 Objects need to be created on demand.
3 Access control for the original object is required.
4 Added functionality is required when an object is accessed.
Example
Ledger applications often provide a way for users to reconcile
their bank statements with their ledger data on demand, automating
much of the process. The actual operation of communicating
with a third party is a relatively expensive operation that should be
limited. By using a proxy to represent the communications object
we can limit the number of times or the intervals the communication
is invoked. In addition, we can wrap the complex instantiation
of the communication object inside the proxy class, decoupling
calling code from the implementation details.
package javaPattern.proxy;public interface Subject {public void request();}class RealSubject implements Subject{@Overridepublic void request() {System.out.println("实际类的方法");}}class Proxy implements Subject{private Subject real;public Proxy(Subject real){this.real = real;}@Overridepublic void request() {System.out.println("代理以下方法");real.request();}}class Client{public static void main(String[] args) {Proxy proxy = new Proxy(new RealSubject());proxy.request();}}