Design Patterns in Ruby

14
Dev-day Design Patterns in Ruby Tiago Lima 23 Nov 2012

description

Show two differents types of patterns (Template Method and Strategy). This presentations was built to be done with a dojo.

Transcript of Design Patterns in Ruby

Page 1: Design Patterns in Ruby

Dev-day

Design Patterns in Ruby

Tiago Lima23 Nov 2012

Page 2: Design Patterns in Ruby

How it starts (The Gang of Four)

• It’s hard sometimes identify that you’re just building a wheel

• Four guys: Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides

• Design Patterns: Elements of Reusable Object-Oriented Software

2

Page 3: Design Patterns in Ruby

Keys-questions

• How do objects like the ones you tend to find in most systems relate to one other?

• How should they be coupled together?• What should they know about each

other?• How can we swap out parts that are

likely to change frequently?

3

Page 4: Design Patterns in Ruby

Principles

• Separate out the things that change from those that stay the same

• Program to interface, not an implementation

• Prefer composition over inheritance• Delegate, delegate, delegate• My favorite: You ain’t gonna need it

4

Page 5: Design Patterns in Ruby

Design Patterns

• Template Method• Strategy

5

Page 6: Design Patterns in Ruby

Template Method

6

Page 7: Design Patterns in Ruby

The theory

• Only one BIG tip

7

Page 8: Design Patterns in Ruby

Let’s do it

• The Report class• Adding a new type of report, in plain

text (do it in the most simple way)• Let’s refactor and apply the template

method (separate the things that stay the same)

8

Page 9: Design Patterns in Ruby

Strategy

9

Page 10: Design Patterns in Ruby

The theory

• Delegate, delegate, delegate• Isolate all changing code to a

separated class

10

Page 11: Design Patterns in Ruby

Let’s do it

• Using again, The Report class• The drawback of inheritance• Applying the Strategy pattern

11

Page 12: Design Patterns in Ruby

Let’s do it better

• Duck typing• Proc and block

12

Page 13: Design Patterns in Ruby

References

• Design Pattern in Ruby by Russ Olsen

13

Page 14: Design Patterns in Ruby

I appreciate your time.

14

Tiago Lima