/src/lib/design_patterns/singleton.e

http://github.com/tybor/Liberty · 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. --
  4. deferred 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. insert
  21. ANY
  22. redefine
  23. is_equal
  24. end
  25. feature {ANY}
  26. is_equal (other: like Current): BOOLEAN
  27. do
  28. Result := other = Current
  29. end
  30. feature {}
  31. is_real_singleton: BOOLEAN
  32. do
  33. if singleton_memory_pool.fast_has(generating_type) then
  34. Result := singleton_memory_pool.fast_at(generating_type) = Current.to_pointer
  35. else
  36. singleton_memory_pool.add(Current.to_pointer, generating_type)
  37. Result := True
  38. end
  39. ensure
  40. assertion_check_only: Result
  41. end
  42. singleton_memory_pool: HASHED_DICTIONARY[POINTER, STRING]
  43. -- This pool is unique in the whole system. A memory is kept
  44. -- for each singleton type (type, not class) in the system.
  45. once
  46. create Result.make
  47. end
  48. current_is_not_an_expanded_type: BOOLEAN
  49. -- Check that the dynamic type of the SINGLETON is not an expanded type.
  50. local
  51. like_current: like Current
  52. do
  53. Result := like_current = Void
  54. end
  55. invariant
  56. current_is_not_an_expanded_type
  57. is_real_singleton
  58. end -- class SINGLETON
  59. --
  60. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  61. --
  62. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  63. -- of this software and associated documentation files (the "Software"), to deal
  64. -- in the Software without restriction, including without limitation the rights
  65. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  66. -- copies of the Software, and to permit persons to whom the Software is
  67. -- furnished to do so, subject to the following conditions:
  68. --
  69. -- The above copyright notice and this permission notice shall be included in
  70. -- all copies or substantial portions of the Software.
  71. --
  72. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  73. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  74. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  75. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  76. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  77. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  78. -- THE SOFTWARE.