Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Singleton Module and Its Pattern In Ruby

Fbc4aecf546e2c379dbb6277956ecec5?s=47 Mike Calhoun
November 19, 2019

The Singleton Module and Its Pattern In Ruby

The singleton design pattern allows for the creation of an object that is restricted to one single instance. No matter how big or complex your application is, there will only ever be one. This has caused some controversy about the quality and maintainability of code that leverages it. Yet, Ruby’s standard library contains a module that implements this pattern. Let’s take a closer look at this module and the “controversy” around its implemented pattern. Let’s also consider singletons in Ruby and the purpose they serve for the language in general.

Fbc4aecf546e2c379dbb6277956ecec5?s=128

Mike Calhoun

November 19, 2019
Tweet

More Decks by Mike Calhoun

Other Decks in Programming

Transcript

  1. The Singleton Module and Its Pattern In Ruby Ruby Conf

    2019 : Nashville, TN : 11.19.2019
  2. Hello! I’m Mike Calhoun You can find me at places

    on the internet where you would find people as @comike011 2
  3. 3 We’re Hiring!

  4. Overview 2. Singleton.rb 3. Ruby Singleton Class 1. The Singleton

    Pattern 4
  5. How Did We Get Here? Kismet or how curiosity got

    out of hand 5
  6. 6

  7. 7

  8. 8

  9. “ The Singleton module implements the Singleton pattern 9

  10. THANKS! Any questions? You can find me at @comike011 10

  11. Overview 2. Singleton.rb 3. Ruby Singleton Class 1. The Singleton

    Pattern 11
  12. 1. The Singleton Pattern 12

  13. What is The Singleton Pattern? ◦ Restricts to a single

    instance 13
  14. What is The Singleton Pattern? ◦ Restricts to a single

    instance ◦ Provides single access point to the resource 14
  15. What is The Singleton Pattern? ◦ Restricts to a single

    instance ◦ Provides single access point to the resource ◦ Not a global variable 15
  16. What is The Singleton Pattern? ◦ Restricts to a single

    instance ◦ Provides single access point to the resource ◦ Not a global variable ◦ Problematic misuse 16
  17. Are Singletons Bad? Bart Jacobs, January 16, 2018 17

  18. “ I agree that the singleton pattern seems convenient and

    it may occasionally be warranted to use it, but the drawbacks far outweigh the benefits. 18
  19. 6 Reasons Why You Should Avoid Singletons David Tanzer, last

    accessed 9/18/2019 19
  20. “ Yes, I am really writing a blog about why

    the “Singleton Pattern” is bad. In 2016. But it comes up in discussions time and again, so here is a list of reasons why you should avoid singletons. 20
  21. Why The Singleton Pattern Sucks and You Should Avoid It

    WDIS, February 29, 2016 21
  22. “ Most of these patterns [from GoF] are straightforward and

    frankly plain logical...However, there is one specific pattern that gets hate from every coding-related website on the Internet...the infamous Singleton Pattern, probably the most overused and frequently misplaced programming anti-pattern of all time 22
  23. Why Singletons are Evil Scott Densmore, May 25, 2004 23

  24. Singletons Are Pathological Liars Miško Hevery, April 17, 2008 24

  25. Patterns I Hate: #1 Singletons Alex Miller, July 3, 2007

    25
  26. Singletons I Love You, But You’re Bringing Me Down codingwithoutcomments,

    October 4, 2008 26
  27. Singletons Are Great and You Should Use Them 27

  28. Singletons Are Great and You Should Use Them Citation Needed

    28
  29. Concerns ◦ Concurrent access to a shared resource 29

  30. Concerns ◦ Concurrent access to a shared resource ◦ Global

    state 30
  31. Concerns ◦ Concurrent access to a shared resource ◦ Global

    state ◦ Violation of SRP 31
  32. Concerns ◦ Concurrent access to a shared resource ◦ Global

    state ◦ Violation of SRP ◦ Testing is problematic 32
  33. Concerns ◦ Concurrent access to a shared resource ◦ Global

    state ◦ Violation of SRP ◦ Testing is problematic ◦ Encourages tight coupling 33
  34. Common Appropriate Use Cases ◦ Unidirectional flow of data 34

  35. Common Appropriate Use Cases ◦ Unidirectional flow of data ◦

    Resource requested from multiple and disparate areas 35
  36. Common Appropriate Use Cases ◦ Unidirectional flow of data ◦

    Resource requested from multiple and disparate areas ◦ Logging 36
  37. Common Appropriate Use Cases ◦ Unidirectional flow of data ◦

    Resource requested from multiple and disparate areas ◦ Logging ◦ Configurations 37
  38. Common Appropriate Use Cases ◦ Unidirectional flow of data ◦

    Resource requested from multiple and disparate areas ◦ Logging ◦ Configurations ◦ Nothing modified within the application 38
  39. Overview 2. Singleton.rb 3. Ruby Singleton Class 1. The Singleton

    Pattern 39
  40. 2. singleton.rb 40

  41. singleton.rb ◦ .new and .allocate are made private 41

  42. singleton.rb ◦ .new and .allocate are made private ◦ .instance

    added 42
  43. singleton.rb ◦ .new and .allocate are made private ◦ .instance

    added ◦ .clone and .dup raise TypeError 43
  44. 44

  45. 45

  46. 46

  47. 47

  48. 48

  49. 49

  50. 50

  51. 51

  52. 52

  53. 53

  54. 54

  55. 55

  56. 56

  57. 57

  58. 58

  59. 59

  60. 60

  61. 61

  62. 62

  63. 63

  64. Overview 2. Singleton.rb 3. Ruby Singleton Class 1. The Singleton

    Pattern 64
  65. 3. Ruby Singleton Class 65

  66. Singleton class ◦ Also referred to as ‘eigenclasses’ or ‘metaclasses’

    66
  67. Singleton class ◦ Also referred to as ‘eigenclasses’ or ‘metaclasses’

    ◦ Somewhat hidden 67
  68. Singleton class ◦ Also referred to as ‘eigenclasses’ or ‘metaclasses’

    ◦ Somewhat hidden ◦ Essential to the Ruby Object Model 68
  69. 69

  70. The Ruby Object Model ◦ BasicObject, Object, <Defined> 70

  71. The Ruby Object Model ◦ BasicObject, Object, <Defined> ◦ Modules

    are included above their including object 71
  72. 72

  73. 73

  74. 74

  75. 75

  76. 76

  77. An Example What and where is the singleton class? 77

  78. 78

  79. 79 Porch Cat "PK"

  80. 80 Porch Cat "PK" Pearl

  81. 81

  82. 82

  83. 83

  84. 84

  85. 85

  86. 86

  87. 87

  88. 88

  89. 89

  90. 90

  91. 91

  92. 92

  93. 93

  94. 94

  95. 95

  96. 96

  97. 97

  98. 98

  99. 99

  100. 100

  101. 101

  102. 102

  103. 103

  104. 104

  105. 105

  106. 106

  107. 107

  108. 108

  109. 109

  110. 110

  111. 111

  112. 112

  113. 113

  114. 114

  115. 115

  116. 116

  117. 117

  118. 118

  119. 119

  120. 120

  121. 121

  122. Overview 2. Singleton.rb 3. Ruby Singleton Class 1. The Singleton

    Pattern 122
  123. 123

  124. THANKS! 124 Mike Calhoun (@comike011)