Decorator - C++ Patterns
← Patterns

Decorator

This pattern is licensed under the CC0 Public Domain Dedication.

Requires c++98 or newer.

Intent

Extend the functionality of a class.

Description

On lines 7–12, we define the class that we wish to decorate, foo_concrete, which implements the foo interface.

The foo_decorator class, on lines 14–29, also implements the foo interface. This decorator class wraps any other foo object, and forwarding any calls to the wrapped object. By adding additional code to foo_decorator::do_work (lines 21–25), we can extend the functionality of the wrapped object.

To demonstrate, we wrap a foo_concrete with a foo_decorator on lines 38–39, and pass it to the bar function on line 41, which takes a reference to any foo object and calls do_work on it. In this case, the call will be decorated by foo_decorator.

Contributors

  • Joseph Mansfield
  • Robbie Shade

Last Updated

09 December 2017

Source

Fork this pattern on GitHub

Share

Feel like contributing? This website is generated from a git repository. If you have something to add or have noticed a mistake, please fork the project on GitHub.

C++ Patterns created by Joseph Mansfield