Advertisement
Guest User

Untitled

a guest
Aug 14th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. class QuestionHandler
  2. attr_reader :successor
  3.  
  4. def initialize(successor = nil)
  5. @successor = successor
  6. end
  7.  
  8. def process_request(request)
  9.  
  10. if accept_request(request)
  11. return
  12.  
  13. elsif @successor
  14. @successor.process_request(request)
  15. else
  16. fail_request(request)
  17. end
  18. end
  19.  
  20. def fail_request(request)
  21. puts "The question '#{request}' could not be answered."
  22. end
  23.  
  24. def accept_request(request)
  25. raise '#accept_request method must be implemented.'
  26. end
  27. end
  28.  
  29. class StarWarsQuestionHandler < QuestionHandler
  30. def accept_request(request)
  31.  
  32. if request.include?("Star Wars")
  33. answer_question(request)
  34. return true
  35. else
  36. return false
  37. end
  38. end
  39.  
  40.  
  41. def answer_question(request)
  42. puts "answering a Star Wars related question: '#{request}'"
  43. end
  44. end
  45.  
  46. class HarryPotterQuestionHandler < QuestionHandler
  47. def accept_request(request)
  48.  
  49. if request.include?("Harry Potter")
  50. answer_question(request)
  51. return true
  52. else
  53. return false
  54. end
  55. end
  56.  
  57. def answer_question(request)
  58. puts "answering a Harry Potter related question: '#{request}'"
  59. end
  60. end
  61.  
  62. class LordOfTheRingsQuestionHandler < QuestionHandler
  63.  
  64. def accept_request(request)
  65. if request.include?("Lord of the Rings")
  66. answer_question(request)
  67. return true
  68. else
  69. return false
  70. end
  71. end
  72.  
  73. def answer_question(request)
  74. puts "answering a Lord of the Rings related question: '#{request}'"
  75. end
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement