Apply stragegy pattern to "duck class" (C # source code)

Source: Internet
Author: User
  1. Using system;
  2. Using system. text;
  3. Namespace nmduck
  4. {
  5. Public class crunmain
  6. {
  7. Public static int main ()
  8. {
  9. // Normal duck
  10. Cduck objduck = new cmallardduck ();
  11. Objduck. Display ();
  12. Objduck. parse mfly ();
  13. Objduck. Fetch mquack ();
  14. // Set for model duck;
  15. Objduck = new cmodelduck ();
  16. Objduck. parse mfly ();
  17. Objduck. setflybehavior (New cflyrocketpowered ());
  18. Objduck. parse mfly ();
  19. Return 0;
  20. }
  21. }
  22. Public abstract class cduck
  23. {
  24. Public abstract void display ();
  25. Public void initialize mfly ()
  26. {
  27. This. m_objfly.fly ();
  28. }
  29. Public void initialize mquack ()
  30. {
  31. This. m_objquack.quack ();
  32. }
  33. Public void swim ()
  34. {
  35. Console. writeline ("all ducks float, even decoys! ");
  36. }
  37. Public void setflybehavior (iflybehavior objfly)
  38. {
  39. This. m_objfly = objfly;
  40. }
  41. Public void setquackbehavior (iquackbehavior objquack)
  42. {
  43. This. m_objquack = objquack;
  44. }
  45. Protected iquackbehavior m_objquack;
  46. Protected iflybehavior m_objfly;
  47. };
  48. Public class cmallardduck: cduck
  49. {
  50. Public cmallardduck ()
  51. {
  52. This. m_objfly = new cflywithwings ();
  53. This. m_objquack = new cquack ();
  54. }
  55. Public override void display ()
  56. {
  57. Console. writeline ("I am cmallardduc ");
  58. }
  59. }
  60. Public class cmodelduck: cduck
  61. {
  62. Public cmodelduck ()
  63. {
  64. This. m_objfly = new cflynoway ();
  65. This. m_objquack = new cquack ();
  66. }
  67. Public override void display ()
  68. {
  69. Console. writeline ("I am cmodelduck ");
  70. }
  71. }
  72. Public interface iquackbehavior
  73. {
  74. Void quack ();
  75. }
  76. Public class cquack: iquackbehavior
  77. {
  78. // Iquackbehavior members
  79. Public void quack ()
  80. {
  81. Console. writeline ("quack! ");
  82. }
  83. }
  84. Public class cmutequack: iquackbehavior
  85. {
  86. // Iquackbehavior members
  87. Public void quack ()
  88. {
  89. Console. writeline ("Silence! ");
  90. }
  91. }
  92. Public class csqueak: iquackbehavior
  93. {
  94. // Iquackbehavior members
  95. Public void quack ()
  96. {
  97. Console. writeline ("squeak! ");
  98. }
  99. }
  100. Public interface iflybehavior
  101. {
  102. Void fly ();
  103. }
  104. Public class cflywithwings: iflybehavior
  105. {
  106. // Iflybehavior
  107. Public void fly ()
  108. {
  109. Console. writeline ("I am flying! ");
  110. }
  111. }
  112. Public class cflynoway: iflybehavior
  113. {
  114. // Iflybehavior
  115. Public void fly ()
  116. {
  117. Console. writeline ("I can't fly! ");
  118. }
  119. }
  120. Public class cflyrocketpowered: iflybehavior
  121. {
  122. // Iflybehavior members
  123. Public void fly ()
  124. {
  125. Console. writeline ("I am flying with a rocket! ");
  126. }
  127. }
  128. }

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.