code_junkie

Dealing with nested if then else/nested switch statements

Nov 13th, 2011
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. function doWork(object x)
  2. {
  3.  
  4. if (x is a type of Apple)
  5. {
  6. x.Eat();
  7.  
  8. } else if (x is a type of Orange)
  9. {
  10. x.Peel();
  11. x.Eat();
  12. }
  13.  
  14. }
  15.  
  16. class FruitFactory
  17. {
  18. Fruit GetMeMoreFruit(typeOfFruit)
  19. {
  20. switch (typeOfFruit)
  21. ...
  22. ...
  23. }
  24. }
  25.  
  26. class X {
  27. public:
  28. int get_type(); /* Or an enum return type or similar */
  29. ...
  30. };
  31.  
  32. void eat(X& x) {
  33. switch (x.get_type()) {
  34. TYPE_A: eat_A(x); break;
  35. TYPE_B: eat_B(x); break;
  36. TYPE_C: eat_C(x); break;
  37. }
  38. }
  39.  
  40. void drink(X& x) {
  41. switch (x.get_type()) {
  42. TYPE_A: drink_A(x); break;
  43. TYPE_B: drink_B(x); break;
  44. TYPE_C: drink_C(x); break;
  45. }
  46. }
  47.  
  48. void be_merry(X& x) {
  49. switch (x.get_type()) {
  50. TYPE_A: be_merry_A(x); break;
  51. TYPE_B: be_merry_B(x); break;
  52. TYPE_C: be_merry_C(x); break;
  53. }
  54. }
  55.  
  56. class Base {
  57. virtual void eat() = 0;
  58. virtual void drink() = 0;
  59. virtual void be_merry() = 0;
  60. ...
  61. };
  62.  
  63. class A : public Base {
  64. public:
  65. virtual void eat() { /* Eat A-specific stuff */ }
  66. virtual void drink() { /* Drink A-specific stuff */ }
  67. virtual void be_merry() { /* Be merry in an A-specific way */ }
  68. };
  69.  
  70. class B : public Base {
  71. public:
  72. virtual void eat() { /* Eat B-specific stuff */ }
  73. virtual void drink() { /* Drink B-specific stuff */ }
  74. virtual void be_merry() { /* Be merry in an B-specific way */ }
  75. };
  76.  
  77. class C : public Base {
  78. public:
  79. virtual void eat() { /* Eat C-specific stuff */ }
  80. virtual void drink() { /* Drink C-specific stuff */ }
  81. virtual void be_merry() { /* Be merry in a C-specific way */ }
  82. };
  83.  
  84. /* Code Block 1... */
  85.  
  86. if(/* result of some condition or function call */)
  87. {
  88. /* Code Block 2... */
  89.  
  90. if(/* result of some condition or function call */)
  91. {
  92. /* Code Block 3... */
  93.  
  94. if(/* result of some condition or function call */)
  95. {
  96. /* Code Block 4... */
  97. }
  98. }
  99. }
  100.  
  101. /* Code Block 1... */
  102. IsOk = /* result of some condition or function call */
  103.  
  104. if(IsOK)
  105. {
  106. /* Code Block 2... */
  107. IsOk = /* result of some condition or function call */
  108. }
  109.  
  110. if(IsOK)
  111. {
  112. /* Code Block 3...*/
  113. IsOk = /* result of some condition or function call */
  114. }
  115.  
  116. if(IsOK)
  117. {
  118. /* Code Block 4...*/
  119. IsOk = /* result of some condition or function call */
  120. }
  121.  
  122. /* And so on... */
Add Comment
Please, Sign In to add comment