Eliminating `switch` statements [closed]

Switch-statements are not an antipattern per se, but if you’re coding object oriented you should consider if the use of a switch is better solved with polymorphism instead of using a switch statement. With polymorphism, this: foreach (var animal in zoo) { switch (typeof(animal)) { case “dog”: echo animal.bark(); break; case “cat”: echo animal.meow(); break; … Read more

Factory, Abstract Factory and Factory Method

The Gang Of Four “Design Patterns; Elements of Reusable Object-Oriented Software” book contains two entries, “Abstract Factory” (aka ‘Virtual Constructor’) and “Factory Method”. I don’t know about “Concrete Factory.” I’ve heard the term, but never given it too much thought. Factory Method In “Factory Method” an object has a method which is responsible for the … Read more

Is the Service Locator pattern any different from the Abstract Factory pattern?

I have stumbled across the same question while investigating these patterns. I think the major differences can be found between a Service Locator and a Factory (whether it is abstract or not): Service Locator ‘Locates’ an existing dependency (the service). Although the service may be created during resolution, it is of no consequence to the … Read more

Are design patterns really language weaknesses?

Some canonized design patterns — Adapter, Factory, Command, Visitor, etc — are approximations of features which are baked into other languages. Off the top of my head: Event-handlers in C# are baked-in versions of the observer pattern. Think about how you’d wire up events in C# if you had to roll your own observer each … Read more

Which Typesafe Enum in C++ Are You Using?

I’m currently playing around with the Boost.Enum proposal from the Boost Vault (filename enum_rev4.6.zip). Although it was never officially submitted for inclusion into Boost, it’s useable as-is. (Documentation is lacking but is made up for by clear source code and good tests.) Boost.Enum lets you declare an enum like this: BOOST_ENUM_VALUES(Level, const char*, (Abort)(“unrecoverable problem”) … Read more

Does a definitive list of design patterns exist? [closed]

I think there’s a basic “life cycle of a design pattern” Author writes about design pattern in a book. Book becomes well read, possibly best seller Design pattern enters public conscious, gains mindshare. Design pattern gets used. It works well. design pattern gets more mindshare Design pattern becomes panacea, gets over-used. Different Author writes “Design … Read more