Tuesday, 19 April 2016

Decorator pattern - Structural


  • Add additional responsibilities dynamically to an object. 
  • Add new functionality to an existing object without altering its structure
  • creates a decorator class which wraps the original class and provides additional functionality
  • Also known as Wrapper

Where to use ?
  • To add new function to an object without affecting other objects
  • To make a responsibility easily added and removed dynamically.


Implementation example
  • Interface Shape implemented by multiple concrete classes : Circle, Rectangle, Square
  • Client uses concrete classes.

      After introducing decorator :
  • Create another interface ShapeDecorator extending Shape interface
  • Create concrete class RedShapeDecorator implementing ShapeDecorator
  • Client can use decorator class to get additional behavior



No comments:

Post a Comment

Note: only a member of this blog may post a comment.