The Singleton Module and Its Pattern In Ruby

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

Transcript

  1. 1.

    The Singleton Module and Its Pattern In Ruby Ruby Conf

    2019 : Nashville, TN : 11.19.2019
  2. 2.

    Hello! I’m Mike Calhoun You can find me at places

    on the internet where you would find people as @comike011 2
  3. 6.

    6

  4. 7.

    7

  5. 8.

    8

  6. 14.

    What is The Singleton Pattern? ◦ Restricts to a single

    instance ◦ Provides single access point to the resource 14
  7. 15.

    What is The Singleton Pattern? ◦ Restricts to a single

    instance ◦ Provides single access point to the resource ◦ Not a global variable 15
  8. 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
  9. 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
  10. 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
  11. 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
  12. 32.

    Concerns ◦ Concurrent access to a shared resource ◦ Global

    state ◦ Violation of SRP ◦ Testing is problematic 32
  13. 33.

    Concerns ◦ Concurrent access to a shared resource ◦ Global

    state ◦ Violation of SRP ◦ Testing is problematic ◦ Encourages tight coupling 33
  14. 35.

    Common Appropriate Use Cases ◦ Unidirectional flow of data ◦

    Resource requested from multiple and disparate areas 35
  15. 36.

    Common Appropriate Use Cases ◦ Unidirectional flow of data ◦

    Resource requested from multiple and disparate areas ◦ Logging 36
  16. 37.

    Common Appropriate Use Cases ◦ Unidirectional flow of data ◦

    Resource requested from multiple and disparate areas ◦ Logging ◦ Configurations 37
  17. 38.

    Common Appropriate Use Cases ◦ Unidirectional flow of data ◦

    Resource requested from multiple and disparate areas ◦ Logging ◦ Configurations ◦ Nothing modified within the application 38
  18. 43.

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

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

    44

  20. 45.

    45

  21. 46.

    46

  22. 47.

    47

  23. 48.

    48

  24. 49.

    49

  25. 50.

    50

  26. 51.

    51

  27. 52.

    52

  28. 53.

    53

  29. 54.

    54

  30. 55.

    55

  31. 56.

    56

  32. 57.

    57

  33. 58.

    58

  34. 59.

    59

  35. 60.

    60

  36. 61.

    61

  37. 62.

    62

  38. 63.

    63

  39. 68.

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

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

    69

  41. 71.

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

    are included above their including object 71
  42. 72.

    72

  43. 73.

    73

  44. 74.

    74

  45. 75.

    75

  46. 76.

    76

  47. 78.

    78

  48. 81.

    81

  49. 82.

    82

  50. 83.

    83

  51. 84.

    84

  52. 85.

    85

  53. 86.

    86

  54. 87.

    87

  55. 88.

    88

  56. 89.

    89

  57. 90.

    90

  58. 91.

    91

  59. 92.

    92

  60. 93.

    93

  61. 94.

    94

  62. 95.

    95

  63. 96.

    96

  64. 97.

    97

  65. 98.

    98

  66. 99.

    99

  67. 100.

    100

  68. 101.

    101

  69. 102.

    102

  70. 103.

    103

  71. 104.

    104

  72. 105.

    105

  73. 106.

    106

  74. 107.

    107

  75. 108.

    108

  76. 109.

    109

  77. 110.

    110

  78. 111.

    111

  79. 112.

    112

  80. 113.

    113

  81. 114.

    114

  82. 115.

    115

  83. 116.

    116

  84. 117.

    117

  85. 118.

    118

  86. 119.

    119

  87. 120.

    120

  88. 121.

    121

  89. 123.

    123