Proxy
Here is a common interface that the proxy and service
must share. (They could have shared a superclass instead).
-----------------------------------------------------
public interface IService{
public String doIt(int value);
}
-----------------------------------------------------
Here is the service providing class
-----------------------------------------------------
class Service implements IService{
public String doIt(int temp){
return "I preformed some service for you";
}
}
-----------------------------------------------------
Here is the proxy class.
The proxy here is providing a security layer to calls
made to the service. (It does not like the int 2)
Proxy classes are commonly used for some type of
service managment.
-----------------------------------------------------
class ServiceProxy implements IService{
private Service service = new Service();
public String doIt(int temp){
if(temp==2)
return "I refuse";
else
return service.doIt(temp);
}
}
-----------------------------------------------------
Here is the test class that simply demonstrates the
function of the proxy class.
-----------------------------------------------------
class Test{
public static void main(String[] args){
System.out.println("class: Test, method: main\n");
IService serviceProxy = new ServiceProxy();
IService service = new Service();
System.out.println("calling service and proxy with arg of 1");
System.out.println("service: " + service.doIt(1));
System.out.println("proxy: " + serviceProxy.doIt(1));
System.out.println("");
System.out.println("calling service and proxy with arg of 2");
System.out.println("service: " + service.doIt(2));
System.out.println("proxy: " + serviceProxy.doIt(2));
}
}
-----------------------------------------------------
Here is a tar with above classes in it.
proxy.tar