Your SlideShare is downloading. ×
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Design Patterns in Ruby (2014)
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Design Patterns in Ruby (2014)

468

Published on

Slides from my 2014 talk on design patterns in Ruby. This is a 1/2 hour talk that covers the basics of design patterns, what they are, why you should bother with them as well as the dangers of over …

Slides from my 2014 talk on design patterns in Ruby. This is a 1/2 hour talk that covers the basics of design patterns, what they are, why you should bother with them as well as the dangers of over applying them. Examples in Ruby.

Published in: Software, Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
468
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
15
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. @russolsen Design Patterns in Ruby 1
  • 2. 2 FORTRAN BASIC Pascal Python C++ C Java Ruby Clojure Go
  • 3. 3
  • 4. 4
  • 5. Design Patterns What are they? Some Examples The Good & Bad 5
  • 6. What is a Design Pattern? 6
  • 7. 7
  • 8. Prepackaged Solution Common Problem 8
  • 9. 9 Wide Boulevard
  • 10. 10 h Wide Boulevard
  • 11. 11 2hh Wide Boulevard
  • 12. Name: Wide Boulevard Problem: Open Feeling Solution: Width = 2 * Ht 12
  • 13. 13 Why Bother?
  • 14. 1) Prepackaged Solutions 14 Why Bother?
  • 15. 1) Prepackaged Solutions 2) Examples of Good Design 15 Why Bother?
  • 16. 1) Prepackaged Solutions 2) Examples of Good Design 3) Big Ideas ! 16 Why Bother?
  • 17. Some Examples 17
  • 18. Name: Template Method Problem: Solution: 18
  • 19. Name: Template Method Problem:Varying details Solution: 19
  • 20. Documents & Rendering 20 template*.rb
  • 21. 21 Template Method Document! def render render_title(title) text.each_line do|l| render_line(l) end ... PlainDocument def render_title(t) end ! def render_line(l) end HtmlDocument def render_title(t) end ! def render_line(l) end
  • 22. Name: Template Method Problem:Varying details Solution: Push the details into a subclass 22
  • 23. Big Ideas 1) Separate out change 23
  • 24. The Problem is ! 24
  • 25. The Problem is Commitment 25
  • 26. # I start off fine! doc1 = HtmlDocument.new(…) 26
  • 27. # I start off fine! doc1 = HtmlDocument.new(…) # But then I need to render doc1 in plain text… 27
  • 28. # I start off fine! doc1 = HtmlDocument.new(…) # But then I need to render doc1 in plain text… doc2 = PlainDocument.new(doc1.title, doc1.text) doc2.render 28
  • 29. Try Again 29 strategy.rb
  • 30. Strategy Pattern 30
  • 31. 31 Strategy Document def render @renderer.render(self) end HtmlRenderer def render(doc) puts “<title>” puts @doc.title puts “</title>” … end PlainRenderer def render(doc) puts doc.title ... end
  • 32. 32 Strategy Document def render @renderer.render(self) end HtmlRenderer def render(doc) puts “<title>” puts @doc.title puts “</title>” … end PlainRender def render(doc) puts doc.title ... end
  • 33. Name: Strategy Problem:Varying details Solution: Push the whole job into a separate object 33
  • 34. Big Ideas 1) Separate out change 2) Composition over inheritance 34
  • 35. A Different Problem 35 Obs*.rb
  • 36. 36 Observer Pattern Document def text=(t) @observers.each {…} end View def on_doc_change(doc) puts “<title>” puts @doc.title puts “</title>” … end Database def on_doc_change(doc) # Save the doc ... end
  • 37. Observer Pattern 37
  • 38. 38 Observer Pattern Document def text=(t) @observers.each {…} end
  • 39. 39 Black Boxes on_doc_change(doc) on_doc_change(doc)
  • 40. Big Ideas 1) Separate out change 2) Composition not inheritance 3) Interface not implementation 40
  • 41. Good Bad 41
  • 42. Good Orange Beatles 42
  • 43. 43
  • 44. 44
  • 45. Bad Rampaging Patterns 45
  • 46. 46 Strategy Document def render @renderer.render(self) end HtmlRenderer def render(doc) puts “<title>” puts @doc.title puts “</title>” … end PlainRender def render(doc) puts doc.title ... end
  • 47. 47 Renderer Document HtmlRenderer PlainRenderer Strategy + Template
  • 48. 48 Renderer Document HtmlRenderer PlainRenderer Observer Observer Observer Observer Strategy + Insanity
  • 49. Big Ideas 1) Separate out change 2) Composition not inheritance 3) Interface not implementation 4) Pain first, then the pattern 49
  • 50. Summary 50
  • 51. Prepackaged Solution Common Problem 51
  • 52. 52 Template Method Document! def render render_title(title) text.each_line do|l| render_line(l) end ... PlainDocument def render_title(t) end ! def render_line(l) end HtmlDocument def render_title(t) end ! def render_line(l) end
  • 53. 53 Strategy Document def render @renderer.render(self) end HtmlRenderer def render(doc) puts “<title>” puts @doc.title puts “</title>” … end PlainRender def render(doc) puts doc.title ... end
  • 54. 54 Observer Pattern Document def text=(t) @observers.each {…} end View def on_doc_change(doc) puts “<title>” puts @doc.title puts “</title>” … end Database def on_doc_change(doc) # Save the doc ... end
  • 55. Big Ideas 1) Separate out change 2) Composition not inheritance 3) Interface not implementation 4) Pain first, then the pattern 55
  • 56. Questions? 56
  • 57. russ@russolsen.com @russolsen ! cognitect.com 57

×