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

Welcome to day four of our seven-day Design Patterns Series. Today, we’ll be learning the Command and Adapter patterns. Open a free 10-day Safari trial account to access the series materials.

Command and Adapter

Today, we’ll be looking at the Command pattern. Essentially, you’ll build an object that encapsulates a method call. This allows you to make the behavior of an object configurable, and it also provides a clever way to support Undo operations.

Then, we’ll look at the Adapter pattern. Adapters are especially useful when you’re dealing with libraries or other third-party code that you can’t change. You’ll find that you have an object that does what you want, but it doesn’t have the interface that you need. This is where Adapters come into play: they present the interface a particular client expects, while delegating the real work to an “adaptee,” effectively allowing the client and the adaptee to work together even though their interfaces are incompatible.


Dive In Head First

  1. Start by reading Chapter 6 from Head First Design Patterns:

    Chapter 6. The Command Pattern: Encapsulating Invocation

  2. Then, skim the original description of the Command pattern in Design Patterns: Elements of Reusable Object-Oriented Software.

    Object Behavioral: Command

Test Your Knowledge

  1. The Head First reading snuck in a description of another pattern: the Null Object pattern. What is a Null Object, and why is it useful?

  2. How can the Command pattern be used to support undo?


Dive in Head First

  1. Start by reading the first half of Chapter 7 from Head First Design Patterns:

    Chapter 7. The Adapter and Facade Patterns: Being Adaptive
    Stop when you get to the section titled, “And now for something different…”

  2. Now, skim the original description of the Adapter pattern in Design Patterns: Elements of Reusable Object-Oriented Software:

    Class, Object Structural: Adapter

Test Your Knowledge

  1. What is the basic difference between an object adapter and a class adapter?

  2. Two days ago, we mentioned that a design with a large number of Singletons should raise some red flags. Is the same true for Adapters?

Extend Your Knowledge

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

Safari also has a number of great resources discussing the Adapter pattern:

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

Comments are closed.