Slide 1

Slide 1 text

Design patterns Sergey Nartimov Brainspec https://github.com/lest twitter: @just_lest

Slide 2

Slide 2 text

About • Rails, Rubinius, Elixir contributor • Software engineer at Brainspec Sergey Nartimov Brainspec https://github.com/lest twitter: @just_lest

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

$ wc -l app/models/**/*.rb | sort -nr 5450 total 595 app/models/order.rb 434 app/models/user.rb 326 app/models/variant.rb 314 app/models/product.rb 279 app/models/invoice.rb 248 app/models/cart.rb

Slide 6

Slide 6 text

$ wc -l app/models/**/*.rb | sort -nr 5450 total 595 app/models/order.rb 434 app/models/user.rb 326 app/models/variant.rb 314 app/models/product.rb 279 app/models/invoice.rb 248 app/models/cart.rb https://github.com/drhenner/ror_ecommerce

Slide 7

Slide 7 text

https://github.com/errbit/errbit

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

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

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

Martin Fowler

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

http://martinfowler.com/eaaCatalog/

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

Domain Model

Slide 38

Slide 38 text

Service Layer

Slide 39

Slide 39 text

https://github.com/errbit/errbit

Slide 40

Slide 40 text

https://github.com/errbit/errbit

Slide 41

Slide 41 text

Active Record

Slide 42

Slide 42 text

Data Mapper

Slide 43

Slide 43 text

DataMapper 2 FTW Dan Kubb Piotr Solnica

Slide 44

Slide 44 text

Thanks <3 Sergey Nartimov Brainspec https://github.com/lest twitter: @just_lest