Proxy design pattern allows you to create a wrapper class over real object.Wrapper class which is proxy,controls access to real object so in turn you can add extra functionalities to real object without changing real object's code.
As described by GoF:
"Provide a surrogate or placeholder for another object to control access over it."
Real life example may be proxy server used in IT companies for internet access.It blocks access to social networking sites like facebook,twitter and other contents which are not work related.
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#IESZ2CQGT1L5udOK.99
When to use it:
Proxy is required whenever there is need for more sophisticated or versatile reference to an object than a simple pointer.Here are some situations when proxy pattern is applicable.
Lets see one Example :
package com.javasouls.proxypattern;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
interface Calculator{
public int calculate(int operand1, int operand2, Operator operator);
}
enum Operator {
ADD, MULTIPLY, SUBTRACT, DIVIDE, POWER;
Operator (){
}
}
class CalculatorImpl implements Calculator {
public int calculate(int operand1,int operand2, Operator operator){
int result = -1;
switch (operator) {
case ADD:
result = operand1 + operand2;
break;
case SUBTRACT:
result = operand1 - operand2;
break;
case MULTIPLY:
result = operand1 * operand2;
break;
case DIVIDE:
result = operand1 / operand2;
break;
case POWER:
result = operand1 ^ operand2;
break;
default:
break;
}
return result;
}
}
class CalculatorInvocationHandler implements InvocationHandler{
private Object realSubject = null;
public CalculatorInvocationHandler (Object realSubject){
this.realSubject = realSubject;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {
Object result = null;
try{
result = method.invoke(realSubject, args);
}
catch(Exception e){
e.printStackTrace();
}
return result;
}
}
public class ProxyPattern {
public static void main(String[] args) {
Calculator realSubject = new CalculatorImpl();
Calculator proxy = (Calculator)Proxy.newProxyInstance(realSubject.getClass().getClassLoader(), realSubject.getClass().getInterfaces(), new CalculatorInvocationHandler(realSubject));
System.out.println("Calculating via proxy object "+proxy.calculate(2, 3, Operator.ADD));
}
}
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#VpxrVQBCAgI63W4S.99
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#VpxrVQBCAgI63W4S.99
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#VpxrVQBCAgI63W4S.99
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#VpxrVQBCAgI63W4S.99
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#VpxrVQBCAgI63W4S.99
Proxy
design pattern allows you to create a wrapper class over real
object.Wrapper class which is proxy,controls access to real object so in
turn you can add extra functionalities to real object without changing
real object's code.
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#VpxrVQBCAgI63W4S.99
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#VpxrVQBCAgI63W4S.99
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#VpxrVQBCAgI63W4S.99
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#VpxrVQBCAgI63W4S.99
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#VpxrVQBCAgI63W4S.99
As described by GoF:
"Provide a surrogate or placeholder for another object to control access over it."
Real life example may be proxy server used in IT companies for internet access.It blocks access to social networking sites like facebook,twitter and other contents which are not work related.
When to use it:
Proxy is required whenever there is need for more sophisticated or
versatile reference to an object than a simple pointer.Here are some
situations when proxy pattern is applicable.
- A remote proxy provides a local representative for an object in a different address space.Providing interface for remote resources such as web service or REST resources.
- A virtual proxy creates expensive object on demand.
- A protection proxy controls access to the original object.Protection proxies are useful when objects should have different access rights.
- A smart reference is a replacement for a bare pointer that performs additional actions when an object is accessed.
- Adding a thread-safe feature to an existing class without changing the existing class's code.
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#IESZ2CQGT1L5udOK.99
Proxy is required whenever there is need for more sophisticated or versatile reference to an object than a simple pointer.Here are some situations when proxy pattern is applicable.
- A remote proxy provides a local representative for an object in a different address space.Providing interface for remote resources such as web service or REST resources.
- A virtual proxy creates expensive object on demand.
- A protection proxy controls access to the original object.Protection proxies are useful when objects should have different access rights.
- A smart reference is a replacement for a bare pointer that performs additional actions when an object is accessed.
- Adding a thread-safe feature to an existing class without changing the existing class's code.
Lets see one Example :
package com.javasouls.proxypattern;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
interface Calculator{
public int calculate(int operand1, int operand2, Operator operator);
}
enum Operator {
ADD, MULTIPLY, SUBTRACT, DIVIDE, POWER;
Operator (){
}
}
class CalculatorImpl implements Calculator {
public int calculate(int operand1,int operand2, Operator operator){
int result = -1;
switch (operator) {
case ADD:
result = operand1 + operand2;
break;
case SUBTRACT:
result = operand1 - operand2;
break;
case MULTIPLY:
result = operand1 * operand2;
break;
case DIVIDE:
result = operand1 / operand2;
break;
case POWER:
result = operand1 ^ operand2;
break;
default:
break;
}
return result;
}
}
class CalculatorInvocationHandler implements InvocationHandler{
private Object realSubject = null;
public CalculatorInvocationHandler (Object realSubject){
this.realSubject = realSubject;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {
Object result = null;
try{
result = method.invoke(realSubject, args);
}
catch(Exception e){
e.printStackTrace();
}
return result;
}
}
public class ProxyPattern {
public static void main(String[] args) {
Calculator realSubject = new CalculatorImpl();
Calculator proxy = (Calculator)Proxy.newProxyInstance(realSubject.getClass().getClassLoader(), realSubject.getClass().getInterfaces(), new CalculatorInvocationHandler(realSubject));
System.out.println("Calculating via proxy object "+proxy.calculate(2, 3, Operator.ADD));
}
}
Real
life example may be proxy server used in IT companies for internet
access.It blocks access to social networking sites like facebook,twitter
and other contents which are not work related.
Proxy
design pattern allows you to create a wrapper class over real
object.Wrapper class which is proxy,controls access to real object so in
turn you can add extra functionalities to real object without changing
real object's code.
As described by GoF:
"Provide a surrogate or placeholder for another object to control access over it."
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#VpxrVQBCAgI63W4S.99
Proxy
design pattern allows you to create a wrapper class over real
object.Wrapper class which is proxy,controls access to real object so in
turn you can add extra functionalities to real object without changing
real object's code.
As described by GoF:
"Provide a surrogate or placeholder for another object to control access over it."
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#VpxrVQBCAgI63W4S.99
Proxy
design pattern allows you to create a wrapper class over real
object.Wrapper class which is proxy,controls access to real object so in
turn you can add extra functionalities to real object without changing
real object's code.
As described by GoF:
"Provide a surrogate or placeholder for another object to control access over it."
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#VpxrVQBCAgI63W4S.99
Proxy
design pattern allows you to create a wrapper class over real
object.Wrapper class which is proxy,controls access to real object so in
turn you can add extra functionalities to real object without changing
real object's code.
As described by GoF:
"Provide a surrogate or placeholder for another object to control access over it."
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#VpxrVQBCAgI63W4S.99
Proxy
design pattern allows you to create a wrapper class over real
object.Wrapper class which is proxy,controls access to real object so in
turn you can add extra functionalities to real object without changing
real object's code.
As described by GoF:
"Provide a surrogate or placeholder for another object to control access over it."
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#VpxrVQBCAgI63W4S.99
As described by GoF:
"Provide a surrogate or placeholder for another object to control access over it."
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#VpxrVQBCAgI63W4S.99
Proxy
design pattern allows you to create a wrapper class over real
object.Wrapper class which is proxy,controls access to real object so in
turn you can add extra functionalities to real object without changing
real object's code.
As described by GoF:
"Provide a surrogate or placeholder for another object to control access over it."
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#VpxrVQBCAgI63W4S.99
Proxy
design pattern allows you to create a wrapper class over real
object.Wrapper class which is proxy,controls access to real object so in
turn you can add extra functionalities to real object without changing
real object's code.
As described by GoF:
"Provide a surrogate or placeholder for another object to control access over it."
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#VpxrVQBCAgI63W4S.99
Proxy
design pattern allows you to create a wrapper class over real
object.Wrapper class which is proxy,controls access to real object so in
turn you can add extra functionalities to real object without changing
real object's code.
As described by GoF:
"Provide a surrogate or placeholder for another object to control access over it."
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#VpxrVQBCAgI63W4S.99
Proxy
design pattern allows you to create a wrapper class over real
object.Wrapper class which is proxy,controls access to real object so in
turn you can add extra functionalities to real object without changing
real object's code.
As described by GoF:
"Provide a surrogate or placeholder for another object to control access over it."
Read more at http://javapostsforlearning.blogspot.com/2012/09/proxy-design-pattern-in-java.html#VpxrVQBCAgI63W4S.99
No comments:
Post a Comment