code_junkie

Do polymorphism or conditionals promote better design?

Nov 11th, 2011
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. class Animal
  2. {
  3. public:
  4. Noise warningNoise();
  5. Noise pleasureNoise();
  6. private:
  7. AnimalType type;
  8. };
  9.  
  10. Noise Animal::warningNoise()
  11. {
  12. switch(type)
  13. {
  14. case Cat: return Hiss;
  15. case Dog: return Bark;
  16. }
  17. }
  18. Noise Animal::pleasureNoise()
  19. {
  20. switch(type)
  21. {
  22. case Cat: return Purr;
  23. case Dog: return Bark;
  24. }
  25. }
  26.  
  27. class Animal
  28. {
  29. public:
  30. virtual Noise warningNoise() = 0;
  31. virtual Noise pleasureNoise() = 0;
  32. };
  33.  
  34. class Cat: public Animal
  35. {
  36. // Compiler forces you to define both method.
  37. // Otherwise you can't have a Cat object
  38.  
  39. // All code local to the cat belongs to the cat.
  40.  
  41. };
  42.  
  43. void MyProcedure(int p_iCommand, void *p_vParam)
  44. {
  45. // A LOT OF CODE ???
  46. // each case has a lot of code, with both similarities
  47. // and differences, and of course, casting p_vParam
  48. // into something, depending on hoping no one
  49. // did a mistake, associating the wrong command with
  50. // the wrong data type in p_vParam
  51.  
  52. switch(p_iCommand)
  53. {
  54. case COMMAND_AAA: { /* A LOT OF CODE (see above) */ } break ;
  55. case COMMAND_BBB: { /* A LOT OF CODE (see above) */ } break ;
  56. // etc.
  57. case COMMAND_XXX: { /* A LOT OF CODE (see above) */ } break ;
  58. case COMMAND_ZZZ: { /* A LOT OF CODE (see above) */ } break ;
  59. default: { /* call default procedure */} break ;
  60. }
  61. }
  62.  
  63. void MyProcedure(int p_iCommand, void *p_vParam)
  64. {
  65. switch(p_iCommand)
  66. {
  67. // Only one case. Isn't it cool?
  68. case COMMAND:
  69. {
  70. Command * c = static_cast<Command *>(p_vParam) ;
  71. c->process() ;
  72. }
  73. break ;
  74. default: { /* call default procedure */} break ;
  75. }
  76. }
  77.  
  78. [+] Command
  79. |
  80. +--[+] CommandServer
  81. | |
  82. | +--[+] CommandServerInitialize
  83. | |
  84. | +--[+] CommandServerInsert
  85. | |
  86. | +--[+] CommandServerUpdate
  87. | |
  88. | +--[+] CommandServerDelete
  89. |
  90. +--[+] CommandAction
  91. | |
  92. | +--[+] CommandActionStart
  93. | |
  94. | +--[+] CommandActionPause
  95. | |
  96. | +--[+] CommandActionEnd
  97. |
  98. +--[+] CommandMessage
  99.  
  100. class CommandAction : public Command
  101. {
  102. // etc.
  103. virtual void process() // overriding Command::process pure virtual method
  104. {
  105. this->processBefore() ;
  106. this->processWhile() ;
  107. this->processAfter() ;
  108. }
  109.  
  110. virtual void processBefore() = 0 ; // To be overriden
  111.  
  112. virtual void processWhile()
  113. {
  114. // Do something common for all CommandAction objects
  115. }
  116.  
  117. virtual void processAfter() = 0 ; // To be overriden
  118.  
  119. } ;
  120.  
  121. class CommandActionStart : public CommandAction
  122. {
  123. // etc.
  124. virtual void processBefore()
  125. {
  126. // Do something common for all CommandActionStart objects
  127. }
  128.  
  129. virtual void processAfter()
  130. {
  131. // Do something common for all CommandActionStart objects
  132. }
  133. } ;
  134.  
  135. If (weapon is a rifle) then //Code to attack with rifle else
  136. If (weapon is a plasma gun) //Then code to attack with plasma gun
  137.  
  138. weapon.attack()
  139.  
  140. switch(obj.type): {
  141. case 1: cout << "Type 1" << obj.foo <<...; break;
  142. case 2: cout << "Type 2" << ...
  143.  
  144. cout << object.toString();
  145.  
  146. interface A{
  147.  
  148. int foo();
  149.  
  150. }
  151.  
  152. final class B implements A{
  153.  
  154. int foo(){ print("B"); }
  155.  
  156. }
  157.  
  158. final class C implements A{
  159.  
  160. int foo(){ print("C"); }
  161.  
  162. }
  163.  
  164. class A{
  165.  
  166. int foo(){print("A");}
  167.  
  168. }
  169.  
  170. class B extends A{
  171.  
  172. int foo(){print("B");}
  173.  
  174. }
  175.  
  176. class C extends B{
  177.  
  178. int foo(){print("C");}
  179.  
  180. }
  181.  
  182. ...
  183.  
  184. class Z extends Y{
  185.  
  186. int foo(){print("Z");
  187.  
  188. }
  189.  
  190. main(){
  191.  
  192. F* f = new Z();
  193. A* a = f;
  194. a->foo();
  195. f->foo();
  196.  
  197. }
  198.  
  199. A* a = new Z;
  200. A a2 = *a;
  201. a->foo();
  202. a2.foo();
Add Comment
Please, Sign In to add comment