Proxy.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <stdio.h>
  2. /*
  3. * C++ Design Patterns: PROXY
  4. * http://www.plantuml.com/plantuml/uml/RO_FIiD06CJl-nGVUAWYCNWj8HJ5gvRMWvvainbnPV-attuh1lM1z3NwOgPf2otqEoqpls5cPqaqI_PEbHEQBnwUdcbIgk6CqodHhRC8efGD0dxL1hJCpJkCq9UYuSw8iSf8SdwXVlQX1RGxnYvFONEEd_qEAAXZ-w45BsaRF1Lxioq8lfEUF7m3JahjUwhleZWiFZR6AdMugzg9D2L6n-lVX21QR0pKGcs_JKLHxw8gH07fHFVNuxAggIiJNUGfnrwx6vTHZsHLZd9TbKsz9n_x3jEDG4lXR2IpTgDt5upkKdQW72WEmuGAGY0VMv2tpD4oGKQMaJeO0GsqN_zva8b1M0kjCiZ0PLiOUwKGMjgCLRFXbRtx1m00
  5. *
  6. */
  7. #include <iostream>
  8. // A subject exposes a contract
  9. class Subject {
  10. public:
  11. virtual ~Subject() {}
  12. virtual void request() = 0;
  13. };
  14. // Implementation of the contract
  15. class RealSubject : public Subject {
  16. public:
  17. void request() {
  18. std::cout << "RealSubject Request" << std::endl;
  19. }
  20. };
  21. // A proxy encapsulates an already known class - with the same contract as Subject
  22. class Proxy : public Subject
  23. {
  24. public:
  25. Proxy()
  26. {
  27. private_subject = new RealSubject();
  28. }
  29. ~Proxy()
  30. {
  31. delete private_subject;
  32. }
  33. void request()
  34. {
  35. std::cout << "Do Proxy Request" << std::endl;
  36. private_subject->request();
  37. }
  38. private:
  39. RealSubject *private_subject;
  40. };
  41. int main()
  42. {
  43. Proxy *proxy = new Proxy();
  44. proxy->request();
  45. delete proxy;
  46. }