/src/lib/design_patterns/singleton.e
Specman e | 85 lines | 37 code | 7 blank | 41 comment | 1 complexity | 4762f21c620078fcd7119a58551210d3 MD5 | raw file
1-- This file is part of a Liberty Eiffel library. 2-- See the full copyright at the end. 3-- 4deferred class SINGLETON 5 -- 6 -- Implementation of the "Singleton" design pattern. The singleton pattern is a property of a type 7 -- (yes TYPE, not class) to have at most one single object created at runtime for the corresponding type. 8 -- 9 -- Usage: insert SINGLETON, that's all. 10 -- 11 -- For examples, look at the tools cluster of Liberty Eiffel. 12 -- 13 -- WARNING: double-check if the object is really a singleton in the conceptual sense. 14 -- The singleton pattern is often used in wrong place. See for example: 15 -- http://www.theagiledeveloper.com/articles/2005/03/03/singleton-overuse-disclaimer 16 -- http://blogs.msdn.com/scottdensmore/archive/2004/05/25/140827.aspx 17 -- http://www.softwarereality.com/design/singleton.jsp 18 -- or search "Singletonitis" or "singleton overuse". 19 -- 20 21insert 22 ANY 23 redefine 24 is_equal 25 end 26 27feature {ANY} 28 is_equal (other: like Current): BOOLEAN 29 do 30 Result := other = Current 31 end 32 33feature {} 34 is_real_singleton: BOOLEAN 35 do 36 if singleton_memory_pool.fast_has(generating_type) then 37 Result := singleton_memory_pool.fast_at(generating_type) = Current.to_pointer 38 else 39 singleton_memory_pool.add(Current.to_pointer, generating_type) 40 Result := True 41 end 42 ensure 43 assertion_check_only: Result 44 end 45 46 singleton_memory_pool: HASHED_DICTIONARY[POINTER, STRING] 47 -- This pool is unique in the whole system. A memory is kept 48 -- for each singleton type (type, not class) in the system. 49 once 50 create Result.make 51 end 52 53 current_is_not_an_expanded_type: BOOLEAN 54 -- Check that the dynamic type of the SINGLETON is not an expanded type. 55 local 56 like_current: like Current 57 do 58 Result := like_current = Void 59 end 60 61invariant 62 current_is_not_an_expanded_type 63 is_real_singleton 64 65end -- class SINGLETON 66-- 67-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file. 68-- 69-- Permission is hereby granted, free of charge, to any person obtaining a copy 70-- of this software and associated documentation files (the "Software"), to deal 71-- in the Software without restriction, including without limitation the rights 72-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 73-- copies of the Software, and to permit persons to whom the Software is 74-- furnished to do so, subject to the following conditions: 75-- 76-- The above copyright notice and this permission notice shall be included in 77-- all copies or substantial portions of the Software. 78-- 79-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 80-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 81-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 82-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 83-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 84-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 85-- THE SOFTWARE.