0% found this document useful (0 votes)
5 views

8 Design Patterns Java Structural m8 Slides

The Proxy Pattern involves creating an interface that wraps a real object to add functionality such as security and simplicity. It is implemented using Java's reflection capabilities, specifically through classes like java.lang.reflect.Proxy and InvocationHandler. Key distinctions include its single instance limitation compared to the more flexible Decorator Pattern, which allows for dynamic functionality addition.

Uploaded by

podam91581
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

8 Design Patterns Java Structural m8 Slides

The Proxy Pattern involves creating an interface that wraps a real object to add functionality such as security and simplicity. It is implemented using Java's reflection capabilities, specifically through classes like java.lang.reflect.Proxy and InvocationHandler. Key distinctions include its single instance limitation compared to the more flexible Decorator Pattern, which allows for dynamic functionality addition.

Uploaded by

podam91581
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Proxy Pattern

Bryan Hansen
twitter: bh5k | http://www.linkedin.com/in/hansenbryan
Concepts
▪ Interface by wrapping
▪ Can add functionality
▪ Security, Simplicity, Remote, Cost
▪ Proxy called to access real object
▪ Examples:
▪ java.lang.reflect.Proxy
▪ java.rmi.*
Design

Interface based
Interface and Implementation Class
java.lang.reflect.InvocationHandler
java.lang.reflect.Proxy
Client, Interface, InvocationHandler, Proxy,
Implementation
UML
Everyday Example - Proxy
public  Object  invoke(Object  proxy,  Method  m,  Object[]  args)  throws    
   Throwable  {  
           Object  result;  
           try  {  
                   result  =  m.invoke(obj,  args);  
           }  catch  (InvocationTargetException  e)  {  
                 throw  e.getTargetException();    
           }  catch  (Exception  e)  {  
                 throw  new  RuntimeException("unexpected  invocation  exception:  "    
                          +  e.getMessage());  
           }  
           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

You might also like