Decorator Pattern

Decorator pattern a design pattern that allows behavior to be added to an individual object without affecting the behavior of other objects from the same class. Decorators provide a flexible alternative to subclass for extending functionality.

The decorator pattern is achieved by designing a new decorator class that wraps the original class, and providing additional functionality keeping class methods signature intact.
Decorator Pattern

The basic steps as follows:

  1. Subclass “Component” class into a “Decorator” class
  2. Pass a Component to the Decorator constructor
  3. In the ConcreteDecorator class, override Component method(s).

Here is one example to demonstrate Decorator pattern implementation and usage.
Car_Decorator

Create interface (car)

Concrete classes to implements the interface

Create abstract decorator class implementing the Car interface

Create concrete decorator class extending the CarDecorator class

Decorate Cars (upgrade)

Output

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on RedditShare on StumbleUponEmail this to someoneShare on TumblrDigg this

One thought on “Decorator Pattern

  1. Pingback: Software Design Patterns

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">