CHAPTER 12The Decorator Pattern

In this chapter, we look at a way to add features to objects without creating new subclasses. The decorator pattern is used to transparently wrap objects within another object of the same interface. This allows you to add behavior to a method and then pass the method call on to the original object. Using decorator objects is a flexible alternative to creating subclasses. This pattern is well-suited to JavaScript (as you will see later in the chapter with dynamic interfaces) because typical JavaScript code does not rely heavily on the types of objects.

The Structure of the Decorator

A decorator allows you to add functionality to an object. This can be used in place of large numbers of subclasses. To illustrate ...

Get Pro JavaScript™ Design Patterns now with O’Reilly online learning.

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