Free Shipping Anywhere in the World

Shop over 1 Million Toys in our Huge New Range

Design Patterns in Ruby


Product Description
Product Details

Table of Contents

Foreword xviiPreface xixAcknowledgments xxvAbout the Author xxviiPART I: Patterns and Ruby 1 Chapter 1: Building Better Programs with Patterns 3

The Gang of Four 4 Patterns for Patterns 4 Separate Out the Things That Change from Those That Stay the Same 5Program to an Interface, Not an Implementation 5Prefer Composition over Inheritance 7 Delegate, Delegate, Delegate 12You Ain't Gonna Need It 13Fourteen Out of Twenty-Three 15 Patterns in Ruby? 17

Chapter 2: Getting Started with Ruby 19

Interactive Ruby 20Saying Hello World 20Variables 23Fixnums and Bignums 24Floats 26There Are No Primitives Here 26But Sometimes There Is No Object 27Truth, Lies, and nil 28Decisions, Decisions 30Loops 32More about Strings 34Symbols 37Arrays 38Hashes 40Regular Expressions 40A Class of Your Own 41Getting at the Instance Variables 43An Object Asks: Who Am I? 46Inheritance, Subclasses, and Superclasses 46Argument Options 47Modules 49Exceptions 52Threads 53Managing Separate Source Files 54Wrapping Up 55

PART II: Patterns in Ruby 57 Chapter 3: Varying the Algorithm with the Template Method 59

Keeping Up with What Life Throws at You 60Separate the Things That Stay the Same 61Discovering the Template Method Pattern 65Hook Methods 66But Where Are All the Declarations? 68Types, Safety, and Flexibility 69Unit Tests Are Not Optional 71Using and Abusing the Template Method Pattern 73Templates in the Wild 74Wrapping Up 75

Chapter 4: Replacing the Algorithm with the Strategy 77

Delegate, Delegate, and Delegate Again 78Sharing Data between the Context and the Strategy 80Duck Typing Yet Again 82Procs and Blocks 84Quick-and-Dirty Strategies 88Using and Abusing the Strategy Pattern 90The Strategy Pattern in the Wild 90Wrapping Up 92

Chapter 5: Keeping Up with the Times with the Observer 95

Staying Informed 95A Better Way to Stay Informed 97Factoring Out the Observable Support 100Code Blocks as Observers 104Variations on the Observer Pattern 105Using and Abusing the Observer Pattern 106Observers in the Wild 108Wrapping Up 109

Chapter 6: Assembling the Whole from the Parts with the Composite 111

The Whole and the Parts 112Creating Composites 114Sprucing Up the Composite with Operators 118An Array as a Composite? 119An Inconvenient Difference 120Pointers This Way and That 120Using and Abusing the Composite Pattern 122Composites in the Wild 123Wrapping Up 125

Chapter 7: Reaching into a Collection with the Iterator 127

External Iterators 127Internal Iterators 130Internal Iterators versus External Iterators 131The Inimitable Enumerable 133Using and Abusing the Iterator Pattern 134Iterators in the Wild 136Wrapping Up 140

Chapter 8: Getting Things Done with Commands 143

An Explosion of Subclasses 144An Easier Way 145Code Blocks as Commands 147Commands That Record 148Being Undone by a Command 151Queuing Up Commands 154Using and Abusing the Command Pattern 154The Command Pattern in the Wild 155ActiveRecord Migrations 155Madeleine 156Wrapping Up 160

Chapter 9: Filling in the Gaps with the Adapter 163

Software Adapters 164The Near Misses 167An Adaptive Alternative? 168Modifying a Single Instance 170Adapt or Modify? 172Using and Abusing the Adapter Pattern 173Adapters in the Wild 173Wrapping Up 174

Chapter 10: Getting in Front of Your Object with a Proxy 175

Proxies to the Rescue 176The Protection Proxy 178Remote Proxies 179Virtual Proxies Make You Lazy 180Eliminating That Proxy Drudgery 182Message Passing and Methods 183The method_missing Method 184Sending Messages 185Proxies without the Tears 185Using and Abusing Proxies 189Proxies in the Wild 190Wrapping Up 192

Chapter 11: Improving Your Objects with a Decorator 193

Decorators: The Cure for Ugly Code 193Formal Decoration 200Easing the Delegation Blues 200Dynamic Alternatives to the Decorator Pattern 201Wrapping Methods 202Decorating with Modules 202Using and Abusing the Decorator Pattern 204Decorators in the Wild 205Wrapping Up 206

Chapter 12: Making Sure There Is Only One with the Singleton 207

One Object, Global Access 207Class Variables and Methods 208Class Variables 208Class Methods 209A First Try at a Ruby Singleton 211Managing the Single Instance 212Making Sure There Is Only One 213The Singleton Module 214Lazy and Eager Singletons 214Alternatives to the Classic Singleton 215Global Variables as Singletons 215Classes as Singletons 216Modules as Singletons 218A Safety Harness or a Straitjacket? 219Using and Abusing the Singleton Pattern 220They Are Really Just Global Variables, Right? 220Just How Many of These Singletons Do You Have? 221Singletons on a Need-to-Know Basis 221Curing the Testing Blues 223Singletons in the Wild 224Wrapping Up 225

Chapter 13: Picking the Right Class with a Factory 227

