CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeremy D. Miller -- The Shade Tree Developer

Under the hood and working with .Net, TDD, Software Design, and Agile Stuff

April 2006 - Posts

  • The State Pattern: An Underappreciated Pattern

    Following up on my post on Six Design Patterns to Start With, I mentioned the State pattern in regards to its similarity to the Strategy pattern.  It might not come up that often, but it's very effective in creating cleaner code in certain situations.

    From www.dofactory.com, the State pattern is

    Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

    Many objects need to behave differently in several ways when some sort of internal state changes.  The State pattern isolates the divergent functionality into swappable classes implementing a common interface.  The main object simply delegates to its internal state object at the appropriate times.  When the "state" property of the main object changes, it simply has to replace its internal state member with a different object. 

    Let's jump into an example.  For the last three years or so I've been using a data access layer framework I've written (and rewritten) to do very basic data access.  The Facade for the data access layer is called DataSession.  Among other things, DataSession wraps and manages all access to the underlying ADO.Net connection and transaction objects.  What I came up with was a design that allowed consumers of a DataSession to just drop off an IDbCommand for execution not have to worry about transaction boundaries or IDbTransaction objects.  The public interface of IDataSession looks like this:

        [PluginFamily("Default")]
        public interface IDataSession
        {
            bool IsInTransaction { get; }
     
            void BeginTransaction();
            void CommitTransaction();
            void RollbackTransaction();
     
            int ExecuteCommand(IDbCommand command);
            int ExecuteSql(string sql);
            IDataReader ExecuteReader(IDbCommand command);
            IDataReader ExecuteReader(string sql);
            DataSet ExecuteDataSet(IDbCommand command);
            object ExecuteScalar(IDbCommand command);
     
            ICommandCollection Commands { get; }
            IReaderSourceCollection ReaderSources { get; }
     
            void Initialize(IInitializable initializable);
        }

    Internally, the ADO.Net object usage varies a little bit between an "autocommit" mode where each command is its own transaction and a full blown transactional mode.  We could write if/then logic inside each method in DataSession to check if it is in a transactional state, but that will make each method longer, messier, and create some duplication.  This is where the State pattern comes in to play with an interface called IExecutionState.

        public interface IExecutionState
        {
            int Execute(IDbCommand command);
            IDataReader ExecuteReader(IDbCommand command);
            DataSet ExecuteDataSet(IDbCommand command);
            object ExecuteScalar(IDbCommand command);
        }

    DataSession has a private field called _currentState that holds an IExecutionState object.  Take a look at this method in DataSession:

            public int ExecuteCommand(IDbCommand command)
            {
                try
                {
                    return _currentState.Execute(command);
                }
                catch (Exception ex)
                {

    // Just wraps the ADO exception with a full
    // description of the IDbCommand that failed,
    // including parameter values
                   throw new CommandFailureException(command, ex);  
                }
            }

    All it does is delegate to the active State object and provide some consistent exception handling.  So, how does the State object get set?  First off, the DataSession starts with an AutoCommitExecutionState object in the _currentState field.  The state object is replaced when a transaction is started.

            public void BeginTransaction()
            {
                if (IsInTransaction)
                {
                    throw new ApplicationException("A transaction is already started!");
                }
     
                _transactionalState.BeginTransaction();
     
                // Replace the _currentState with the transactional state
                _currentState = _transactionalState;
            }

    Then return to the "autocommit" state whenever the transaction is committed or rolled back.

            public void CommitTransaction()
            {
                if (!IsInTransaction)
                {
                    throw new ApplicationException("A transaction is not started!");
                }
     
                _transactionalState.CommitTransaction();
     
                // Replace the _currentState with the default "Auto Commit" state
                _currentState = _defaultState;
            }

    What did the usage of the State pattern here accomplish?  From my perspective it eliminated a lot of if/then logic and greatly simplified the DataSession class.  It also isolated the transactional code in a single spot where it became easier to find later, and that's awfully important. 

    Some Other Examples

    Occasionally connected Smart Clients are a hot topic the past couple years.  At several points the smart client has to behave differently based on whether or not its connected to its backend or running locally.  I've never done this, but I bet this would be a perfect opportunity to use a State pattern.

    I've used the State pattern effectively with user interface screens.  Say you have a screen that enables users to edit some sort of business entity.  The screen will probably need to behave differently based on whether or not the entity is being created, viewed, or edited.  On a project a couple years ago we were able to reuse the same screen to perform several different actions by making the screen controller class delegate to an internal State object for certain operations within the normal screen flow.  It enabled us to collapse a couple screens down to one and allowed us to add additional actions to the screen with less effort.  This might be relevant to my current project.  We are building a little application to manage a simple workflow of a new type of business entity with a couple different steps.  We might be able to utilize a state pattern that enables or disables screen actions based on the current state of the business entity and the type of user using the screen.

    Advantages of the State Pattern

    1. Kill off if/then statements.  This is one of the primary goals of many of the original Gang of Four patterns, and it's a worthy goal.  If/then branching can breed bugs.
    2. Reduces duplication by eliminating repeated if/then or switch statements, same as its close cousin the Strategy pattern.
    3. Increased cohesion.  By aggregating state specific behavior into State classes with intention revealing names it's easier to find that specific logic, and it's all in one place.
    4. Potential extensibility.  Going back to DataSession, I might want to add some sort of asynchronous or delayed caching mode to DataSession.  I could do that by implementing a new version of the IExecutionState interface.
    5. Better testability.  We could have solved the DataSession state problem by using inheritance and creating separate AutoCommitDataSession and TransactionalDataSession classes, but the old "favor composition over inheritance" rule in OO design says that's not the best idea and I agree.  I was able to unit test the inner workings of DataSession effectively by using mock objects to test the interaction between DataSession and its IExecutionState object without touching a database.
  • Mort + RAD == Business Value Now && IT Headaches Later

    I think everybody and their little brother has seen Jeffrey and Scott's posts bashing the MS personas (I still like the name "Elvis" though) and calling for a higher skill level across the board for developers.  I largely agree with both posts but I've heard it all before.  However, Tim Weaver's response to Jeffrey titled Is Software Development Too Easy struck a nerve with me in a big way.  Tim describes an unfortunately common situation in many shops. 

    From Tim,

    My first job at Dell was with Business Systems Management. We wrote the software that Dell IT couldn't because they didn't have time. Make no mistake about it, we were Mort's. We had some above average skills, but we were all just learning. Every company I've been at since then has had the same arrangement. There is the official IT projects and then there is the "unofficial" IT projects. The unofficial are almost always done by those dual trade individuals who aren't professional programmers, but have enough skill to put something out there.

    I can very accurately describe what happens with these "Customer Supported Systems" (CSS) because I've both built them as a Shadow IT coder and had to take over the resulting CSS systems built by the business.  In Tim's case, I was one of the poor fools in Dell IT that had to take over CSS tools when they became too valuable to not have IT support.

    The business spots a need or an opportunity to use some sort of automation to improve productivity.  IT can't do it, won't do it without some bureaucratic rigmarole, or is perceived to be unresponsive or even flat out incompetent.  Into the void steps Mort, armed with a host of Rapid Application Development tools from Microsoft.  Mort is probably a Clark Kent type in the business by day, but morphs into Mort when the need arises.  Unsurprisingly, Mort understands very well what will provide value to the business because he IS the business.  I've bashed the RAD mindset before, and probably will again, but they're undeniably a valuable tool for folks moonlighting as a developer to create business value.

    So everything is going great.  Mort is a hero and the business gets some value.  Then suddenly things aren't good anymore.  Maybe the CSS goes down and the moonlighting developer has gone back to his day job or moved on.  Maybe the CSS just can't handle the load anymore.  Maybe the CSS is broken when the underlying systems or databases that the CSS rides on change underneath it.  Regardless of the cause, IT finally needs to be called in to support the tool. 

    Most likely, the tool isn't written very well.  Writing software initially is easy, especially with the type of data aware RAD tools that Microsoft has perfected, but writing software that can be maintained over time is a job for truly professional software developers.  So now IT is stuck either supporting a poorly written tool or being tied down doing a rewrite -- while the business happily churns out more CSS's.  In a particularly bad case I was peripherally involved with, an IT team had very little flexibility in changing a major application because the business had written so many CSS applications to automate the mission critical manufacturing process that screen scraped the official IT system (just in case your wondering, I'm pretty sure screen scraping is a really bad idea). 

    My all time favorite story is something that happened to a former colleague of mine.  He got to spend about 6 weeks trolling through a 900 page (not a typo) ASP classic application  written as a CSS looking for places where ADO connections were not being closed.  How in the world a CSS was allowed to grow that large without IT involvement is beyond me.  IT ended up in a really bad spot supporting the evolving ASP system while trying to simultaneously replace it with a scalable J2EE replacement.  Replacing a moving target most certainly doen't lead to a high chance of success.

    We Have to Be Responsive to the Business

    To a point I think the CSS phenomenon is healthy.  It certainly leads to a great deal of innovation.  The headaches for IT could be headed off if WE could respond to the business's needs faster and turn on a dime to fulfill emergent business opportunities.  In other words, we shouldn't give the business a reason to go around us for solutions.  We can't afford to throw process and scheduling hurdles at the business.  Yes, we're afraid of scope change but we've got to get over it and work in a way that manages change instead of preventing change.

    Unsurprisingly I think the answer to the CSS problem is Agile Development.  Quoting the Agile Manifesto - "Customer collaboration over contract negotiation."  I think that is typically thought of in terms of third party service providers, but that sentence should definitely apply to internal IT (in our case Engineering) being responsive to and cooperative with the business.  Iterative development is a no-brainer.  Within reason, the development teams should be able to flexibly change the their workload to accommodate changing business priorities.  The typical quarterly or semi-annual release that's scheduled months and months in advance in a fabulously detailed Gantt chart just isn't flexible enough. 

    How Not to Be Responsive to the Business

    I worked in a big IT shop with a disastrously poor relationship with the business and a nearly crushing CSS support burden.  The natural reaction was to form a committee that was tasked with creating a structural solution to the CSS problem.  A couple months later they presented a proposal that involved a complex multi-step workflow for the business to turn the proposal into an actual IT project, i.e. it put more hurdles in the way of the business and gave them more incentive to go around us.  Needless to say, the business didn't accept the plan.  Our counter-proposal to form a "swat" team that would be permanently tasked to small customer requests and worked in an iterative process was shot down for some sort of political reason.  Nothing improved.

    Quick and Clean

    You're free to disagree, but I'm one of the legion of folks that think the traditional drag and drop RAD programming leads to un-maintainable systems.  I'm pretty early into my exposure to Ruby on Rails (RoR), but my initial impression is that RoR's goal of "quick and clean" development that does not sacrifice maintainability for development speed looks like a move in the right direction.

     

    I started as a Mort, just a guy who wanted to automate some stuff for my engineering team.  I picked up Access and ASP classic and churned out tools for my engineering group.  I quickly found that I enjoyed the automation work far more than being an engineer and embarked on a quest to become a real developer.  Not surprisingly, my CSS tools had to be completely rewritten after I left my first job.

  • I'm an IRecividist

    I apologize in advance for the poor taste displayed by this post, but it was a rough day.

    Harry Pierson's post on Sacred Code Cows made me feel a little a bit guilty.  I've completely shaken off the bonds of Hungarian Notation, except for the silly ISomething convention for interfaces that is so prevalent in the .Net world.  I can't abide CSomething, and I certainly can't stand the old m_strSomething nonsense from VB6 coding.  We tried to stop using the "I" prefix on a .Net project a couple years ago, but it just confused people who expected the interfaces to start with an "I."

    The "I" prefix has been much harder to eliminate, but the slavish adherence to an outdated idea about coding conventions got me in some trouble.  At the Austin Code Camp last month I did a talk on Code Smells.  Of course I had to do the obligatory refactoring from a repeated switch statement to a strategy pattern.  I appropriated and adapted a sample of this refactoring based on the famous "Coconut-laden swallow" bit from Monty Python and the Holy Grail.  So as everybody who's seen the movie (it's an absolute necessity for understanding the culture of the typical software development shop) knows, the European swallows have different payload and wing speed capabilities than African swallows.  In my Code Camp sample I refactored a pair of switch statements on the type code of swallow to a series of strategy classes that all implemented a common interface.  Of course, you can probably guess what I called the interface for types of swallow. 

    And yes, I had a Ron Burgundy moment.

  • Finishing Off Each Iteration with a Deployable Product

    In my last post I talked a little bit about the difficulties that my team is facing in driving the user stories in each user story to "done, done, done."  Last Thursday we did a release retrospective meeting on our just concluded release.  One of the keys to making these meetings succeed is to walk away with something actionable to improve the next release.  An account manager at my former employer used to say that you asked the question "If you could change one thing for the next release, what would it be?"  On our previous release the obvious answers were developer/tester interaction and poor build and configuration management.  We finally got a tester onsite for the team and sunk a lot of ultimately successful time investment into better build automation.  This time the clear problems are the related issues of context switching and leaky iterations, i.e. not finishing all the user stories inside the iteration.

    Context switching has historically killed my team's productivity.  Several times we've had to abandon half-finished work to focus on a new higher priority.  Besides the obvious problem that the human mind just isn't efficient with a lot of context switching, we weren't able to immediately deploy the new priority code because the previous work had the system all torn up (yes, some judicious branching would have helped too).  It's also been more difficult to plan release dates when work keeps bleeding from one iteration to the next.  With our situation as a tiny little software vendor making solutions for very large customers, we really have to be able to accommodate turbulent changes in the release roadmap.  Finishing off our biweekly iterations with production quality code is a good way to ameliorate the roadmap turbulence.

    The Story Wall

    My favorite way to track user stories (I really don't think the Scrum burndown chart gets the job done) is the XP story wall that places the story cards in vertical columns denoting the state of the user story.  On day 1 of the iteration the story wall will look something like this below with all of the user stories  in the "Not Started" column.

    Not Started In Development Testing Awaiting Customer Signature Done, done, done!
    Story #1        
    Story #2        
    Story #3        
    Story #4        
    Story #5        
    Story #6        
    Story #7        
    Story #8        
    Story #9        

    At the halfway point of the iteration the story wall should look something like the chart below.  If the iteration is going smoothly, you should be able to roughly draw a continuous line from the upper right hand corner to bottom left.  Some stories should already be complete and others pushed into testing.   

    Not Started In Development Testing Awaiting Customer Signature Done, done, done!
            Story #1
            Story #2
          Story #3  
        Story #4    
        Story #5    
      Story #6      
      Story #7      
    Story #8        
    Story #9        

    So the next question is how do you work an iteration to get this smoothly continuous flow of stories from left to right?  One of the most important lessons newcomers to XP development need to learn is that you should not treat iterations as mini-waterfalls.  My first "real" XP project and my last project both suffered from what Michael Feathers calls trailer-hitched QAIf you dump all of your stories onto testing near the end of the iteration, you will not complete the iteration.  If the QA testing is an iteration behind you can't really take credit for finishing the work and you bear a context switching cost as developers switch between old and new code.  The only way to successfully finish off iterations is to get code into testing as soon as possible, and the testing team needs to be ready to start testing as soon as they get the new code.  That's not enough by itself, you also have to ruthlessly eliminate inefficiency in handoffs between disciplines.

    Here's how we're planning to plug the leaky iteration problem:

    • Prioritize the completion of stories in flight before putting new stories into play
    • Continuous Integration - I really can't say this forcefully enough, comprehensive, automated builds reduce project friction and make you go faster.  What we call "NAnt-y" work is not overhead, it's an investment in team efficiency.  Throughout my career I've wasted countless hours doing manual code moves and troubleshooting botched testing installs.  Nothing aggravates me more than defects that originate from a testing environment problem.  It just makes developers look dumb.  "We fixed that, but it just didn't get into the build somehow."  Our CI infrastructure now extends to making testing deployments with accompanying environment tests to ensure the testing environment is valid before a tester spends any time on that build.
      • On a related note, pay attention to the build number any time you move a story to testing. The testers need to know the build number that includes the new functionality to test.  That's a real problem because you're potentially pushing code to the testers more often and they need to know what they have.  The testers really need an easy way to query the system to see what the build number is that is installed (some kind of "about" screen for example).  On the other hand, the testers really need to watch the build number on any bugs they report.
    • Smaller User Stories - This is huge.  My vote is that user stories are cut into the smallest units that make sense to test independently.  Smaller stories generally lead to more accurate estimates and a smoother flow of stories into testing.
    • Don't carry non-trivial bugs between iterations.  Zero tolerance.
    • Acceptance Test Driven Development - We've been using FitNesse* for automating and expressing acceptance tests for some time, but only recently has it started to pay off.  The next step we're going to try on this release is to require that the FitNesse test tables for a user story be written before development.  There's a couple of good reasons to try this:
      • Unambiguous requirements - Creating a FitNesse test forces analysis questions out of hiding.  Writing a FitNesse test means getting down to the nitty gritty of exactly how a system is supposed to behave
      • Executable requirements - We can validate our code against the requirements.  Even better yet, if we and the testers have set the tests up early, we code until the FitNesse tests pass without involving the testers.  It's a much, much faster feedback cycle than pushing code to a tester only to play defect tracker volleyball. You can't express everything in FitNesse, but the more you can do will enable more time for manual testing and exploratory testing of the things FitNesse can't handle. 
      • Creating a shared understanding with the testers.  I've suffered quite a bit in the past when the testers have simply interpreted requirements differently than I did.  Writing the FitNesse tests together upfront, along with the product manager and customer, can drastically reduce that problem.  It also helps to create Eric Evan's Ubitiquous Language on a project, the grammar of the fixture tables should closely reflect your Domain Model.  Theoretically, I'm hoping it eliminates the intellectual lag between developers and testers when we hand them a new story to test.  This way the testers should never be learning about a story for the first time when we hand it over.
    • Just a little bit of ceremony.  Do 5-10 minute meetings when you start a new story for the first time across disciplines.  When you move a story between columns on the story wall, make sure the rest of the team knows that.  When testers find bugs and move stories back to development, they need to verbally tell us.  Defect tracking tools are for auditing, not communication!  Ideally, I like testers to show me the bug on their box as they're entering the bug.  I've found that little practice greatly reduces the amount of time it takes to turn around a bug fix.  If for no other reason, do iteration retrospectives just to feel like the iteration boundaries are real.

     

    * My first reaction to Fit/FitNesse was WTF?  My second experience with Fit was trying to write an ActionFixture to test a WinForms client. Pain.  Late last year we bought several copies of Fit for Developing Software and suddenly it all made sense.  Also grab the FitLibrary for .Net. It makes complicated tests and state management inside of FitNesse tests much smoother.  The point being to give FitNesse a real try because it can pay off in a big way. 

    Cory Foy has a nice getting started introduction to FitNesse.

    James Shore has a series of articles on using Fit/FitNesse for requirements analysis here.

     

    <SARCASM>

    Architecture, design, and coding are the most intellectually demanding and important activities on a software project.  That's why we developers, the talented few, take this work.  That said though, there's a lot of other process activities (iteration management, testing, requirements, etc.) that can negatively impact our ability to create code.  The project team's process is far too important to be trusted solely to project managers and managers in general (and most certainly not to CMM enforcers, SEPG teams, and external "Quality Assurance" bureaucrats who have absolutely no stake in your project succeeding).  We developers need to exert some amount of control over the team's processes.

    </SARCASM>

  • "Code Complete" is a lie, "Done, done, done" is the truth

    Before you flame me, I'm not talking about the canonical book by Steve McConnell.  What I mean is that this statement is a lie - "we're code complete on feature xyz."  That phrase is a lie, or at least misleading, because you really aren't complete with the feature.  Code complete doesn't tell you anything definitive about the quality of the code, or most importantly, the readiness of the code for actual deployment.  Code complete just means that the developers have reached a point where they're ready to turn the code over for testing.  You say the phrase "code complete" to mark off a gate on a schedule or claim earned credit for coding work done on a project schedule.  Using "code complete" to claim earned value is a bald-faced lie because it doesn't translate into business value - yet.  It could have lots of bugs and issues yet to be uncovered by the testers.  If the code hasn't gone through user acceptance testing, it might not even be the right functionality.

    It's a nearly arbitrary status anyway.  Oftentimes a team will call something code complete just because the date on the schedule says its supposed to be code complete on that date.  Just call it code complete to satisfy the schedule, all software has bugs anyway right?  Code complete is one of many common evils on waterfall projects when team members have somewhat divergent goals.  Developers may be judged by meeting the code complete date, and the testers by the quality of the system - i.e. the lack of bugs.  These conflicting goals can easily have a negative impact on the project.

    One of my favorite aspects of XP development has been the emphasis on creating working software instead of obsessing over intermediate progress gates and deliverables.  In direct contrast to "Code Complete," XP teams use the phrase "Done, done, done" to describe a feature as complete.  "Done, done, done" means the feature is 100% ready to deploy to production. 

    There's quite a bit of variance from project to project, but the "story wall" statuses within an iteration I learned on my first XP project were:

    1. Not started
    2. Development
    3. Business Analyst Review (The developers called it "BA Volleyball," I think this was a process smell)
    4. Testing
    5. Customer Review
    6. Done, done, done

    The other story wall columns besides "done, done, done" are just intermediate stages that help the team coordinate activities and handoffs between team members.  We use a Scrum style burndown chart to track intermediate progress in an iteration, but it's nothing better than an educated guess of status.  The burndown chart informs management on the state of the iteration and helps spot problems and changes in the iteration plan, but the authoritative progress meter is the number stories crossing the line into the "done" column.  The goal of the XP team in any given iteration is to get every story in play into the "done, done, done" column before the end of the iteration.  No credit or progress is earned on an XP project until a user story has been coded, fully tested, and approved by the business customer.  It's a draconian measure for a team, but it's an accurate indication of how much business value an XP team has really delivered.  XP nirvana is being able to consistently push all iteration stories into the "done, done, done" column and produce a potentially deployable release at the end of every iteration.  It's a lofty goal that most XP teams probably don't meet, but that doesn't mean we should stop trying to reach that goal.  Like so many agile teams we struggle with leaky iterations because we aren't fully driving stories to production-ready completion within an iteration, but that's the topic for my next post...

    A friend of mine who's still mired in waterfall land challenged me one time on how an XP can know their status without a detailed plan.  My response was that our status was measured by what new features were ready to deploy.  If you think about it, that's a far more useful measurement than saying x number of features are "code complete" with unknown quality attributes. 

    A very important attribute of agile methods in general is the entire team sharing a common goal to produce working software.  My experience has been that developer/tester interaction is far smoother in agile projects (certainly not perfect of course), in no small part because the developer's purview moves from getting code into test to getting code to a potentially deployable state.  As an agile developer it's in your best interest to do everything possible to make testing go smoothly - helping with test automation, build automation, unit testing, etc.  Testers are your allies now, not sparring partners.

  • Six Design Patterns to Start With

    One of my colleagues asked me to demonstrate and explain six patterns for folks with little or no exposure to design patterns.  Thou shall count to 6, no more, no less and 8 is right out.  Choosing the six patterns to cover first was actually a bit difficult.  I'm sticking strictly with the original "Gang of Four" patterns initially, because that's what you'll bump into the most often.  I scanned over the frequency rating of each pattern listed at www.dofactory.com, but discarded patterns like Observer and Iterator that are tightly integrated into the .Net framework itself.  Singleton is out because it often does more harm than good (I inherited a system that overused singleton's.  We're almost done ripping them out).  I've got a post in the distant future about a major refactoring I made to StructureMap with a Composite and Visitor pattern combo that did wonders to improve the cyclomatic complexity numbers and unit test coverage, but those are definitely not starter patterns.  In the end I ended up choosing these six to start, partially based on the relevance to work my team is currently doing. 

    1. Template Method
    2. Strategy
    3. Factory Method
    4. Abstract Factory
    5. Facade
    6. Command

    If you've done any amount of object oriented programming you've probably used all of these patterns because they're just little solutions that constantly reoccur (hence the name "pattern").  Learning the names of the design patterns and being conscious of pattern usage is effectively an index to a lot of existing writing and design wisdom about prior design pattern usage.  By themselves, the pattern names are valuable as a communication tool with other developers.

    Just for fun, I'm going to try to use open source tools for the source of most of the examples.  And for Scott Bellware I'm switching to FrontPage for editing blog posts.  We'll see how it goes.  When he suggested the switch my gut reaction was "but it will eat my ASP code!"  Some memories just won't stay down.

    Please keep in mind that learning about patterns means more than memorizing pattern names and the mechanics of using the patterns.  Learning the benefits and the drawbacks of patterns is even more important, you can look up the mechanics later.

    Template Method

    "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's structure. "

    Let's jump right into an example.  You're building a component that receives and processes incoming messages.  Each type of message is handled and processed differently, but there are some common steps that are always performed in the same order.  In this case you might create a common base class for all of the MessageProcessor classes like the one below to enforce and reuse the basic sequence of actions. 

        public interface IMessageAuditor
        {
            void Audit(string messageType, string userName, 
                string correlationId, string messageBody);
        }
     
     
        public abstract class MessageProcessor
        {
            private readonly IMessageAuditor _auditor;
     
            public MessageProcessor(IMessageAuditor auditor)
            {
                _auditor = auditor;
            }
     
            public void ProcessMessage(Message message)
            {
                IPrincipal principal = Thread.CurrentPrincipal;
                if (this.authorize(message, principal))
                {
                    doWork(message);
                    audit(message, principal);
                }
            }
     
            // Template Methods that must be overriden by subclasses
            protected abstract string messageType {get;}
            protected abstract bool authorize(Message message, 
                IPrincipal principal);
            protected abstract void doWork(Message message);
     
            private void audit(Message message, IPrincipal principal)
            {
                _auditor.Audit(this.messageType, principal.Identity.Name, 
                    message.CorrelationId, message.Body.ToString());
            }
        }
     

    In order, each MessageProcessor handles a message by these steps in the ProcessMessage(Message) method:

    1. Get the current IPrincipal containing information about the identity and security rights of the authenticated user
    2. Call the authorize(Message, IPrincipal) method to check for security rights.
    3. If the user is authorized, call the doWork(Message) method to process the message
    4. Call the audit() method that uses the messageType property to create a new audit trail record of the messaging activity

    In this case the messageType {get;}, authorize(), and doWork() methods are Template Methods. To create a specific CancelOrderMessageProcessor we merely need to inherit from MessageProcessor and implement these three methods.

        public class CancelOrderMessageProcessor : MessageProcessor
        {
            public CancelOrderMessageProcessor(IMessageAuditor auditor)
                 : base(auditor)
            {
            }
     
            protected override string messageType
            {
                get { return "CancelOrder"; }
            }
     
            protected override bool authorize(Message message, IPrincipal principal)
            {
                // go check whether the IPrincipal user is the same user who placed the order
                // referenced by the contents of the message
            }
     
            protected override void doWork(Message message)
            {
                // find the order number and cancel the order in the backend system
            }
        }

    So what have we really accomplished here?  For one thing we've standardized the message processing.  If we need to handle any new message types we can just create a new subclass and fill in the missing pieces.  Somebody new to the project can create a new message type by following the established pattern.  We've also eliminated some code duplication, and that's always a good thing.

    The Template pattern is definitely a double-edged sword.  Used judiciously it eliminates duplication, standardizes the implementation of related classes, and *can* lead to better productivity.  The downside of Template Method's is the same as inheritance in general.  The workings of a class are split between a super and sub class.  My main warning for the Template Method is to avoid an "Analysis Paralysis" condition where you spend too much time obsessing over the perfect abstractions upfront.   I'll freely admit that I didn't completely buy into the XP ideas of simple design and continuous design at first, but I've learned that it's often easier to introduce the superclass/Template Method combinations only after the second or third similar class.  Refactoring tools like ReSharper in conjunction with unit tests make it easier to introduce an abstract super class with its automated "Extract Superclass" refactoring.  In other words, use the Template Method defensively because you don't have to get it perfect upfront.

    Strategy

    "Define a family of algorithms, encapsulate each one, and make them interchangeable."

    The Strategy pattern is a widely used way move repeated if/then logic to polymorphism.  Here's a quick example from a previous job where I worked with supply chain and reverse logistics software for a large manufacturing company.  One of the things you have to account for in your supply chain is the differing means of shipping parts to the manufacturing lines.  Parts to the factory might be supplied by air, truck, or train depending on pricing, availability, and urgency.  There are several places in the code where the logic needs to branch to account for the differing means of shipping to the factory.  Without using a Strategy pattern you might start seeing something like the following switch statement being repeated in your code.

                switch (shippingType)
                {
                    case ShippingType.Air:
                        // Air shipping specific logic and functions
                        break;
     
                    case ShippingType.Truck:
                        // Trucking specific logic and functions
                        break;
     
                    case ShippingType.Train:
                        // Train shipping specific logic and functions
                        break;
     
                    default:
                        throw new ArgumentOutOfRangeException("shippingType", shippingType, "Unrecognized shipping type!");
                }

    I wouldn't say that switch statements are inherently evil, but a repeated switch statement on shipping type in different places is evidence of the classic "Switch Statement Smell."  Repeated switch statements can easily lead to duplicated code, and longish switch statements, in my experience, can lead to code that is hard to read and buggy.  Seeing a repeated switch statement on a type code is a sign that there is an abstraction in your code that's trying to come out.  Now, let's say that we went looking for the different times our code switches on shipping type and used this list to define a new interface to represent a new shipping strategy abstraction.

        public interface IShippingStrategy
        {
            DateTime DetermineExpectedArrivalTime(ShippingOrder order);
            Decimal CalculateShippingCost(ShippingOrder order);
            bool IsAvailable(ShippingOrder order);
        }

    Next, build out the concrete implementations of IShippingStrategy for air, truck, and train shipping.  Move all specific logic that was in the switch statements into the appropriate Strategy classes.

        public class AirShippingStrategy : IShippingStrategy
        {
        }
     
        public class TruckShippingStrategy : IShippingStrategy
        {
        }
     
        public class TrainShippingStrategy : IShippingStrategy
        {
        }

    So, what have we gained?  I'll claim three advantages over the switch statements:

    1. The code is easier to maintain.  If I need to change the logic for air shipping I only need to go to one place instead of three or more, the AirShippingStrategy class.  The code is easier to read because I'm only looking at the code for air shipping instead of seeing it in the middle of a large switch statement.  The code that calls the new strategy pattern is potentially much easier to read.  The big switch statement can be replaced with a simple call to _currentShippingStrategy.DetermineExpectedArrivalTime(order).  This should make the workflow in the shipping controller code easier to see because its no longer getting bogged down in the specifics of the shipping type.
    2. It's easier to add new types of shipping.  Instead of modifying code at all the places with a switch statement on shipping type, we just need to do is create a new implementation of IShippingStrategy to add support for new types of shipping to our supply chain automation system.  This is a very common example of the Open/Closed Principle.  With the Strategy pattern in place we can extend the system with new types of shipping without modifying the code that uses the IShippingStrategy interface.  Otherwise, we would have to alter a lot more classes.  More on this topic from David Hayden here.
    3. I'd argue that testing is easier because you can test the specific air, ground, or truck shipping in isolation by just creating the Strategy class instances and exercising the methods.  Without a strategy pattern you would have to test the specific air shipping logic through the calling code that contains the switch statement, creating a more difficult unit test to write and debug.

    We have a case like this in our big legacy document messaging system.  The system behaves differently based on what type of document is submitted to the system.  Unfortunately, the code uses repeated switch statements.  This is making it harder for us to map out the differences between the document types because the logical splits are scattered all over the code.  It's put us into a state where we're not very confident about our ability to add new document types, which is exactly what we need to do for the business this year.  We will be moving the variation in functionality to a separate handler class for each type of document -- even if it's the last thing I do, at least at work.

    The Strategy pattern is very similar to the State pattern, to the point where I abandoned my first idea for an example of a Strategy pattern because I couldn't decide if it was a strategy or state pattern.  I really wouldn't waste a lot of energy arguing whether a group of classes are implementing a State or a Strategy pattern.  I think the State pattern is very useful in many cases and often overlooked.  I'll definitely post on some example usages of the State pattern someday.

    Other examples:

    • NHibernate has an abstract class named Dialect that represents a database engine's particular SQL syntax (because every database's SQL is every so slightly different).  There is a separate subclass for each type of database engine that is supported by NHibernate (Oracle, MySql, Sql Server, DB2, etc).  New database types can be supported by creating a new subclass of Dialect.
    • Last week I used the ICellHandler interface inside of FitNesse to create a specific handler for DateTime properties inside of FitNesse tests to parse "(today)" and "(today+3)" as valid dates to effectively test date sensitive business rules.  Some day in the near future I'm going to create another implementation to handle .Net enumerations.  That's always aggravated me, until I finally realized that FitNesse is easy to extend (not that the documentation in FitNesse makes this obvious).

    Okay Jeremy, exactly how do you get the different Strategy implementation inside your code?  I distinctly remember getting a question like that from an old Cobol developer several years ago when I was explaining polymorphism and its advantages to him.  On the off chance he's out there somewhere, one of the more common and simpler possibilities is the Factory pattern.

    Factory Method

    "Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. "

    Last year we had to write several "translation" classes to map flat files to an xml format.  One easy way to do this is to create an abstraction that represents the mapping from a flat file column to the xml structure.

        public interface INodeMapping
        {
            void MapColumn(string textValue, XmlNode targetNode);
        }
     
        // Simple implementation of INodeMapping
        public class BasicNodeMapping : INodeMapping
        {
            private readonly string _nodeName;
     
            public BasicNodeMapping(string nodeName)
            {
                _nodeName = nodeName;
            }
     
            public void MapColumn(string textValue, XmlNode targetNode)
            {
                XmlElement newNode = targetNode.OwnerDocument.CreateElement(_nodeName);
                newNode.InnerText = textValue;
                targetNode.AppendChild(newNode);
            }
        }

    The actual translation classes aren't much more that an aggregation of INodeMapping objects.  The superclass for all of the translation classes has an abstract Factory Method (buildMappings() in the code below) to build the specific array of INodeMapping objects for the concrete instance.  I think you can say that the Factory Method is a specific type of Template Method.  Something to keep in mind with patterns is that they often occur in clusters and even in the same class.  Please don't waste calories arguing or worrying over exactly which pattern a class exhibits.

        public abstract class FlatFileToXmlTranslator
        {
            // Factory Method
            protected abstract INodeMapping[] buildMappings();
        }
     
        public class ReallyWeirdFlatFileFormatTranslator : FlatFileToXmlTranslator
        {
            // Factory method
            protected override INodeMapping[] buildMappings()
            {
                return new INodeMapping[] {
                    new BasicNodeMapping("Name"),
                    new BasicNodeMapping("Address"),
                    new BasicNodeMapping("City")
                    };
            }
        }

    Many people, including myself, think of a Factory Method as something different than the GoF definition.  "Factory Method" often describes a class or a method that returns the proper Strategy or State class based on some sort of input.  Going back to the shipping Strategy Pattern example from above, you might use something like the following method to select the proper IShippingStrategy implementation for a given ShippingType.

            // Yes Gary, there are *some* perfectly valid usages for static members
            public static IShippingStrategy SelectShippingStrategy(ShippingType shippingType)
            {    
                switch (shippingType)
                {
                    case ShippingType.Air:
                        return new AirShippingStrategy();
     
                    case ShippingType.Truck:
                        return new TruckShippingStrategy();
     
                    case ShippingType.Train:
                        return new TrainShippingStrategy();
     
                    default:
                        throw new ArgumentOutOfRangeException("shippingType", shippingType, "Unrecognized shipping type!");
                }
            }

    Abstract Factory

    "Provide an interface for creating families of related or dependent objects without specifying their concrete classes."

    The main point in talking about the Abstract Factory is to differentiate it from the Factory Method.  Think of the Abstract Factory as a way to run an application or a piece of code in multiple modes. An easy and common example is the creation of a data access layer to support multiple database engines. When you're coding against Sql Server you'll be using the SqlConnection, SqlCommand, and SqlDataAdapter classes.  If you're using a real database like Oracle you'll be performing the exact same actions with OracleConnection, OracleCommand, and OracleDataAdapter classes.  What you don't want to do in this situation is to create a lot of duplicated if/then logic like this:

        public class DataAccessHelper
        {
            private string _connectionString;
            private DatabaseEngineType _databaseEngineType;
     
            public DataAccessHelper(string connectionString, DatabaseEngineType databaseEngineType)
            {
                _connectionString = connectionString;
                _databaseEngineType = databaseEngineType;
            }
     
            // Don't do it this way
            public int ExecuteSql(string sql)
            {
                IDbConnection connection = null;
                IDbCommand command = null;
     
                if (_databaseEngineType == DatabaseEngineType.SqlServer)
                {
                    connection = new SqlConnection();
                    command = new SqlCommand();
                }
                else
                {
                    connection = new OracleConnection();
                    command = new OracleCommand();
                }
     
                // execute the sql with the connection and command objects
            }
     
            // Don't do it this way
            public IDataReader GetReader(string sql)
            {
                IDbConnection connection = null;
                IDbCommand command = null;
     
                if (_databaseEngineType == DatabaseEngineType.SqlServer)
                {
                    connection = new SqlConnection();
                    command = new SqlCommand();
                }
                else
                {
                    connection = new OracleConnection();
                    command = new OracleCommand();
                }
     
                // execute the sql with the connection and command objects
            }

    Plus, what if you want to add support later on for DB2 or MySql or PostgresSQL?  One of the consistent themes of design patterns is to eliminate if/then statements and this is no exception.  Here is an actual example of an Abstract Factory class we use in our data access library in a couple of our projects (I might release this as part of StructureMap as an alternative to the data access block in the EntLib someday, but don't hold your breath).

    namespace StructureMap.DataAccess
    {
        [PluginFamily("MSSQL")]
        public interface IDatabaseEngine
        {
            IDbConnection GetConnection();
            IDbCommand GetCommand();
            IDbCommand CreateStoredProcedureCommand(string commandText);
            IDbDataAdapter GetDataAdapter();
     
     
            IDataParameter CreateStringParameter(string parameterName, int size, bool isNullable);
            IDataParameter CreateParameter(string parameterName, DbType dbType, bool isNullable);
            string GetParameterName(string logicalName);
        }
    }

    The Facade (more on this later) class for the data access helper has a dependency on an IDatabaseEngine that is pushed in through constructor injection. 

            [DefaultConstructor]
            public DataSession(IDatabaseEngine database)
                : this(database, 
                       new CommandFactory(database),
                       new AutoCommitExecutionState(database.GetConnection(), database.GetDataAdapter()),
                       new TransactionalExecutionState(database.GetConnection(), database.GetDataAdapter()))
            {
     
            }

    Anytime the other classes in the data access layer need an IDbConnection or an IDbCommand they create it through an instance of the IDatabaseEngine interface.  Since we're primarily a Sql Server shop, the only concrete implementation of IDatabaseEngine is MSSQLDatabaseEngine (partial implementation below), but we could easily use the data access layer against Oracle by creating a different implementation of the IDatabaseEngine interface for Oracle and inject it into the DataSession class.

        [Pluggable("MSSQL")]
        public class MSSQLDatabaseEngine : IDatabaseEngine
        {
            private readonly string _connectionString;
     
            [DefaultConstructor]
            public MSSQLDatabaseEngine(IConnectionStringProvider provider)
            {
                _connectionString = provider.GetConnectionString();
            }
     
            public MSSQLDatabaseEngine(string connectionString)
            {
                _connectionString = connectionString;
            }
     
            public IDbConnection GetConnection()
            {
                return new SqlConnection(_connectionString);
            }
     
            public IDbCommand GetCommand()
            {
                return new SqlCommand();
            }
     
            public IDbDataAdapter GetDataAdapter()
            {
                return new SqlDataAdapter();
            }
     
            
        }

    Here's another example.  Occasionally-connected Smart Clients are a hot topic at the moment.  Say you have a new Smart Client that communicates with three different web services.

        public interface IService1{}
        public interface IService2{}
        public interface IService3{}

    The Smart Client is obviously going to behave very differently based on whether or not it is able to connect to the network.  The Abstract Factory might be a very good tool in this case.  Make both a local and remote implementation for each of the three services. 

        public class LocalService1 : IService1{}
        public class LocalService2 : IService2{}
        public class LocalService3 : IService3{}
     
        public class RemoteService1 : IService1{}
        public class RemoteService2 : IService2{}
        public class RemoteService3 : IService3{}

    Now, make all other classes get an instance of the three services through an Abstract Factory with this signature.

        public interface IServiceFactory
        {
            IService1 GetService1();
            IService2 GetService2();
            IService3 GetService3();
        }

    Next, build two implementations of the IServiceFactory interface.

        public class LocalServiceFactory : IServiceFactory
        {
            public IService1 GetService1()
            {
                return new LocalService1();
            }
     
            public IService2 GetService2()
            {
                return new LocalService2();
            }
     
            public IService3 GetService3()
            {
                return new LocalService3();
            }
        }

     The Smart Client can now completely switch its connection mode by replacing the instance of IServiceFactory between the connected and disconnected modes.

    Command

    "Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue of log requests, and support undoable operations"

    Boiling it down, you encapsulate a complex action or transaction into a single class that has a void Execute() method to kick it off.  The command pattern is simple to use, but it has some powerful uses. 

    • Transactions boundaries.  What if you have a long running operation that potentially run several optional database operations that need to run in the same transactions. You really don't want to keep a database connection open for all the time that you're evaluating the gating logic on top of the time it takes to do the actual database manipulation.  One answer is to use similar to the Fowler's Unit of Work pattern where you first you determine what actions to take in the form of an array of command patterns that have some sort of DoOperations(connection, transaction) method that performs its data access commands against the connection and transaction objects that are passed into it.   I've always been very leery of declarative transactions for some reason and minus distributed transactions I don't think declarative transactions are a necessity.
    • Reliable workflow.  Several years ago I got to do a lot of prototyping and brainstorming for a very large manufacturing workflow application that was going to manipulate and interface with a slew of unreliable physical processes and networks.  Wrapping operations inside of command classes that could be persisted can give you a way to queue up operations and create generalized mechanisms to retry operations that fail with network failures.
    • In many cases using a Command pattern can be a great way to break complex code into smaller chunks that are easier to unit test.  Say you have a case where the code is going to do a complex operation with a lot of input, that also requires a lot of logic to decide when to do this complex action.  You end up with one set of tests where you test the command objects, and another set of classes that are solely responsible for creating and configuration of the command classes.

    Here's a typical example of using the Command pattern inside of a user interface to do "CTRL-Z undo's" of user actions.  Let's define an interface for that exposes both "do" and "undo" operations for a user interface that's built around a TreeView motif.

        public interface IClientCommand
        {
            void Do();
            void UnDo();
        }

    Now, create an implementation of IClientCommand that removes a child object from a parent object and makes the corresponding change to the TreeView on the screen.  This RemoveChildCommand class will also have ReDo() functionality that will also put the child and child TreeNode back.

        public class RemoveChildCommand : IClientCommand
        {
            private readonly TreeNode _childNode;
            private TreeNode _parentNode;
            private readonly ParentClass _parent;
            private readonly ChildClass _child;
     
            public RemoveChildCommand(TreeNode node, ParentClass parent, ChildClass child)
            {
                _childNode = node;
                _parent = parent;
                _child = child;
            }
     
            public void Do()
            {
                // remove the TreeNode
                _parentNode = _childNode.Parent;
                _childNode.Remove();
     
                // set the Child field of the ParentClass to null
                _parent.RemoveChild(_child);
            }
     
            public void UnDo()
            {
                // add the Child back to the ParentClass
                _parent.AddChild(_child);
     
                // put the TreeNode back
                _parentNode.Nodes.Add(_childNode);
            }
        }

    And create a class that will maintain and manage the history of the IClientCommand's (apologies for doing this in v1.1 sans generics).  The top menu of the UI application would delegate to the UnDoLast() and ReDoNext() methods.

        public class CommandHistory
        {
            private Stack _pastCommands = new Stack();
            private Stack _futureCommands = new Stack();
     
            public void ExecuteCommand(IClientCommand command)
            {
                command.Do();
                _pastCommands.Push(command);
            }
     
            public void UnDoLast()
            {
                IClientCommand command = (IClientCommand) _pastCommands.Pop();
                command.UnDo();
                _futureCommands.Push(command);
            }
     
            public void ReDoNext()
            {
                IClientCommand command = (IClientCommand) _futureCommands.Pop();
                ExecuteCommand(command);
            }
        }

    Facade

    "Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use. "

    The facade is one of the best tools to ensure loose coupling between subsystems by reducing the exposed surface area between subsystems.  It's exactly what it sounds like, an attractive covering over a complex structure.  Think of a mechanical watch face.  On the outside you see a circle with numbers, an hour hand, and a minute hand.  It's a pretty simple user interface to use to tell time, but looking behind the watch face you'll see a complex multitude of gears and springs that do the actual work of telling time. A facade class is the watch face.

    Last year my colleague and I built a custom rules engine component that was used inside a much larger messaging system.  Internally, the rules component uses several granular services to:

    1. Translate an industry standard xml message to the internal "canonical" format that the rules engine uses
    2. Load specific information for both the sender and recipient
    3. Locate and build the rule objects for a given recipient and sender
    4. Run the rules against the canonical data model
    5. Process and coordinate any actions that result from the rules, including transaction management

    For a variety of reasons, all of these services are coded separately. That's fine and great for the team building the rules component, but it was potentially confusing for the other team that was consuming the rules component. The simple solution was to create a Facade class to shield the larger application and application team from the internal complexities of the rules component.  This Facade class simply took in an xml document, the id's of the sender and receiver, and returned an object array of validation errors.

    Just because the Facade is in place doesn't mean that you can't use the granular services behind the Facade, and we've done just that in writing custom testing harnesses.  Theoretically the rules component was built to be used in a different product.  If that ever happens, we'll probably just create a different Facade appropriate to the new application.

    More about the Facade pattern at the original Wiki