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