Design Patterns in Ruby

Post on 28-May-2015

781 views 4 download

Tags:

description

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

Transcript of Design Patterns in Ruby

Design Patterns in RubyAnil Wadghule Ruby hacker

Sunday, 7 July 13

What are Design Patterns?

Sunday, 7 July 13

What are Design Patterns?

Design Patterns are nothing but decent pre-loaded solutions to common problems

Sunday, 7 July 13

What are Design Patterns?

They are worth knowing because they are solutions to common problems

Sunday, 7 July 13

Are ‘Design Patterns’ restricted to certain programming languages?

NO! They are programming language independent.

They can be implemented in any programming languageMost OOP-specific design patterns may be irrelevant to functional languages

Sunday, 7 July 13

Design Patterns book by Gang of Four

It made Design Patterns popular

Sunday, 7 July 13

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

Design Patterns we will see are

Template Method

Strategy

Proxy

Composite

Adapter

Decorator

Sunday, 7 July 13

Template MethodDocument

def renderrender_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

Strategy

Document

def initialize(renderer) @renderer = rendererend

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

Strategy

Separate out the change

Composition NOT inheritance

Sunday, 7 July 13

Proxy

Document

def render(doc) puts doc.title puts “By #{doc.author}”end

ProxyDoc

def render if allowed?

@doc.render endend

client

Sunday, 7 July 13

Adapter

Document

def renderend

Adapter

def print @doc.renderend

client

Sunday, 7 July 13

CompositeDocument

def renderend

Composite

def render @subdocs.each do |s|

s.render endend

client

Document

def renderend

Document

def renderend

Sunday, 7 July 13

Decorator

Document

def renderend

Decorator

def to_japanese translate(@doc.paragraphs)end

def render @doc.render + “do awesome”end

client

Sunday, 7 July 13

Recommended Book

Sunday, 7 July 13

References

http://en.wikipedia.org/wiki/Software_design_pattern

Sunday, 7 July 13

Thanks!

Sunday, 7 July 13

Questions?

Sunday, 7 July 13

@anildigital on twitter

anildigital@gmail.com

Sunday, 7 July 13