C # implementation prototype

Source: Internet
Author: User
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. LINQ;
  4. Using system. text;
  5. Namespace Design Mode
  6. {
  7. Class Program
  8. {
  9. Static void main (string [] ARGs)
  10. {
  11. // First, the prototype mode is a creation mode used to handle the dependency caused by new.
  12. // There are two essentials for the prototype mode: 1. Solve the dependency, that is to say, the Customer Code must depend on abstraction rather than implementation.
  13. // 2 clone the instance that implements the class through the prototype of the connected image.
  14. // The following is an example: for example, we can simulate and process different enemies in a Soldado game.
  15. // Enemies fall into the sky, go to the ground, and swim in the water .. When the sky flies, a bomb can be thrown, and a gun can be taken on the ground. when traveling in the water, a knife can be used to hurt people.
  16. // Below we start to define three enemy abstract classes. Note: Because the clone method is implemented, it cannot be an interface.
  17. // Next, we will create specific implementation classes for these three types of people, such as the enemies of Water games and the enemies of armor.
  18. // This encapsulates the change point. That is to say, the enemy of Water games will always have a knife, but some will have armor and some will not.
  19. // That is to say, the knife remains unchanged, and the armor can be changed. For example, the armor is used to describe changes in the other two types. In fact, if it is a real project, more images should be drawn from the change points.
  20. // Then we compile the game runtime. Here, we do not rely on implementation classes, but can create a batch of enemies.
  21. // Let's start running the game ....
  22. Wateractor W = new armorwateractor ();
  23. Flyactor F = new normalflyactpolictor ();
  24. Roadactor r = new armorroadactor ();
  25. // We can see that we instantiate people in the water and people on land as soldiers with armor
  26. Gamesystem GM = new gamesystem (W, r, f );
  27. GM. rungame ();
  28. // Assume that we want to replace people in water and land with people without armor. We only need to give them different instances in some places and do not need to change the dependency GM.
  29. Wateractor W1 = new normalwateractor ();
  30. Flyactor F1 = new armorflyactpolictor ();
  31. Roadactor R1 = new normalroadactor ();
  32. // We can see that we instantiate people in the water and people on land as soldiers with armor
  33. Gamesystem GM1 = new gamesystem (W1, R1, F1 );
  34. Gm1.rungame ();
  35. Console. Readline ();
  36. }
  37. }
  38. // Enemies of Water games
  39. Abstract class wateractor
  40. {
  41. // Return your instance. Use the system-provided shortest copy. Note: if there is a reference type, we recommend that you use serialization and deserialization for deep copy.
  42. Public wateractor clone ()
  43. {
  44. Return (wateractor) This. memberwiseclone ();
  45. }
  46. // Knife attack method
  47. Public abstract void knifeattack ();
  48. }
  49. // Enemies flying in the sky
  50. Abstract class flyactor
  51. {
  52. Public flyactor clone ()
  53. {
  54. Return (flyactor) This. memberwiseclone ();
  55. }
  56. // Bomb throwing
  57. Public abstract void bombattack ();
  58. }
  59. // Enemies walking on the ground
  60. Abstract class roadactor
  61. {
  62. Public roadactor clone ()
  63. {
  64. Return (roadactor) This. memberwiseclone ();
  65. }
  66. // Machine gun Scanning
  67. Public abstract void gunattack ();
  68. }
  69. Class armorwateractor: wateractor
  70. {
  71. Public override void knifeattack ()
  72. {
  73. Console. writeline ("% attacked with a knife in armor water ");
  74. }
  75. }
  76. Class normalwateractor: wateractor
  77. {
  78. Public override void knifeattack ()
  79. {
  80. Console. writeline ("% no enemy in armor water attacked with a knife ");
  81. }
  82. }
  83. Class armorflyactoractor: flyactor
  84. {
  85. Public override void bombattack ()
  86. {
  87. Console. writeline ("### air enemies with armor ");
  88. }
  89. }
  90. Class normalflyactoractor: flyactor
  91. {
  92. Public override void bombattack ()
  93. {
  94. Console. writeline ("############## no armor, air enemies, throwing bombs ");
  95. }
  96. }
  97. Class armorroadactor: roadactor
  98. {
  99. Public override void gunattack ()
  100. {
  101. Console. writeline ("@ target land enemies with armor scan with machine guns ");
  102. }
  103. }
  104. Class normalroadactor: roadactor
  105. {
  106. Public override void gunattack ()
  107. {
  108. Console. writeline ("@ no armor on land enemies use machine guns to scan ");
  109. }
  110. }
  111. Class gamesystem
  112. {
  113. Private wateractor wactor;
  114. Private roadactor ractor;
  115. Private flyactor factor;
  116. Public gamesystem (wateractor wactor, roadactor ractor, flyactor factor)
  117. {
  118. This. wactor = wactor;
  119. This. ractor = ractor;
  120. This. Factor = factor;
  121. }
  122. Public void rungame ()
  123. {
  124. Wateractor W1 = This. wactor. Clone ();
  125. Wateractor W2 = This. wactor. Clone ();
  126. Wateractor W3 = This. wactor. Clone ();
  127. Wateractor W4 = This. wactor. Clone ();
  128. Wateractor W5 = This. wactor. Clone ();
  129. // Here, we don't care at all. The subclass has armor or armor .. Whether you have armor or not, attack ..
  130. W1.knifeattack ();
  131. W2.knifeattack ();
  132. W3.knifeattack ();
  133. W4.knifeattack ();
  134. W5.knifeattack ();
  135. Flyactor F1 = This. factor. Clone ();
  136. Flyactor F2 = This. factor. Clone ();
  137. Flyactor F3 = This. factor. Clone ();
  138. Flyactor F4 = This. factor. Clone ();
  139. F1.bombattack ();
  140. F2.bombattack ();
  141. F3.bombattack ();
  142. F4.bombattack ();
  143. Roadactor R1 = This. ractor. Clone ();
  144. Roadactor r2 = This. ractor. Clone ();
  145. Roadactor R3 = This. ractor. Clone ();
  146. Roadactor r4 = This. ractor. Clone ();
  147. Roadactor R5 = This. ractor. Clone ();
  148. R1.gunattack ();
  149. R2.gunattack ();
  150. R3.gunattack ();
  151. R4.gunattack ();
  152. R5.gunattack ();
  153. }
  154. }
  155. }

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.