A pattern is a careful description of a perennial
solution to a recurring problem within a
building context, describing one of the
configurations that brings life to a building.
Slide 12
Slide 12 text
Ralph Johnson, Richard Helm, Erich Gamma, John Vlissides
Slide 13
Slide 13 text
Program to an interface,
not an implementation.
Gang of Four 1995:18
Slide 14
Slide 14 text
Favor object composition
over class inheritance.
Gang of Four 1995:20
Slide 15
Slide 15 text
Strategy
Slide 16
Slide 16 text
Strategy
Slide 17
Slide 17 text
Strategy
Slide 18
Slide 18 text
Strategy
class Status
attr_accessor :publisher
def publish
publisher.publish(status)
end
end
Slide 19
Slide 19 text
Strategy
class Status
attr_accessor :publisher
def publish
publisher.publish(status)
end
end
class TwitterPublisher
def publish(status)
# ...
end
end
class FacebookPublisher
def publish(status)
# ...
end
end
Slide 20
Slide 20 text
Strategy
class Status
attr_accessor :publisher
def publish
publisher.publish(status)
end
end
class TwitterPublisher
def publish(status)
# ...
end
end
class FacebookPublisher
def publish(status)
# ...
end
end
status = Status.new
status.publisher = TwitterPublisher.new
status.publish
Slide 21
Slide 21 text
Decorator
Slide 22
Slide 22 text
Decorator
Slide 23
Slide 23 text
Decorator
Slide 24
Slide 24 text
Decorator
class Post
def publish
# ...
end
end
Slide 25
Slide 25 text
Decorator
class Post
def publish
# ...
end
end
class EmailNotifier < SimpleDelegator
def publish
super && notify!
end
private
def notify!
# ...
end
end
Slide 26
Slide 26 text
Decorator
class Post
def publish
# ...
end
end
class EmailNotifier < SimpleDelegator
def publish
super && notify!
end
private
def notify!
# ...
end
end
post = Post.new
notifiable_post = EmailNotifier.new(post)
notifiable_post.publish
Slide 27
Slide 27 text
Command
Slide 28
Slide 28 text
Command
Slide 29
Slide 29 text
Command
class CreateProducts < ActiveRecord::Migration
def up
create_table :products do |t|
t.string :name
t.text :description
t.timestamps
end
end
def down
drop_table :products
end
end