"However, there is one possible pitfall to this approach, as it makes this code possible:Singleton obj = Singleton.Instance;MyClass obj2 = new MyClass();While I personally like the idea of having the freedom to use the same class in two different ways throughout the application, I know some people like their Singletons - well, single.On the other hand, if you write a class from the start as a Singleton this is not an issue.There is an inherit risk in decoupling a class from it's expected behavior, so take this into consideration before using this pattern."
"don't believe discoverability was a motivation for Singleton. The purpose was to ensure only one instance existed (a print spooler, a file system). You can get discoverability through the technique you explain but the primary purpose of singleton was a pattern of prohibition - to stop a programmer doing something they shouldn't do by accident by enforcing the primary goals of single point of access. It is more than a global, and the OP, while comparing them to globals, does not acknowledge that this solves many of the problems which made "global" a 4-letter word. Knee-jerk reaction against globals is not healthy once you've mitigated their drawbacks. It is true that usage of singleton can be misguided and cause coupling - which is why you need to ensure that the usage of the pattern matches the motivation in the first place. This is a key point of design patterns which Alexander should have made clear in http://en.wikipedia.org/wiki/N...is_of_Form - the pattern is a result of a resolution of system of forces. It only satisfies that system - moving it to another different system will result in poor fitness. "
"design patterns are a sign of a deficiency of a language for the purpose that the design pattern addresses. In other words, the Visitor pattern used in Java points to the fact that Java is deficient in terms of list processing: the `map` and `filter` constructs need to be emulated with lengthy OO incantations."
"Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template method lets subclasses redefine certain steps of an algorithm without changing the algorithm strucuture"
class Program { static void Main(string[] args) { var alg = new Algorithm(); alg.DoSomething(Funky); } static bool Funky(string value) { return value == "somevalue"; } } class Algorithm { public void DoSomething(Func<string,bool>CanGoForward) { string someVariable = "somevalue"; if (null!=CanGoForward && CanGoForward(someVariable)) { //do one step } // whatever } }
Subscribe to RSS headline updates from: