Design Patterns in Ruby

1,327 views

Published on

I gave talk on Design Patterns in Ruby at 'Pune Ruby Users Group' meeting on 6th July, 2013. Here are the code snippets https://gist.github.com/anildigital/5939892

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,327
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
29
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Design Patterns in Ruby

  1. 1. Design Patterns in Ruby Anil Wadghule Ruby hacker Sunday, 7 July 13
  2. 2. What are Design Patterns? Sunday, 7 July 13
  3. 3. What are Design Patterns? Design Patterns are nothing but decent pre-loaded solutions to common problems Sunday, 7 July 13
  4. 4. What are Design Patterns? They are worth knowing because they are solutions to common problems Sunday, 7 July 13
  5. 5. Are ‘Design Patterns’ restricted to certain programming languages? NO! They are programming language independent. They can be implemented in any programming language Most OOP-specific design patterns may be irrelevant to functional languages Sunday, 7 July 13
  6. 6. Design Patterns book by Gang of Four It made Design Patterns popular Sunday, 7 July 13
  7. 7. Design Patterns categories Creational patterns (Singleton, Builder etc) Structural patterns (Adapter, Composite, Proxy etc) Behavioral patterns (Command, Strategy, Observer etc) Architectural patterns (MVC - Model View Controller etc) Concurrency patterns (Reactor, Thread pool etc) Sunday, 7 July 13
  8. 8. Design Patterns we will see are Template Method Strategy Proxy Composite Adapter Decorator Sunday, 7 July 13
  9. 9. Template Method Document def render render_title(@title) render_footer(@author) ... end HTML DocumentText Document def render_title(t) end def render_author(a) end def render_title(t) end def render_author(a) end Template Method Sunday, 7 July 13
  10. 10. Strategy Document def initialize(renderer) @renderer = renderer end def render @renderer.render(self) end HTMLRenderer TextRenderer def render(doc) puts doc.title puts “By #{doc.author}” end def render(doc) ... end Sunday, 7 July 13
  11. 11. Strategy Separate out the change Composition NOT inheritance Sunday, 7 July 13
  12. 12. Proxy Document def render(doc) puts doc.title puts “By #{doc.author}” end ProxyDoc def render if allowed? @doc.render end end client Sunday, 7 July 13
  13. 13. Adapter Document def render end Adapter def print @doc.render end client Sunday, 7 July 13
  14. 14. Composite Document def render end Composite def render @subdocs.each do |s| s.render end end client Document def render end Document def render end Sunday, 7 July 13
  15. 15. Decorator Document def render end Decorator def to_japanese translate(@doc.paragraphs) end def render @doc.render + “do awesome” end client Sunday, 7 July 13
  16. 16. Recommended Book Sunday, 7 July 13
  17. 17. References http://en.wikipedia.org/wiki/Software_design_pattern Sunday, 7 July 13
  18. 18. Thanks! Sunday, 7 July 13
  19. 19. Questions? Sunday, 7 July 13
  20. 20. @anildigital on twitter anildigital@gmail.com Sunday, 7 July 13

×