Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

“Each pattern describes a problem that occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.”

Slide 7

Slide 7 text

Context Forces Solution

Slide 8

Slide 8 text

Context: An object’s behaviour needs to be extended. The new behaviour only applies to some instances. Forces Solution

Slide 9

Slide 9 text

Context: An object’s behaviour needs to be extended. The new behaviour only applies to some instances. Forces: Addition of feature(s) should be invisible to client code. Base class should not know about additional behaviour. There are multiple independent extensions. Solution

Slide 10

Slide 10 text

Solution: Wrap the object in a decorator. Implement or override feature-specific methods. Pass other messages to the wrapped object. Context: An object’s behaviour needs to be extended. The new behaviour only applies to some instances. Forces: Addition of feature(s) should be invisible to client code. Base class should not know about additional behaviour. There are multiple independent extensions.

Slide 11

Slide 11 text

UML ahead

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

class Coffee def ingredients [:beans, :water] end ! def price BigDecimal("2.00") end end

Slide 16

Slide 16 text

class LargeCoffee < Coffee def price super * BigDecimal("1.2") end end

Slide 17

Slide 17 text

class WhiteCoffee < Coffee def ingredients super + [:milk] end end

Slide 18

Slide 18 text

class LargeWhiteCoffee < Coffee def price super * BigDecimal("1.2") end ! def ingredients super + [:milk] end end

Slide 19

Slide 19 text

drink = LargeWhiteCoffee.new ! drink.ingredients # => [:beans, :water, :milk] ! "%.2f" % drink.price # => "2.40"

Slide 20

Slide 20 text

class LargeDrink def initialize drink @drink = drink end ! def ingredients @drink.ingredients end ! def price @drink.price * BigDecimal("1.2") end end

Slide 21

Slide 21 text

class MilkDrink def initialize drink @drink = drink end ! def ingredients @drink.ingredients + [:milk] end ! def price @drink.price end end

Slide 22

Slide 22 text

drink = MilkDrink.new( LargeDrink.new(Coffee.new) ) ! drink.ingredients # => [:beans, :water, :milk] ! "%.2f" % drink.price # => "2.40"

Slide 23

Slide 23 text

drink = Coffee.new |> LargeDrink.new |> MilkDrink.new Meanwhile, in a parallel universe…

Slide 24

Slide 24 text

class MilkDrink def initialize drink @drink = drink end ! def ingredients @drink.ingredients + [:milk] end ! def price @drink.price end end

Slide 25

Slide 25 text

class MilkDrink def initialize drink @drink = drink end ! def ingredients @drink.ingredients + [:milk] end ! def price @drink.price end end

Slide 26

Slide 26 text

class MilkDrink def initialize drink @drink = drink end ! def ingredients @drink.ingredients + [:milk] end ! def method_missing *args, &block @drink.send(*args, &block) end end

Slide 27

Slide 27 text

drink = MilkDrink.new( LargeDrink.new(Coffee.new) ) ! drink.ingredients # => [:beans, :water, :milk] ! "%.2f" % drink.price # => "2.40"

Slide 28

Slide 28 text

class Decorator def initialize obj @obj = obj end ! attr_reader :obj ! def method_missing *args, &block obj.send(*args, &block) end end

Slide 29

Slide 29 text

class LargeDrink < Decorator def price obj.price * BigDecimal("1.2") end end ! class MilkDrink < Decorator def ingredients obj.ingredients + [:milk] end end

Slide 30

Slide 30 text

drink = MilkDrink.new( LargeDrink.new(Coffee.new) ) ! drink.ingredients # => [:beans, :water, :milk] ! "%.2f" % drink.price # => "2.40"

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

require "delegate" ! class LargeDrink < SimpleDelegator def price super * BigDecimal("1.2") end end ! class MilkDrink < SimpleDelegator def ingredients super + [:milk] end end

Slide 34

Slide 34 text

drink = MilkDrink.new( LargeDrink.new(Coffee.new) ) ! drink.ingredients # => [:beans, :water, :milk] ! "%.2f" % drink.price # => "2.40"

Slide 35

Slide 35 text

module LargeDrink def price super * BigDecimal("1.2") end end ! module MilkDrink def ingredients super + [:milk] end end

Slide 36

Slide 36 text

drink = Coffee.new drink.extend LargeDrink drink.extend MilkDrink ! drink.ingredients # => [:beans, :water, :milk] ! "%.2f" % drink.price # => "2.40"

Slide 37

Slide 37 text

In the wild

Slide 38

Slide 38 text

FileInputStream f = new FileInputStream("foo.txt"); ! BufferedInputStream buf = new BufferedInputStream(f);

Slide 39

Slide 39 text

Not to be confused with…

Slide 40

Slide 40 text

An adapter changes
 a class’s interface. A decorator changes
 its behaviour.

Slide 41

Slide 41 text

A composite wraps
 multiple objects. A decorator
 only wraps one.

Slide 42

Slide 42 text

A proxy acts as a temporary stand-in. A decorator augments another object.

Slide 43

Slide 43 text

A façade presents a
 reduced interface. A decorator preserves the object’s interface.

Slide 44

Slide 44 text

https://www.flickr.com/photos/tinou/453593446 https://www.flickr.com/photos/15016964@N02/5917784319 https://www.flickr.com/photos/snowpeak/8516781169 https://en.wikipedia.org/wiki/File:Christopher_Alexander.jpg https://www.flickr.com/photos/txmx-2/6145817075 https://www.flickr.com/photos/johannahobbs/473289801 https://www.flickr.com/photos/sabeth718/14816607620

Slide 45

Slide 45 text

No content