Posted on by & filed under Design Patterns, java, learning, Learning & Development, programming, Programming & Development, Tech.

Welcome to the conclusion of our seven-day Design Patterns Series. Today, we’ll conclude the series by learning the State pattern, reviewing the Strategy pattern, and pointing you to a number of other resources you can use to continue learning about Design Patterns. Open a free 10-day Safari trial account to access the series materials.

Did you know there’s an object-oriented way to implement state machines? More generally, sometimes objects can be in different “states” at different points in time — e.g., a file could be open or closed — and their behavior needs to change accordingly. Today, we’ll learn how to handle situations like this using the State pattern.

After that, we’ll look at the Strategy pattern. You already saw the Strategy pattern on Day 1 of this series, when you read chapter 1 of Head First Design Patterns. But now that you have a few more patterns under your belt, you might see it in a slightly different light.

As you learn more patterns, and as you gain experience applying them, you will begin to see relationships among the patterns. The same basic ideas recur, like preferring composition over inheritance and taking advantage of polymorphism. Some of the patterns will seem remarkably similar; you’ll notice that the biggest differences are not in their structure but in their intent — in what problem they are designed to solve. Revisiting the Strategy pattern now will give you a chance to compare and contrast it with some other patterns.

Although today concludes the Design Patterns Series, we hope you don’t stop learning about Design Patterns now! Safari has several fantastic resources on design patterns and many related concepts. After you finish revisiting the Strategy pattern, we’ll give you some ideas for what to do next.


Dive in Head First

  1. Begin with chapter 10 from Head First Design Patterns:

    Chapter 10. The State Pattern: The State of Things

    It makes a few references to the Strategy pattern, which was introduced back in chapter 1. If you’ve forgotten the details of Strategy, take a look at the Strategy readings suggested later in this post for a review.

  2. Once you’re comfortable with the State pattern, review the original description from the GoF book Design Patterns: Elements of Reusable Object-Oriented Software:

    Object Behavioral: State

Test Your Knowledge

When you’re ready, try to answer these questions.

  1. Who is responsible for transitioning between states: the ConcreteStates or the Context?

  2. True or False: Conditionals (if/else or switch statements) can sometimes be eliminated by replacing the conditional with a polymorphic method call.

  3. True or False: A class diagram can be used to provide a complete, concise description of a design pattern.


Start Somewhere Different

  1. Start by reading the Strategy chapter from one of James Cooper’s books. He opens the chapter by comparing the State pattern to the Strategy pattern:

    From Java Design Patterns: A Tutorial: 24. The Strategy Pattern, or

    From C# Design Patterns: A Tutorial: Chapter 29. The Strategy Pattern

  2. Next, read the first two paragraphs of this chapter from Russ Olsen’s Design Patterns in Ruby (if you don’t know Ruby, that’s OK; it’s only two paragraphs):

    Chapter 4. Replacing the Algorithm with the Strategy

    Stop at “Delegate, Delegate, and Delegate Again”

  3. Finally, skim the original description of the Strategy pattern from Design Patterns: Elements of Reusable Object-Oriented Software:

    Object Behavioral: Strategy

Test Your Knowledge

Got the Strategy pattern? Try to answer a few questions about the relationship between Strategy and some of the other patterns you’ve seen.

  1. True or False: Template Method uses inheritance; it lets subclasses override parts of an algorithm. Strategy uses composition; it uses objects to encapsulate entire algorithms, and it allows several alternative algorithms to be
    used, depending on context.

  2. What are the differences between the State and Strategy patterns?

  3. True or False: State objects are often Singletons.

Extend Your Knowledge

For more information on the State pattern, take a look at these other resources on Safari:

For more information on the Strategy pattern, take a look at these resources:


Now that you’ve graduated from the Patterns Series, what’s next? Don’t worry; there’s still a lot more to learn! Here are some ideas of what to do next.

  • Watch Doug Schmidt’s Design Patterns in Java LiveLessons (Video Training). These are highly recommended. Doug works through an expression tree case study, so these video lessons are not just a rehashing of what you’ve already read: they’re an opportunity to see design patterns being applied to solve nontrivial design problems.

  • Read a book on object-oriented analysis and design, to review or refine your knowledge of the principles underlying design patterns. If you liked Head First Design Patterns, then Head First Object-Oriented Analysis and Design has the same approachable style and sound technical content.

  • Read Martin Fowler’s Refactoring: Improving the Design of Existing Code, and work through William Wake’s Refactoring Workbook. There are “code smells” that suggest that you might need to change your object-oriented design… and refactoring is a disciplined method of doing so. Object-oriented design principles are abstract, which can make them difficult to learn. However, “code smells” are more concrete… and easier to identify. Learning code smells and how to eliminate them can be a great, concrete way to see object-oriented design principles in action.

  • Read Josh Kerievsky’s Refactoring to Patterns. From the back cover, “Joshua Kerievsky has changed our approach to design by forever uniting patterns with the evolutionary process of refactoring.”

  • Read a patterns book that focuses on something not related to object-oriented design. Jenifer Tidwell’s bestselling Designing Interfaces uses patterns to describe how to develop graphical user interfaces.

  • Finish reading the GoF book (which does, in fact, have an actual title: Design Patterns: Elements of Reusable Object-Oriented Software). (Re-)read Chapter 1, as well as Chapter 2. These introductory chapters will take on more meaning now that you’ve seen a few patterns and the object-oriented design principles underlying them. After that, read the descriptions of the 10 remaining patterns that we didn’t cover in the bootcamp.

  • Finish reading Head First Design Patterns, specifically, Chapter 11, Chapter 12, Chapter 13, and Appendix A.

  • Read a Design Patterns book that focuses on a different programming language. We’ve cited several of these as we went through the bootcamp. Of course, there are lots of great books covering Java and C#, but there are other excellent books like Pro Objective-C Design Patterns for iOS, Pro JavaScript Design Patterns, Learning Python Design Patterns, Learning PHP Design Patterns, and Design Patterns in Ruby.

  • Write code! This is probably the most important thing you can do. You can learn a lot by reading, but you’ll never become a design patterns expert unless you actually use design patterns. So, build software. Apply patterns. Make mistakes. Fix them. You’ve learned a lot over the last seven days. It’s time to put it into action.

Jeff Overbey is an Assistant Professor in the Department of Computer Science and Software Engineering at Auburn University, and also works as an independent software consultant. He received his Ph.D. in 2011 from the University of Illinois at Urbana-Champaign, where he studied under Ralph Johnson, one of the authors of the original Design Patterns book. Contact information is available from his web site.

Tags: design patterns, development, gang of four, java, Learning, programming,

2 Responses to “Design Patterns Series, Day 7: The State and Strategy Patterns”