Design Patterns in Ruby

Post on 30-Nov-2014

652 views 1 download

Tags:

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

Dev-day

Design Patterns in Ruby

Tiago Lima23 Nov 2012

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

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

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

Design Patterns

• Template Method• Strategy

5

Template Method

6

The theory

• Only one BIG tip

7

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

Strategy

9

The theory

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

separated class

10

Let’s do it

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

11

Let’s do it better

• Duck typing• Proc and block

12

References

• Design Pattern in Ruby by Russ Olsen

13

I appreciate your time.

14

Tiago Lima