Slide 1

Slide 1 text

The Decorator Pattern Matt Steele

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Give it to me straight, nerd.

Slide 4

Slide 4 text

–Me, right now “The Decorator Pattern lets you implement the Open Closed Principle”

Slide 5

Slide 5 text

https://flic.kr/p/awnCQu The Open-Closed Principle Open to Extension, Closed to Modification

Slide 6

Slide 6 text

Version 1: Inheritance

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

https://flic.kr/p/aYVKBx Version 2: Decorator

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

New Behavior, New Decorator

Slide 11

Slide 11 text

https://flic.kr/p/9tT9Yh Nest ‘em

Slide 12

Slide 12 text

Original public static class MacBook { public int cost() { return 997; } ! public double screenSize() { return 11.6; } }

Slide 13

Slide 13 text

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; } }

Slide 14

Slide 14 text

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; } }

Slide 15

Slide 15 text

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; } }

Slide 16

Slide 16 text

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()); }

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Nesting https://flic.kr/p/4PTTW5

Slide 19

Slide 19 text

Isn’t Inheritance evil?

Slide 20

Slide 20 text

- Me, again “Types, not behavior”

Slide 21

Slide 21 text

JavaScript function MacBook() { this.cost = function () { return 997; }; this.screenSize = function () { return 11.6; }; }

Slide 22

Slide 22 text

Nest 1 function memory( macbook ) { var v = macbook.cost(); macbook.cost = function() { return v + 75; }; }

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Running var mb = new MacBook(); memory( mb ); engraving( mb ); insurance( mb ); ! console.log(mb.cost());

Slide 26

Slide 26 text

https://flic.kr/p/H6Q6w Nesting

Slide 27

Slide 27 text

Uses

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Logging

Slide 30

Slide 30 text

Logging Performance

Slide 31

Slide 31 text

Logging Performance Security

Slide 32

Slide 32 text

Logging Performance Security Transactions

Slide 33

Slide 33 text

Logging Performance Security Transactions (anything AOP can do)

Slide 34

Slide 34 text

Logging Performance Security Transactions (anything AOP can do)

Slide 35

Slide 35 text

Example: Spring JMS http://docs.spring.io/spring/docs/3.2.8.RELEASE/javadoc-api/org/springframework/jms/connection/package-frame.html

Slide 36

Slide 36 text

Summary

Slide 37

Slide 37 text

Summary ❖ Open-Closed Principle

Slide 38

Slide 38 text

Summary ❖ Open-Closed Principle ❖ Decorator Pattern: Alternative to Inheritance

Slide 39

Slide 39 text

Summary ❖ Open-Closed Principle ❖ Decorator Pattern: Alternative to Inheritance ❖ Decorators mirror type

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

https://flic.kr/p/4ryZqa