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

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

Iterator and Composite Patterns

We’ll begin with the Iterator pattern. In the words of the GoF book, Iterators “provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” If you’ve used the Java Collections classes, you’ve almost certainly used an Iterator. In fact, Java’s Iterators are an instantiation of the Iterator Design Pattern. They’re external iterators, to be precise.

After that, we’ll look at the Composite pattern. If you’ve used PowerPoint (or almost any vector drawing program), you might have seen that you can draw some boxes and lines, select them, and then put them into a group. Then, you can draw more boxes and lines and put them into a group too. But you can also put groups in a group, along with boxes and lines. This can get arbitrarily complex: you can have groups of groups of boxes and lines and more groups, which also have boxes and lines and groups in them. Now, if you’re developing a program like PowerPoint, you’ll probably have objects representing boxes, lines, and groups… and this is an ideal situation for applying the Composite pattern.

Iterator

Dive in Head First

  1. Start by reading the first part of Chapter 9 from Head First Design Patterns:

    Chapter 9. The Iterator and Composite Patterns: Well-Managed Collections

    Stop when you get to the section titled, “Is the Waitress ready for prime time?”

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

    Object Behavioral: Iterator

Test Your Knowledge

  1. True or False: The Iterator pattern gives the responsibility of traversing elements to the aggregate object.

  2. What is the difference between an external and an internal iterator?
    Answer

  3. What is the relationship between the Iterator pattern and the Single Responsibility Principle?
    Answer

Composite

Dive In Head First

  1. Start by reading the rest of Chapter 9 from Head First Design Patterns:

    Chapter 9. The Iterator and Composite Patterns: Well-Managed Collections

    Start from the section titled, “Is the Waitress ready for prime time?

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

    Object Structural: Composite

Test Your Knowledge

  1. A paragraph consists of one or more words. Suppose you have Paragraph and Word classes, so Words are part of a Paragraph. Does it make sense to use the Composite pattern?
    Answer

  2. The Head First reading shows how to implement an Iterator that traverses a composite. It does so using what data structure?
    Answer

Extend Your Knowledge

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

For more information on the Composite pattern, see these resources on Safari.

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

Comments are closed.