Surviving change

The Decorator Pattern, AngularJS 
and You

Superheroic

with great power comes great responsibility


Over time, will our software become rickety ...


Or will it stand the test of time ...

 

Build for change ...

Picking "the right tool for the job."



What's your toolbox look like ? 

Or

 

Bad tool choices lead to software that can't change

like furniture held together 

by stripped screws ...

The decorator pattern

what is it?


a way 

to change or extend 

the behaviour of a service


without modifying the original service

The decorator pattern

why use it? 

because

  • simpler services (single responsibility principle)

  • simpler tests

  • loose coupling / separating concerns

  • more confidence (open/closed principle)

The decorator pattern

how to use it?

  1. Register the decorator with the 
    $injector using the  $provide service:
  2. Define the decorator:

    The decorator should return the
    decorated service instance.

Use case 1: extending $log

TimeStamp$logDecorator 

 

Use case 2: extending $resource

Url$resourceDecorator


Other use cases:

  • chaining decorators (TimeStamp$logDecorator decorates Publishing$logDecorator decorates $log)

  • CachingWidgetsDecorator

  • overriding/extending services or directives from external modules

?


links

The Decorator Pattern

By Joel Skrepnek