C++ Proxy
Here is a common interface that the proxy and service
must share. (They could have shared a superclass instead).
-----------------------------------------------------
class IService{
public:
virtual std::string doIt(int value) = 0;
}
-----------------------------------------------------
Here is the service providing class
-----------------------------------------------------
class Service : public IService{
public:
std::string doIt(int value);
};
std::string Service::doIt(int value){
return std::string("I performed some service for you\n");
}
-----------------------------------------------------
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 : public IService{
public:
std::string doIt(int value);
private:
Service service;
};
std::string ServiceProxy::doIt(int value){
if(value ==2)
return std::string("I refuse\n");
else
return service.doIt(value);
}
-----------------------------------------------------
Here is the test class that simply demonstrates the
function of the proxy class.
-----------------------------------------------------
int main(){
std::cout<<"main: START\n";
IService* service = new Service();
IService* serviceProxy = new ServiceProxy();
std::cout<<"calling service and proxy with an arg of 1\n";
std::cout<<"service: " << service->doIt(1);
std::cout<<"proxy: " << serviceProxy->doIt(1)<<std::endl;
std::cout<<"calling service and proxy with an arg of 2\n";
std::cout<<"service: " << service->doIt(2);
std::cout<<"proxy: " << serviceProxy->doIt(2);
std::cout<<"main: END\n";
return 0;
}
-----------------------------------------------------
Here is a tar with above classes in it.
C++_proxy.tar