From 7850ebd5e43c89b504c8960248f7ed47a426b53d Mon Sep 17 00:00:00 2001 From: Corey McMahon Date: Wed, 9 Jan 2013 17:59:24 +1100 Subject: [PATCH 1/2] Added singleton pattern to the Design-Patterns page --- pages/Design-Patterns.md | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/pages/Design-Patterns.md b/pages/Design-Patterns.md index 53b0f92..cb3a9a8 100644 --- a/pages/Design-Patterns.md +++ b/pages/Design-Patterns.md @@ -62,6 +62,50 @@ yourself a lot of trouble down the road by using factories. * [Factory pattern on Wikipedia](https://en.wikipedia.org/wiki/Factory_pattern) +## Singleton + +When designing web applications, it often makes sense conceptually and architecturally to allow access to one and +only one instance of a particular class. The singleton pattern enables us to do this. + +{% highlight php %} + Date: Mon, 14 Jan 2013 15:28:25 +1100 Subject: [PATCH 2/2] Added a sentence defining the benefit or using DI over the singleton pattern --- pages/Design-Patterns.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pages/Design-Patterns.md b/pages/Design-Patterns.md index cb3a9a8..d57b79b 100644 --- a/pages/Design-Patterns.md +++ b/pages/Design-Patterns.md @@ -101,8 +101,9 @@ request lifecycle in a web application. This typically occurs when we have globa class) or a shared resource (such as an event queue). You should be wary when using the singleton pattern, as by its very nature it introduces global state into your -application, reducing testability. In most cases, dependency injection can (and should) be used in place of -singleton objects. +application, reducing testability. In most cases, dependency injection can (and should) be used in place of a +singleton class. Using dependency injection means that we do not introduce unnecessary coupling into the design of our +application, as the object using the shared or global resource requires no knowledge of a concretely defined class. * [Singleton pattern on Wikipedia](https://en.wikipedia.org/wiki/Singleton_pattern)