From 51f6769526c1e2886b73120de690b3240193b06a Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 7 Sep 2017 13:18:29 +0300 Subject: [PATCH 1/4] expand description of Singleton https://stackoverflow.com/a/138012/2455621 --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ff06378..8ea474e 100644 --- a/README.md +++ b/README.md @@ -627,7 +627,12 @@ And now you must use instance of `Configuration` in your application. **[⬆ back to top](#table-of-contents)** ### Don't use a Singleton pattern -Singleton is an [anti-pattern](https://en.wikipedia.org/wiki/Singleton_pattern). + +Singleton is an [anti-pattern](https://en.wikipedia.org/wiki/Singleton_pattern). Paraphrased from Brian Button: + 1. They are generally used as a global instance, why is that so bad? Because you hide the dependencies of your application in your code, instead of exposing them through the interfaces. Making something global to avoid passing it around is a [code smell](https://en.wikipedia.org/wiki/Code_smell). + 2. They violate the [single responsibility principle](#user-content-single-responsibility-principle-srp): by virtue of the fact that they control their own creation and lifecycle. + 3. They inherently cause code to be tightly [coupled](#user-content-single-responsibility-principle-srp). This makes faking them out under test rather difficult in many cases. + 4. They carry state around for the lifetime of the application. Another hit to testing since you can end up with a situation where tests need to be ordered which is a big no no for unit tests. Why? Because each unit test should be independent from the other. **Bad:** From 5bc8682740934437cff5400761d6e0a4ecbfb4e0 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 7 Sep 2017 13:24:32 +0300 Subject: [PATCH 2/4] correct link to Coupling --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8ea474e..b27f7d1 100644 --- a/README.md +++ b/README.md @@ -631,7 +631,7 @@ And now you must use instance of `Configuration` in your application. Singleton is an [anti-pattern](https://en.wikipedia.org/wiki/Singleton_pattern). Paraphrased from Brian Button: 1. They are generally used as a global instance, why is that so bad? Because you hide the dependencies of your application in your code, instead of exposing them through the interfaces. Making something global to avoid passing it around is a [code smell](https://en.wikipedia.org/wiki/Code_smell). 2. They violate the [single responsibility principle](#user-content-single-responsibility-principle-srp): by virtue of the fact that they control their own creation and lifecycle. - 3. They inherently cause code to be tightly [coupled](#user-content-single-responsibility-principle-srp). This makes faking them out under test rather difficult in many cases. + 3. They inherently cause code to be tightly [coupled](https://en.wikipedia.org/wiki/Coupling_%28computer_programming%29). This makes faking them out under test rather difficult in many cases. 4. They carry state around for the lifetime of the application. Another hit to testing since you can end up with a situation where tests need to be ordered which is a big no no for unit tests. Why? Because each unit test should be independent from the other. **Bad:** From 179cc1369e03dfca8e45eb162d5f484694c145de Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Thu, 7 Sep 2017 13:59:05 +0300 Subject: [PATCH 3/4] add thoughts by Misko Hevery --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index b27f7d1..c4d571a 100644 --- a/README.md +++ b/README.md @@ -634,6 +634,8 @@ Singleton is an [anti-pattern](https://en.wikipedia.org/wiki/Singleton_pattern). 3. They inherently cause code to be tightly [coupled](https://en.wikipedia.org/wiki/Coupling_%28computer_programming%29). This makes faking them out under test rather difficult in many cases. 4. They carry state around for the lifetime of the application. Another hit to testing since you can end up with a situation where tests need to be ordered which is a big no no for unit tests. Why? Because each unit test should be independent from the other. +There is also very good thoughts by [Misko Hevery](http://misko.hevery.com/about/) about the [root of problem](http://misko.hevery.com/2008/08/25/root-cause-of-singletons/). + **Bad:** ```php From 780af00c212905411771a7286bb07ed561e76722 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Sat, 9 Sep 2017 09:25:06 +0300 Subject: [PATCH 4/4] identified important parts --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9a8be43..d68583e 100644 --- a/README.md +++ b/README.md @@ -673,10 +673,10 @@ And now you must use instance of `Configuration` in your application. ### Don't use a Singleton pattern Singleton is an [anti-pattern](https://en.wikipedia.org/wiki/Singleton_pattern). Paraphrased from Brian Button: - 1. They are generally used as a global instance, why is that so bad? Because you hide the dependencies of your application in your code, instead of exposing them through the interfaces. Making something global to avoid passing it around is a [code smell](https://en.wikipedia.org/wiki/Code_smell). - 2. They violate the [single responsibility principle](#user-content-single-responsibility-principle-srp): by virtue of the fact that they control their own creation and lifecycle. - 3. They inherently cause code to be tightly [coupled](https://en.wikipedia.org/wiki/Coupling_%28computer_programming%29). This makes faking them out under test rather difficult in many cases. - 4. They carry state around for the lifetime of the application. Another hit to testing since you can end up with a situation where tests need to be ordered which is a big no no for unit tests. Why? Because each unit test should be independent from the other. + 1. They are generally used as a **global instance**, why is that so bad? Because **you hide the dependencies** of your application in your code, instead of exposing them through the interfaces. Making something global to avoid passing it around is a [code smell](https://en.wikipedia.org/wiki/Code_smell). + 2. They violate the [single responsibility principle](#single-responsibility-principle-srp): by virtue of the fact that **they control their own creation and lifecycle**. + 3. They inherently cause code to be tightly [coupled](https://en.wikipedia.org/wiki/Coupling_%28computer_programming%29). This makes faking them out under **test rather difficult** in many cases. + 4. They carry state around for the lifetime of the application. Another hit to testing since **you can end up with a situation where tests need to be ordered** which is a big no for unit tests. Why? Because each unit test should be independent from the other. There is also very good thoughts by [Misko Hevery](http://misko.hevery.com/about/) about the [root of problem](http://misko.hevery.com/2008/08/25/root-cause-of-singletons/).