Beyond Ordinary Extension

The ordinary way to extend a software system is to find a suitable superclass—usually Object—and to subclass it. You can also “inherit” behavior from more than one class by copying some of the operations of a second class and delegating calls to an instance of that class. Both of these extension techniques, however, require that you know at compile time what behaviors you want to add. If you need to add behavior to an object without changing its class, you may be able to apply the Template Method (Chapter 21), Command (Chapter 24), Decorator (Chapter 27), or Visitor (Chapter 29) design patterns.

Ordinary extension also adds behavior to individual instances of a class. You may need to add behaviors that apply to a collection ...

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.