© Edward Sciore 2019
Edward ScioreJava Program Designhttps://doi.org/10.1007/978-1-4842-4143-1_8

8. Decorators

Edward Sciore1 
(1)
Newton, MA, USA
 

A decorator is a wrapper that implements the same interface as the object it wraps. Decorator methods enhance the methods of the wrapped object, in contrast to adapter methods, which replace the methods of the wrapped object.

This chapter examines several useful applications of decorators. They can be used to create immutable versions of objects, coordinate the execution of complex tasks, and implement collection streams. The Java library class InputStream uses decorators prominently. The chapter also examines the design issues that the writers of decorator classes must confront.

Decorator Classes

Let’s begin ...

Get Java Program Design: Principles, Polymorphism, and Patterns now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.