code_junkie

Ways to eliminate switch in code

Nov 11th, 2011
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. foreach (var animal in zoo) {
  2. switch (typeof(animal)) {
  3. case "dog":
  4. echo animal.bark();
  5. break;
  6.  
  7. case "cat":
  8. echo animal.meow();
  9. break;
  10. }
  11. }
  12.  
  13. foreach (var animal in zoo) {
  14. echo animal.speak();
  15. }
  16.  
  17. class RequestHandler {
  18.  
  19. public void handleRequest(int action) {
  20. switch(action) {
  21. case LOGIN:
  22. doLogin();
  23. break;
  24. case LOGOUT:
  25. doLogout();
  26. break;
  27. case QUERY:
  28. doQuery();
  29. break;
  30. }
  31. }
  32. }
  33.  
  34. interface Command {
  35. public void execute();
  36. }
  37.  
  38. class LoginCommand implements Command {
  39. public void execute() {
  40. // do what doLogin() used to do
  41. }
  42. }
  43.  
  44. class RequestHandler {
  45. private Map<Integer, Command> commandMap; // injected in, or obtained from a factory
  46. public void handleRequest(int action) {
  47. Command command = commandMap.get(action);
  48. command.execute();
  49. }
  50. }
  51.  
  52. class House {
  53. private int state;
  54.  
  55. public void enter() {
  56. switch (state) {
  57. case INSIDE:
  58. throw new Exception("Cannot enter. Already inside");
  59. case OUTSIDE:
  60. state = INSIDE;
  61. ...
  62. break;
  63. }
  64. }
  65. public void exit() {
  66. switch (state) {
  67. case INSIDE:
  68. state = OUTSIDE;
  69. ...
  70. break;
  71. case OUTSIDE:
  72. throw new Exception("Cannot leave. Already outside");
  73. }
  74. }
  75.  
  76. abstract class HouseState {
  77. public HouseState enter() {
  78. throw new Exception("Cannot enter");
  79. }
  80. public HouseState leave() {
  81. throw new Exception("Cannot leave");
  82. }
  83. }
  84. class Inside extends HouseState {
  85. public HouseState leave() {
  86. return new Outside();
  87. }
  88. }
  89. class Outside extends HouseState {
  90. public HouseState enter() {
  91. return new Inside();
  92. }
  93. }
  94.  
  95. class House {
  96. private HouseState state;
  97. public void enter() {
  98. this.state = this.state.enter();
  99. }
  100. public void leave() {
  101. this.state = this.state.leave();
  102. }
  103. }
  104.  
  105. void InitMap(){
  106. Map[key1] = Object/Action;
  107. Map[key2] = Object/Action;
  108. }
  109.  
  110. Object/Action DoStuff(Object key){
  111. return Map[key];
  112. }
  113.  
  114. switch
  115.  
  116. IState
  117.  
  118. switch(*IP) {
  119. case OPCODE_ADD:
  120. ...
  121. break;
  122. case OPCODE_NOT_ZERO:
  123. ...
  124. break;
  125. case OPCODE_JUMP:
  126. ...
  127. break;
  128. default:
  129. fixme(*IP);
  130. }
  131.  
  132. opcode_table[*IP](*IP, vm);
  133.  
  134. ... // in somewhere else:
  135. void opcode_add(byte_opcode op, Vm* vm) { ... };
  136. void opcode_not_zero(byte_opcode op, Vm* vm) { ... };
  137. void opcode_jump(byte_opcode op, Vm* vm) { ... };
  138. void opcode_default(byte_opcode op, Vm* vm) { /* fixme */ };
  139.  
  140. OpcodeFuncPtr opcode_table[256] = {
  141. ...
  142. opcode_add,
  143. opcode_not_zero,
  144. opcode_jump,
  145. opcode_default,
  146. opcode_default,
  147. ... // etc.
  148. };
  149.  
  150. public class Animal
  151. {
  152. public abstract void Speak();
  153. }
  154.  
  155.  
  156. public class Dog : Animal
  157. {
  158. public virtual void Speak()
  159. {
  160. Console.WriteLine("Hao Hao");
  161. }
  162. }
  163.  
  164. public class Cat : Animal
  165. {
  166. public virtual void Speak()
  167. {
  168. Console.WriteLine("Meauuuu");
  169. }
  170. }
  171.  
  172. foreach (var animal in zoo)
  173. {
  174. echo animal.speak();
  175. }
  176.  
  177. if a = 1 then
  178. do something else
  179. if a = 2 then
  180. do something else
  181. else
  182. if a = 3 then
  183. do the last thing
  184. endif
  185. endif
  186. endif
  187.  
  188. if a = 1 then
  189. do something
  190. endif
  191. if a = 2 then
  192. do something else
  193. endif
  194. if a = 3 then
  195. do something more
  196. endif
  197.  
  198. procedure processA(a:int)
  199. if a = 1 then
  200. do something
  201. procedure_return
  202. endif
  203. if a = 2 then
  204. do something else
  205. procedure_return
  206. endif
  207. if a = 3 then
  208. do something more
  209. procedure_return
  210. endif
  211. end_procedure
  212.  
  213. private void doLog(LogLevel logLevel, String msg) {
  214. String prefix;
  215. switch (logLevel) {
  216. case INFO:
  217. prefix = "INFO";
  218. break;
  219. case WARN:
  220. prefix = "WARN";
  221. break;
  222. case ERROR:
  223. prefix = "ERROR";
  224. break;
  225. default:
  226. throw new RuntimeException("Oops, forgot to add stuff on new enum constant");
  227. }
  228. System.out.println(String.format("%s: %s", prefix, msg));
  229. }
  230.  
  231. for i from 1 to 1000 {statement1; statement2}
  232. if something=false then {statement1; statement2}
  233. while isOKtoLoop {statement1; statement2}
Add Comment
Please, Sign In to add comment