PageRenderTime 24ms CodeModel.GetById 17ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/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--
 4deferred 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
16inherit
17   COMMAND
18
19feature {ANY}
20   add (c: CHAIN_HANDLER[like Current])
21         -- Add a handler to the Chain.
22      do
23         chain.add_last(c)
24      end
25
26   remove (c: CHAIN_HANDLER[like Current])
27         -- Remove a handler from the Chain.
28      require
29         has(c)
30      local
31         i: INTEGER
32      do
33         i := chain.first_index_of(c)
34         chain.remove(i)
35      end
36
37   has (c: CHAIN_HANDLER[like Current]): BOOLEAN
38         -- Does the handler belong to the chain?
39      do
40         Result := chain.has(c)
41      end
42
43feature {ANY}
44   execute
45         -- Find a suitable handler to handle a request.
46      local
47         i: INTEGER; ok: BOOLEAN
48      do
49         from
50            i := chain.lower
51         until
52            ok or else i > chain.upper
53         loop
54            if chain.item(i).can_handle(Current) then
55               chain.item(i).handle(Current)
56               ok := True
57            end
58            i := i + 1
59         end
60      end
61
62feature {}
63   chain: FAST_ARRAY[CHAIN_HANDLER[like Current]]
64
65invariant
66   chain /= Void
67
68end -- class CHAIN_OF_RESPONSIBILITY
69--
70-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
71--
72-- Permission is hereby granted, free of charge, to any person obtaining a copy
73-- of this software and associated documentation files (the "Software"), to deal
74-- in the Software without restriction, including without limitation the rights
75-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
76-- copies of the Software, and to permit persons to whom the Software is
77-- furnished to do so, subject to the following conditions:
78--
79-- The above copyright notice and this permission notice shall be included in
80-- all copies or substantial portions of the Software.
81--
82-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
83-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
84-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
85-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
86-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
87-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
88-- THE SOFTWARE.