After nudging by Joe Ocampo and Scott Bellware, I finally sat down at finished “Design Patterns in Ruby” by Russ Olsen. The format of most of the chapters made the book an interesting read:
- A introduction to why you might need the pattern
- A static language developer’s approach with the Ruby language
- A seasoned Ruby developer’s approach to the design pattern
- Using and Abusing
- In the wild
- Wrapping up
Some of the items that I learned [LosTechies is not a cult contrary to some of the examples you read below; some of the examples below are using LosTechies nomenclature but closely resemble what the author had in the book] If any of the stuff below intrigues you: GO BUY THE BOOK. You won’t regret it. Even if you are trying to understand patterns in another language. Russ Olsen does an excellent job explaining the INTENT of the patterns. FUN When teaching the reader about “Truth, Lies, and nil”, the author even pokes fun at himself:
| |
BOOLEAN In Ruby, zero, being neither false nor nil, evaluates to true in Boolean expression.
| |
will print out: Zero is true!
ARRAYS Points for matrix reference in array examples
| |
REGULAR EXPRESSIONS The flow of the language when creating regular expressions:
| |
ARBITRARY PARAMETERS Any author that uses DC comic character to explain arbitrary numbers of arguments, is a winner in my mind:
| |
DUCK TYPING AND UNIT TESTS He mentions duck typing and the fact that “Unit Tests Are Not Optional” is a section heading when teaching the Template Method Pattern.
PROCS AND BLOCKS
| |
STRATEGY PATTERN using proc-based formatters to create a ruby-based strategy pattern
| |
You could create any type of formatter you want in a proc instead creating new classes.
OBSERVER PATTERN Modules exist that encapsulate things that some of us static developers might already be used to:
| |
ITERATOR PATTERN Internal Iterators versus External Iterators: (had never heard it put this way) External iterator - client drives the iteration…you won’t call next until you are good and ready for the next element Internal iterator - the aggregate relentlessly pushes the code block to accept item after item.
COMMAND PATTERN The Command pattern translates very smoothly into code blocks. Here is a PabloForPresidentButton class reworked to use code blocks:
| |
The author does not diminish the needs for classes. For straightforward actions, use a Proc object. For complex object or objects that will carry around a lot of state, create a command class.
ADAPTER PATTERN Instead of adhering to some interface and trying to create your adapter, why not just extend the original class.
| |
Before any of you Open-Closed people attack, please re-read the definition of OCP - Open for extension, closed for modification. Doesn’t this adhere to that? :)
PROXY PATTERN There are three tyes of Proxies: The Protection Proxy, Remote Proxy, Virtual Proxy These are mentioned in the Gang of Four book. He introduces a Ruby-esqe way to approach proxies: the method_missing Method
| |
Will output: delegating deposit method to subject. delegating withdraw method to subject. delegating balance method to subject. account balance is now: 75
DECORATOR PATTERN
| |
SINGLETON PATTERN The author admits the career of the singleton has been checkered, but still shows that you can use it in the Ruby world. An example he gives us to allow testing of singleton implementation code is to put the implementation code in a base class and have the child be the singleton:
| |
FACTORY/ABSTRACT FACTORY PATTERN I never had it straight, exactly, what the difference between these patterns were (yes, besides name). I never bothered to look. According to the author, Factory returns back a single object while Abstract Factory is “an object dedicated to creating a compatible set of objects”. According to GoF book (which I have open in front of me), Abstract Factory “provides an interface for creating families of related or dependent objects without specifying their concreate classes”. Which one do you think would have turned the light bulb off in your head? :) The other item was using “Convention Over Configuration” to generate abstract factories:
| |
Comments Link to heading
Scott Bellware: Cheers!
Joe Ocampo: Your welcome, About the only thing I would critique is that from what I have observed from most frameworks in Ruby is that the parens are omitted when there is only one arg. It reads betters: # d = SimpleItem.new() # d.extend(Decorator1) # d.extend(Decorator2) to # d = SimpleItem.new # d.extend Decorator1 # d.extend Decorator2
Russ Olsen: Jason, Hey thanks for the kind words, I’m glad that you are finding the book useful. The funny thing about the factories patterns is a) that I wasn’t 100% clear on the difference between the two before I started researching the book and b) given Ruby’s dynamic nature, you really don’t find many of examples of the classic factories in real Ruby code. Oh, and in the interest of fairness I’m going to include some Marvel characters in my next book. Russ
Russ Olsen: Joe Ocampo wrote: > About the only thing I would critique is that from what I have > observed from most frameworks in Ruby is that the parens are > omitted when there is only one arg. I think there is a range of opinions regarding the parens, yes or no? question in the Ruby community. I am sure, however, that leaving the parens off is the surest way to confuse people who are newly arrived in Ruby from say Java. I can’t tell you the number of times that I have confused a room full of newly minted Ruby programmers because I left the parens off of some example. Put the parens back on and their faces light up. Russ