Proxy pattern (C ++ source code)

Source: Internet
Author: User
Proxy mode removes the real operations of the original class from the caller's perspective, and may also add operations that the caller is not aware, however, from the perspective of the class designer, this does not affect the caller's use of the original class function. To protect a class, the caller generates a new class and does not dare to say that the idea is very correct, at least I feel a little uncomfortable when I turn a bend to achieve my goal. Maybe the real power of the proxy mode is the dynamic proxy method derived from this.
  1. // Proxyparenter. cpp: defines the entry point for the console application.
  2. //
  3. // The proxy mode is the object structure mode, and the operation target is a class object.
  4. // The following is a classic and simplified example:
  5. # Include "stdafx. H"
  6. # Include <iostream>
  7. Using namespace STD;
  8. // Abstract role
  9. Class isubject
  10. {
  11. Public:
  12. Isubject ()
  13. {
  14. }
  15. Virtual ~ Isubject ()
  16. {
  17. }
  18. Public:
  19. Virtual void request () = 0;
  20. };
  21. // Real role: implements the subject request () method.
  22. Class csubject: Public isubject
  23. {
  24. Public:
  25. Csubject ()
  26. {
  27. }
  28. Virtual ~ Csubject ()
  29. {
  30. }
  31. Public:
  32. Virtual void request ()
  33. {
  34. Cout <"csubject: Request ()" <Endl;
  35. }
  36. };
  37. // Proxy role
  38. Class cproxysubject: Public isubject
  39. {
  40. Public:
  41. Cproxysubject (int nid)
  42. {
  43. This-> m_nid = NID;
  44. }
  45. Virtual ~ Cproxysubject ()
  46. {
  47. }
  48. Public:
  49. Virtual void request ()
  50. {
  51. If (! This-> prerequest (this-> m_nid ))
  52. {
  53. Cout <"request reject! "<Endl;
  54. Return;
  55. }
  56. This-> m_objsub.request ();
  57. This-> afterrequest ();
  58. }
  59. Void setid (int nid)
  60. {
  61. This-> m_nid = NID;
  62. }
  63. Int GETID ()
  64. {
  65. Return this-> m_nid;
  66. }
  67. PRIVATE:
  68. Bool prerequest (int nid)
  69. {
  70. If (NID> 0) & (NID <= 100 ))
  71. {
  72. Return true;
  73. }
  74. Return false;
  75. }
  76. Void afterrequest ()
  77. {
  78. Cout <"after do request! "<Endl;
  79. }
  80. PRIVATE:
  81. Csubject m_objsub;
  82. Int m_nid;
  83. };
  84. Int main ()
  85. {
  86. Isubject * pobj = NULL;
  87. Int nid = 0;
  88. Cout <"Please input your ID:" <Endl;
  89. Cin> NID;
  90. Pobj = new cproxysubject (NID );
  91. If (null! = Pobj)
  92. {
  93. Pobj-> request ();
  94. }
  95. // Clear
  96. If (null! = Pobj)
  97. {
  98. Delete pobj;
  99. Pobj = NULL;
  100. }
  101. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.