Home > Assignments > Recipe for Success: Implementing the Strategy Pattern

Recipe for Success: Implementing the Strategy Pattern

There are a few Design Principles surrounding the Strategy Pattern:

  1. Identify the aspects of your application that vary and separate them from what stays the same, i.e. take the parts that vary and encapsulate them, so that later you can alter or extend the parts that vary without affecting those that don’t.
  2. Favor composition over inheritance.
  3. Program to an interface, not an implementation.

You should keep these in mind when implementing the Strategy Pattern in your software.

Here are the basic steps for implementing the Strategy Pattern:

  1. Within your class hierarchy, decide which methods vary depending on the subclass. There will probably be more than one method, so select all of them and remove them from that class (should be removing them from the base/super class). All that should be left within your base/super class should be constants, or methods that do not vary depending on the subclass. This is the idea of encapsulation, you’re taking what changes and separating it from what is constant.
  2. Now, for each of your methods that you removed create a new hierarchy, either with an abstract class or interface (preferably interface). Your new hierarchies should contain only one or two methods within the base class or interface.
  3. Now that you have several new hierarchies, you need to create subclasses for all of them. These subclasses should implement the interface (or extend the base class), and will be concrete classes (i.e. you will instantiate them). Each subclass varies by its own definition of its inherited method. Once you are finished, you should have a few new hierarchies with several subclasses each.
  4. With your new hierarchies complete, it’s time to return to your original class hierarchy (where you removed the methods). Within the base/super class of this hierarchy, you need to add several new member variables. These member variables should be types corresponding to your new hierarchies. You will need to declare a level of protection (private, protected, public) for these member variables. If you choose to make them private, you will need to add new methods to your base/super class to set them, or you could make them protected and allow your subclasses to directly modify them. Either way works fine for our purposes.
  5. Now you need to edit the constructor code for all of your subclasses. Within each constructor, set the appropriate member variables to the correct subclasses from each of your new hierarchies. You will need to instantiate new objects of the correct subclass (from your new hierarchies). By correct I don’t mean there’s a right or wrong way of doing this, I mean that if you have, for example, an object of type Bully, you would set his member variable of type Attitude to “Mean,” (correct) you wouldn’t set it to “Happy” (incorrect), unless for some reason you wanted a Bully to have a Happy attitude (but that is beside the point). You have several member variables that need to be set accordingly for each of your subclasses (of your original class hierarchy), so you will pick and choose 1 subclass from your new hierarchies to set to each of the member variables.
  6. If you haven’t already, it might be nice to add set methods to the base class of your original hierarchy so you can change at runtime the member variables for each of your subclasses. Since the member variables of your subclasses are actually objects, it’s very simple to change them at runtime; all it takes is a simple “bully.setBehavior(new Happy());” line of code.

This pattern is very nice as it exemplifies encapsulation very well as well as code reuse and delegation. The encapsulation is obvious, and your new hierarchies may now be used for anything that is given an appropriate member variable. As for delegation, because you separated what changes from what stays constant, you can give your removed methods to other programmers to design the hierarchies for, and all they need to give you is the interface or abstract class that they used for the hierarchy so you can add them as member variables to your original hierarchy.

Categories: Assignments Tags: ,
  1. Wei Chen
    January 29, 2008 at 2:29 am

    Hi Nate,

    Your answer is great, keep on this.


  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: