Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Decorator Pattern

Avatar for Matt Steele Matt Steele
April 02, 2014
58

The Decorator Pattern

Avatar for Matt Steele

Matt Steele

April 02, 2014
Tweet

Transcript

  1. What is it? ❖ In object-oriented programming, the decorator pattern

    (also known as Wrapper, an alternative naming shared with the Adapter pattern) is a design pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class.! ❖ - Wikipedia
  2. Original public static class MacBook { public int cost() {

    return 997; } ! public double screenSize() { return 11.6; } }
  3. Nest 1 public static class Memory extends MacBook { private

    MacBook device; ! public Memory(MacBook device) { this.device = device; } @Override public int cost() { return device.cost() + 75; } }
  4. Nest 2 public static class Engraving extends MacBook { private

    MacBook device; ! public Engraving(MacBook device) { this.device = device; } @Override public int cost() { return device.cost() + 200; } }
  5. Nest 3 public static class Insurance extends MacBook { private

    MacBook device; ! public Insurance(MacBook device) { this.device = device; } ! @Override public int cost() { return device.cost() + 250; } }
  6. Running public static void main(String[] args) { MacBook book =

    new MacBook(); ! MacBook withFeatures = new Insurance(new Engraving(new Memory(book))); System.out.println(withFeatures.cost()); }
  7. JavaScript function MacBook() { this.cost = function () { return

    997; }; this.screenSize = function () { return 11.6; }; }
  8. Nest 1 function memory( macbook ) { var v =

    macbook.cost(); macbook.cost = function() { return v + 75; }; }
  9. Nest 2 function engraving( macbook ){ var v = macbook.cost();

    macbook.cost = function() { return v + 200; }; }
  10. Nest 3 function insurance( macbook ){ var v = macbook.cost();

    macbook.cost = function() { return v + 250; }; }
  11. Running var mb = new MacBook(); memory( mb ); engraving(

    mb ); insurance( mb ); ! console.log(mb.cost());
  12. Summary ❖ Open-Closed Principle ❖ Decorator Pattern: Alternative to Inheritance

    ❖ Decorators mirror type ❖ Adds new functionality around (or in place of) base
  13. Summary ❖ Open-Closed Principle ❖ Decorator Pattern: Alternative to Inheritance

    ❖ Decorators mirror type ❖ Adds new functionality around (or in place of) base ❖ Wrap with 0 or more decorators
  14. Summary ❖ Open-Closed Principle ❖ Decorator Pattern: Alternative to Inheritance

    ❖ Decorators mirror type ❖ Adds new functionality around (or in place of) base ❖ Wrap with 0 or more decorators ❖ Transparent, should be orderless
  15. Summary ❖ Open-Closed Principle ❖ Decorator Pattern: Alternative to Inheritance

    ❖ Decorators mirror type ❖ Adds new functionality around (or in place of) base ❖ Wrap with 0 or more decorators ❖ Transparent, should be orderless ❖ Make sure you’re logging usage well