From the course: Java EE: Design Patterns and Architecture

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

The Decorator design pattern

The Decorator design pattern

From the course: Java EE: Design Patterns and Architecture

Start my 1-month free trial

The Decorator design pattern

- [Instructor] The Decorator Pattern is one of the structural patterns described in the Gang of Four design pattern book. It's purpose is to wrap a target object, so that you can dynamically add new responsibilities at runtime. Each decorator can wrap another one, which allows for a theoretically unlimited number of decorating of target objects. The Decorator Pattern is often use dynamically to add behavior to objects at runtime, or when it is not possible, or advisable, to use sub classing. A good example are data streams. They are often decorated in this way. The java.io.BufferedInputStream is an example of a decorator wrapping a lower level API and adding functionality to buffer and input stream. Although this runtime behavior is much more flexible than inheritance by sub classing, it introduces a level of complexity to concrete sub classing as it makes it more difficult to determine the types and behaviors of objects prior to executing the application. The decorator implementation…

Contents