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 ...

Get Design Patterns Java™ Workbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.