Proxy Pattern
Bryan Hansen
twitter: bh5k | [Link]
Concepts
▪ Interface by wrapping
▪ Can add functionality
▪ Security, Simplicity, Remote, Cost
▪ Proxy called to access real object
▪ Examples:
▪ [Link]
▪ [Link].*
Design
Interface based
Interface and Implementation Class
[Link]
[Link]
Client, Interface, InvocationHandler, Proxy,
Implementation
UML
Everyday Example - Proxy
public Object invoke(Object proxy, Method m, Object[] args) throws
Throwable {
Object result;
try {
result = [Link](obj, args);
} catch (InvocationTargetException e) {
throw [Link]();
} catch (Exception e) {
throw new RuntimeException("unexpected invocation exception: "
+ [Link]());
}
return result;
}
Exercise Proxy
Twitter API Download
Twitter Service Impl
Restrict POST call
Pitfalls
▪ Only one proxy
▪ Another Abstraction
▪ Similar to other patterns
Contrast
Proxy Decorator
▪ Can add functionality, but not its ▪ Dynamically add functionality
main purpose ▪ Chained
▪ Can only have one ▪ Decorator points to its own type
▪ Compile time ▪ Runtime
Proxy Summary
• Great utilities built into Java API
• Only one instance
• Used by DIJ/IOC Frameworks