background preloader

Software Development

Facebook Twitter

Illustrated GOF Design Patterns in C# Part VI: Behavioral III - Download source files - 2 Kb Abstract Design Patterns, Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides [also known as the Gang of Four (GOF)] has been a de facto reference for any Object-Oriented software developer. This article is the conclusion of a series of articles illustrating the GOF Design Patterns in C#. We will discuss the Strategy, Template Method, and Visitor patterns.

Background In Design Patterns, each pattern is described with its name (and other well-known names); the motivation behind the pattern; its applicability; the structure of the pattern; class/object participants; participant collaborations; pattern consequences; implementation; sample code; known uses; and related patterns. A design pattern is not code, per se, but a "plan of attack" for solving a common software development problem. This article is meant to illustrate the design patterns as a supplement to their material. Behavioral Patterns Strategy. TheStrategyTemplatePattern - Pattern Repository Wiki. From Pattern Repository Wiki Motivation You have redundant structure in the forms of switches spread across several existing classes and methods.

All the cases in each switch are tightly coupled to the fact that you have a particular situation to handle (typically an object of a particular type). There are many problems with this. If you have to change one switch, you may have to change them all. With Shalloway's lawapplying, doing that without mistake is unlikely. This pattern is generally used as a transition to a better design - that is, a transition that takes place while reducing code debt. Encapsulation The Strategy Template pattern encapsulates the type of object being used from all of the switches involved.

Procedural Analog We're replacing a series of ifs with a group of related strategies. Non-Software Analog When I teach a course there are many similar things I have to do - but how I do them depends upon the situation I am in. To name a few. Implementation Forces Number of switches. C# sample code for interface injection - Young's Software for Me. Elegant Code. Refactoring Finds Dead Code. One of the many things that I just love about my job as a consultant/mentor is when I actually get to sit down with programmers and pair program with them.

This doesn’t seem to happen nearly as often as I would like, so when two developers at a recent client site asked me if I could look at some legacy code to see if I could figure out how to get some tests around it, I jumped at the opportunity. We acquired a room equipped with a projector and a whiteboard. A laptop was connected to the projector and we were all able to comfortably view the code. I visit a lot of different companies and see a lot of absolutely ghastly code. What I was looking at here wasn’t all that bad. Variable names were not chosen to limit keystrokes and method names appeared to be descriptive. This was good news, as I needed to understand how this was put together before I could offer help with a testing strategy. The developers clarified that they wanted help in testing private methods.