/src/lib/design_patterns/chain_of_responsibility.e

http://github.com/tybor/Liberty · Specman e · 88 lines · 43 code · 8 blank · 37 comment · 2 complexity · c54d3b52b5218a461d14d3c727219b32 MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. deferred class CHAIN_OF_RESPONSIBILITY
  5. --
  6. -- The ''Chain Of Responsibility'' Design Pattern reified. Such a chain is made of handlers you can add or
  7. -- remove at your leisure.
  8. --
  9. -- The Chain itself is a ''Command'' (see that Design Pattern). Executing that command looks for a handler
  10. -- that can handle a request, and let it handle that request.
  11. --
  12. -- How you do implement the query is up to you. The handlers are given `Current' i.e. the Chain itself, and
  13. -- CHAIN_HANDLER is a generic class so you can implement your own protocol.
  14. --
  15. inherit
  16. COMMAND
  17. feature {ANY}
  18. add (c: CHAIN_HANDLER[like Current])
  19. -- Add a handler to the Chain.
  20. do
  21. chain.add_last(c)
  22. end
  23. remove (c: CHAIN_HANDLER[like Current])
  24. -- Remove a handler from the Chain.
  25. require
  26. has(c)
  27. local
  28. i: INTEGER
  29. do
  30. i := chain.first_index_of(c)
  31. chain.remove(i)
  32. end
  33. has (c: CHAIN_HANDLER[like Current]): BOOLEAN
  34. -- Does the handler belong to the chain?
  35. do
  36. Result := chain.has(c)
  37. end
  38. feature {ANY}
  39. execute
  40. -- Find a suitable handler to handle a request.
  41. local
  42. i: INTEGER; ok: BOOLEAN
  43. do
  44. from
  45. i := chain.lower
  46. until
  47. ok or else i > chain.upper
  48. loop
  49. if chain.item(i).can_handle(Current) then
  50. chain.item(i).handle(Current)
  51. ok := True
  52. end
  53. i := i + 1
  54. end
  55. end
  56. feature {}
  57. chain: FAST_ARRAY[CHAIN_HANDLER[like Current]]
  58. invariant
  59. chain /= Void
  60. end -- class CHAIN_OF_RESPONSIBILITY
  61. --
  62. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  63. --
  64. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  65. -- of this software and associated documentation files (the "Software"), to deal
  66. -- in the Software without restriction, including without limitation the rights
  67. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  68. -- copies of the Software, and to permit persons to whom the Software is
  69. -- furnished to do so, subject to the following conditions:
  70. --
  71. -- The above copyright notice and this permission notice shall be included in
  72. -- all copies or substantial portions of the Software.
  73. --
  74. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  75. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  76. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  77. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  78. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  79. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  80. -- THE SOFTWARE.