A Different Kind of Duck Typing 228The Template Method Strikes Again 231Parameterized Factory Methods 233Classes Are Just Objects, Too 236Bad News: Your Program Hits the Big Time 237Bundles of Object Creation 239Classes Are Just Objects (Again) 241Leveraging the Name 242Using and Abusing the Factory Patterns 244Factory Patterns in the Wild 244Wrapping Up 246

Chapter 14: Easier Object Construction with the Builder 249

Building Computers 250Polymorphic Builders 253Builders Can Ensure Sane Objects 256Reusable Builders 257Better Builders with Magic Methods 258Using and Abusing the Builder Pattern 259Builders in the Wild 259Wrapping Up 260

Chapter 15: Assembling Your System with the Interpreter 263

The Right Language for the Job 264Building an Interpreter 264A File-Finding Interpreter 267Finding All the Files 267Finding Files by Name 268Big Files and Writable Files 269More Complex Searches with Not, And, and Or 270Creating the AST 272A Simple Parser 272A Parser-less Interpreter? 274Let XML or YAML Do the Parsing? 276Racc for More Complex Parsers 277Let Ruby Do the Parsing? 277Using and Abusing the Interpreter Pattern 277Interpreters in the Wild 278Wrapping Up 279

PART III: Patterns for Ruby 281 Chapter 16: Opening Up Your System with Domain-Specific Languages 283

The Domain of Specific Languages 283A File Backup DSL 284It's a Data File--No, It's a Program! 285Building PackRat 287Pulling Our DSL Together 288Taking Stock of PackRat 289Improving PackRat 290Using and Abusing Internal DSLs 293Internal DSLs in the Wild 294Wrapping Up 295

Chapter 17: Creating Custom Objects with Meta-programming 297

Custom-Tailored Objects, Method by Method 298Custom Objects, Module by Module 300Conjuring Up Brand-New Methods 301An Object's Gaze Turns Inward 306Using and Abusing Meta-programming 306Meta-programming in the Wild 308Wrapping Up 311

Chapter 18: Convention Over Configuration 313

A Good User Interface--for Developers 315Anticipate Needs 315Let Them Say It Once 316Provide a Template 316A Message Gateway 317Picking an Adapter 319Loading the Classes 320Adding Some Security 323Getting the User Started 325Taking Stock of the Message Gateway 326Using and Abusing the Convention Over Configuration Pattern 327Convention Over Configuration in the Wild 328Wrapping Up 328

Chapter 19: Conclusion 331 Appendix A: Getting Hold of Ruby 333Appendix B: Digging Deeper 335 Index 339

Promotional Information

Praise for Design Patterns in Ruby"Design Patterns in Ruby documents smart ways to resolve many problems that Ruby developers commonly encounter. Russ Olsen has done a great job of selecting classic patterns and augmenting these with newer patterns that have special relevance for Ruby. He clearly explains each idea, making a wealth of experience available to Ruby developers for their own daily work."-Steve Metsker, Managing Consultant with Dominion Digital, Inc."This book provides a great demonstration of the key 'Gang of Four' design patterns without resorting to overly technical explanations. Written in a precise, yet almost informal style, this book covers enough ground that even those without prior exposure to design patterns will soon feel confident applying them using Ruby. Olsen has done a great job to make a book about a classically 'dry' subject into such an engaging and even occasionally humorous read."-Peter Cooper"This book renewed my interest in understanding patterns after a decade of good intentions. Russ picked the most useful patterns for Ruby and introduced them in a straightforward and logical manner, going beyond the GoF's patterns. This book has improved my use of Ruby, and encouraged me to blow off the dust covering the GoF book."-Mike Stok"Design Patterns in Ruby is a great way for programmers from statically typed objectoriented languages to learn how design patterns appear in a more dynamic, flexible language like Ruby."-Rob Sanheim, Ruby Ninja, RelevanceMost design pattern books are based on C++ and Java. But Ruby is different-and the language's unique qualities make design patterns easier to implement and use. In this book, Russ Olsen demonstrates how to combine Ruby's power and elegance with patterns, and write more sophisticated, effective software with far fewer lines of code.After reviewing the history, concepts, and goals of design patterns, Olsen offers a quick tour of the Ruby language-enough to allow any experienced software developer to immediately utilize patterns with Ruby. The book especially calls attention to Ruby features that simplify the use of patterns, including dynamic typing, code closures, and "mixins" for easier code reuse.Fourteen of the classic "Gang of Four" patterns are considered from the Ruby point of view, explaining what problems each pattern solves, discussing whether traditional implementations make sense in the Ruby environment, and introducing Ruby-specific improvements. You'll discover opportunities to implement patterns in just one or two lines of code, instead of the endlessly repeated boilerplate that conventional languages often require.Design Patterns in Ruby also identifies innovative new patterns that have emerged from the Ruby community. These include ways to create custom objects with metaprogramming, as well as the ambitious Rails-based "Convention Over Configuration" pattern, designed to help integrate entire applications and frameworks.Engaging, practical, and accessible, Design Patterns in Ruby will help you build better software while making your Ruby programming experience more rewarding.

About the Author

Russ Olsen has been building software for more than twenty-five years. He has led projects through several generations of programming technologies, from FORTRAN to C to C++ to Java, and now Ruby. He has been using and teaching Ruby since 2002. Olsen writes the popular technology blog Technology As If People Mattered (

Ask a Question About this Product More...
Write your question below:
Look for similar items by category
Home » Books » Computers » Programming » General
Item ships from and is sold by Fishpond World Ltd.
Back to top