In this talk from a Tampa 8th Light University, Senior Craftsman Cory Foy details the design patterns used in Rails, and shows their use and implementation while reference Fowler's PoEAA and Alexander's Timeless Way of Building
4. “The specific patterns out of which a building or a town
is made may be alive or dead. To the extent they are
alive, they let our inner forces loose, and set us free; but
when they are dead, they keep us locked in inner
conflict.”
Christopher Alexander - “The Timeless Way of Building”
Friday, June 21, 13
5. “And it turns out...behind all processes which allow us to
make buildings live, there is a single common process.
But though this method is precise, it cannot be used
mechanically.
Indeed it turns out, in the end, that what this method
does is simply free us from all method.”
Christopher Alexander - “The Timeless Way of Building”
Friday, June 21, 13
6. “...we have so far beset ourselves with rules, and
concepts, and ideas of what must be done to make a
building or a town alive, that we have become afraid of
what will happen naturally, and convinced that we must
work within a “system” and with “methods” since
without them our surroundings will come tumbling down
in chaos.”
Christopher Alexander - “The Timeless Way of Building”
Friday, June 21, 13
7. Naming something the name of a pattern does
not make it that pattern
Friday, June 21, 13
12. “From the beginning, the Rails framework
turned web development on its head with
the insight that the vast majority of time
spent on projects amounted to meaningless
sit-ups.
Instead of having the time to think your
domain-specific code, you’d spend the first
few weeks of a project deciding meaningless
details.”
- Yahuda Katz, The Rails 3 Way
Friday, June 21, 13
13. “[Rails is] not a blank slate equally tolerant
of every kind of expression. On the
contrary, it trades that flexibility for the
convenience of ‘what most people need
most of the time to do most things’”
- David Heinmeier Hansson -
The Rails 3 Way
Friday, June 21, 13
14. Not to be confused with Ruby
(programming language).
- Wikipedia
Friday, June 21, 13
15. Ruby on Rails, often simply Rails, is an open
source web application framework which
runs on the Ruby programming language. It
is a full-stack framework: it allows creating
pages and applications that gather
information from the web server, talk to or
query the database, and render templates
out of the box. As a result, Rails features a
routing system that is independent of the
web server.
Friday, June 21, 13
16. Ruby on Rails emphasizes the use of well-
known software engineering patterns and
principles, such as active record pattern,
convention over configuration (CoC), don't
repeat yourself (DRY), and model-view-
controller (MVC).
Friday, June 21, 13
21. Name Model-View Controller
Intent
Separate the presentation of the model
from the view, and separate the wiring
of the model to the view
Motivation
A web application which has many
models that need to be interacted with
by end users
Implementation
View, Model and Controller should be
in separate classes.View should depend
on model, but not vice versa
Consequences
May be overkill for simple systems
where the model has very little
behavior
Friday, June 21, 13
23. Name Front Controller
Intent
Consolidate request handling by
channeling requests through a single
handler object
Motivation
Centralize the logic and handling for the
many things that need to happen for a
complex web site
Implementation
Create a class to handle all calls for a
web site, and determines the correct
classes to dispatch the request to
Consequences
More complicated than a Page
Controller. Can turn into a God class
Friday, June 21, 13
25. Name Mapper
Intent
Set up communications between two
subsystems that need to stay ignorant
of each other
Motivation
Allow two independent systems to be able
to talk to each other without them being
dependent on each other or the mapper
Implementation
Initialize the mapper (maybe using a
Front Controller) and configure the
communication between systems
Consequences
May be difficult to initialize and
configure the mapper since the
subsystems can’t know about it
Friday, June 21, 13
27. Name Active Record
Intent
An object that wraps a row in a
database table, encapsulates the access,
and adds domain logic on that data
Motivation
Simplify the need to persist data by
putting the data access logic in the
domain object
Implementation
Each class is responsible for saving and
loading to the database, and for any
domain logic that acts on the data
Consequences
Best when domain logic isn’t too complex,
and when model corresponds directly to the
database table. Couples system to DB
Friday, June 21, 13
31. Name Single Table Inheritance
Intent
Represent an inheritance hierarchy of classes
as a single table that has columns for all of
the fields of the classes
Motivation
Minimize the number of joins when
representing an inheritance structure in
a relational database
Implementation
A single table contains all of the fields
for the subclasses. Classes may or may
not use all of the fields.
Consequences
Since not all columns are used, may be
confusing. May cause large tables. Only
have a single namespace for fields.
Friday, June 21, 13
33. Name TemplateView
Intent
Render information into HTML by
embedding markers in an HTML page
Motivation
Allow HTML pages to be created as if
they were static, but still be able to
insert dynamic data
Implementation
Embed placeholders into a static HTML
page when it’s written. Replace
placeholders with content at runtime.
Consequences
Easy to put complicated logic in the
view. Harder to test since generally
designed to work within a web server
Friday, June 21, 13