Friday, January 07, 2005

Patterns Everywhere: The Strategy Pattern


I'm getting ready for the Microsoft Business Framework Advisory Council meeting at the beginning of February and the 141 page Microsoft Business Framework Layperson's Specification. After that I have a series of lab exercises to do that create an application using the latest build of the emerging product. Can't talk about the details as they are still confidential. However, support for patterns comes up now and then and I'm forced to review the Gang of Four (GoF) patterns book. Being too lazy to get up and go across the hall to the bookshelf, I "ask the Internet" as Marvin Minsky said at a recent MIT meeting, giving the distinct impression that the Internet was simply a remote extension of his already oversize brain.

AL4, the lightweb web services frontend to Amazon, quickly gives me the GoF book. I click on the Amazon link hoping to be able to "search inside" but alas, real search is still a work in progress an Amazon. However, Google is the answer to virtually all questions and it quickly gives me a flood of links to papers on the Strategy Pattern. Not only that, when I go to Google Scholar, I get 704,000 links to research papers that mention design patterns with the GoF book at the top of the list. And I can immediately access 6254 publications that reference the GoF book. No wonder Google is getting sued for this! It's too useful to be legal.

Suffice it to say patterns are everywhere! Here is a good review of the Strategy Pattern:


Applying Strategy Pattern in C++ Applications
By T. Kulathu Sarma

Software consulting companies do projects for their customers on a "Fixed Price basis" or on a "Time and Material basis". Also, the projects can be either onsite or offsite. Usually, the customers specify how they want the project to be done (Fixed price or Time and Material basis, onsite or offsite). The ultimate aim of the consulting company is to complete the project in the scheduled time, however the Strategy (or the policy) they adapt in doing the project may differ, depending on how they do the project. This is a real life example, where a Strategy Pattern is applied.

Strategy Pattern can also be used in the software design. When it is possible to have several different algorithms for performing a process, each of which is the best solution depending on the situation, then a Strategy Pattern can be used. This article is all about Strategy Pattern. It uses a programming example to explain what, when and why a Strategy Pattern is needed. Benefits and drawbacks of using Strategy Pattern in software design is discussed. Three different approaches for implementing the Pattern in C++ and known uses of Strategy Pattern are also presented in this article.

Design Patterns are meant to provide a common vocabulary for communicating design principles. Strategy Pattern is classified under Behavioral Patterns in the book, Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma et al. (Addison-Wesley, 1995). In this article, I will be using the terms used by 'Gang of Four (GoF)' to explain Strategy Pattern...

So, what is a Strategy Pattern? The Strategy Pattern is a design pattern to encapsulate the variants (algorithms) and swap them strategically to alter system behavior without changing its architecture. According to GoF, Strategy Pattern is intended to define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

0 Comments:

Post a Comment

<< Home