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

Welcome to day two of our seven-day Design Patterns Series. Today, we’ll learn our first two patterns: Observer and Decorator. Open a free 10-day Safari trial account to access the series materials.

Observer and Decorator

Welcome back to Safari’s Design Patterns Series! Today, we’ll start an in-depth exploration of individual Design Patterns, studying two patterns each day until the end of the bootcamp. We’ll begin with the Observer and Decorator patterns.


Dive in Head First

  1. Start by reading chapter 2 from Head First Design Patterns:

    Chapter 2. The Observer Pattern: Keeping your Objects in the know

    If you’re not specifically interested in Java, you may want to skip the section “Using Java’s built-in Observer Pattern.”

  2. One of the most common places you’ll see the the Observer pattern used is in the implementation of graphical user interfaces. Take a look at the section “A Classic Example: Observer in GUIs” from either of Steven Metsker’s books:

    From Design Patterns in Java, 2nd Edition:
    9. Observer

    From Design Patterns in C#:
    9. Observer

  3. Once you’re comfortable with the Observer pattern, skim the original, authoritative description from Design Patterns: Elements of Reusable Object-Oriented Software:

    Object Behavioral: Observer

Test Your Knowledge

Did you get the gist of the Observer pattern? Take a stab at these questions.

  1. In general, how many observers can a subject have?
    Exactly one
    Zero or one
    Zero or more
    One or more

  2. True or False: One advantage of the Observer pattern is that subjects and observers are loosely coupled.

  3. In a small application, a single object may register itself as an observer to receive all the events in a GUI. However, larger applications tend to use a ___???___ design.


Dive in Head First

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

    Chapter 3. The Decorator Pattern: Decorating Objects

    If you’re not specifically interested in Java, you may want to just skim the sections “Writing your own Java I/O Decorator” and “Test out your new Java I/O Decorator,” but make sure you get the basic idea, because that’s a very typical use of the Decorator pattern. If C# is more your style, Steve Metsker describes the same idea in the context of C# in Design Patterns in C#:

    27. Decorator

  2. Skim the original description of the Decorator pattern from Design Patterns: Elements of Reusable Object-Oriented Software:

    Object Structural: Decorator

Test Your Knowledge

Obviously, the readings covered the details of the Decorator pattern, but they also covered two big ideas in object-oriented design. Did you get them?

  1. The Decorator pattern provides a flexible alternative to ___???___ for extending functionality.

  2. What is the open-closed principle?

Extend Your Knowledge

Safari has numerous resources on design patterns, covering a variety of programming languages. Here are some other resources where you can find explanations and examples of the Observer pattern:

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

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

Comments are closed.