O'Reilly logo

Design Patterns Java™ Workbook by Steven John Metsker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Type-Safe Collections

When you pass a collection of objects to a client, you will often imply that the contents of the collection are instances of a certain type. For example, if you have a static method getPromotionalFireworks() that returns an instance of List, the implication is that the list contains instances of the Firework class. You can strengthen this type of contract by creating a FireworkList class that guarantees that its contents are instances of Firework. Figure 28.1 shows this class.

Figure 28.1. The FireworkList class provides type-specific lists for Firework objects. The Itr class is an inner class for FireworkList.

The FireworkList ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required