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

The Singleton Module and Its Pattern In Ruby

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.

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. 6

  4. 7

  5. 8

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

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

    instance ◦ Provides single access point to the resource ◦ Not a global variable 15
  8. 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. “ 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. “ 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. “ 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. Concerns ◦ Concurrent access to a shared resource ◦ Global

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

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

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

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

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

    Resource requested from multiple and disparate areas ◦ Logging ◦ Configurations ◦ Nothing modified within the application 38
  18. singleton.rb ◦ .new and .allocate are made private ◦ .instance

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

  20. 45

  21. 46

  22. 47

  23. 48

  24. 49

  25. 50

  26. 51

  27. 52

  28. 53

  29. 54

  30. 55

  31. 56

  32. 57

  33. 58

  34. 59

  35. 60

  36. 61

  37. 62

  38. 63

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

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

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

    are included above their including object 71
  42. 72

  43. 73

  44. 74

  45. 75

  46. 76

  47. 78

  48. 81

  49. 82

  50. 83

  51. 84

  52. 85

  53. 86

  54. 87

  55. 88

  56. 89

  57. 90

  58. 91

  59. 92

  60. 93

  61. 94

  62. 95

  63. 96

  64. 97

  65. 98

  66. 99

  67. 100

  68. 101

  69. 102

  70. 103

  71. 104

  72. 105

  73. 106

  74. 107

  75. 108

  76. 109

  77. 110

  78. 111

  79. 112

  80. 113

  81. 114

  82. 115

  83. 116

  84. 117

  85. 118

  86. 119

  87. 120

  88. 121

  89. 123