Roberto Nogueira  
BSd EE, MSd CE
Solution Integrator Experienced - Certified by Ericsson

eBook Design Patterns in Ruby

ebook image

About

Learn everything you need to about the subject of this eBook project.

Homepage

Topics

Table of Contents
[x] Foreword xvii
[x] Preface xix
[x] Acknowledgments xxv
[x] About the Author xxvii

PART I: Patterns and Ruby 1

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

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

PART II: Patterns in Ruby 57

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

Chapter 4: Replacing the Algorithm with the Strategy 77
[x] Delegate, Delegate, and Delegate Again 78
[x] Sharing Data between the Context and the Strategy 80
[x] Duck Typing Yet Again 82
[x] Procs and Blocks 84
[x] Quick-and-Dirty Strategies 88
[x] Using and Abusing the Strategy Pattern 90
[x] The Strategy Pattern in the Wild 90
[x] Wrapping Up 92

Chapter 5: Keeping Up with the Times with the Observer 95
[x] Staying Informed 95
[x] A Better Way to Stay Informed 97
[x] Factoring Out the Observable Support 100
[x] Code Blocks as Observers 104
[x] Variations on the Observer Pattern 105
[x] Using and Abusing the Observer Pattern 106
[x] Observers in the Wild 108
[x] Wrapping Up 109

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

Chapter 7: Reaching into a Collection with the Iterator 127
[ ] External Iterators 127
[ ] Internal Iterators 130
[ ] Internal Iterators versus External Iterators 131
[ ] The Inimitable Enumerable 133
[ ] Using and Abusing the Iterator Pattern 134
[ ] Iterators in the Wild 136
[ ] Wrapping Up 140

Chapter 8: Getting Things Done with Commands 143
[x] An Explosion of Subclasses 144
[x] An Easier Way 145
[x] Code Blocks as Commands 147
[x] Commands That Record 148
[x] Being Undone by a Command 151
[x] Queuing Up Commands 154
[x] Using and Abusing the Command Pattern 154
[x] The Command Pattern in the Wild 155
[x] ActiveRecord Migrations 155
[x] Madeleine 156
[x] Wrapping Up 160

Chapter 9: Filling in the Gaps with the Adapter 163
[ ] Software Adapters 164
[ ] The Near Misses 167
[ ] An Adaptive Alternative? 168
[ ] Modifying a Single Instance 170
[ ] Adapt or Modify? 172
[ ] Using and Abusing the Adapter Pattern 173
[ ] Adapters in the Wild 173
[ ] Wrapping Up 174

Chapter 10: Getting in Front of Your Object with a Proxy 175
[ ] Proxies to the Rescue 176
[ ] The Protection Proxy 178
[ ] Remote Proxies 179
[ ] Virtual Proxies Make You Lazy 180
[ ] Eliminating That Proxy Drudgery 182
[ ] Message Passing and Methods 183
[ ] The method_missing Method 184
[ ] Sending Messages 185
[ ] Proxies without the Tears 185
[ ] Using and Abusing Proxies 189
[ ] Proxies in the Wild 190
[ ] Wrapping Up 192

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

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

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

Chapter 14: Easier Object Construction with the Builder 249
[ ] Building Computers 250
[ ] Polymorphic Builders 253
[ ] Builders Can Ensure Sane Objects 256
[ ] Reusable Builders 257
[ ] Better Builders with Magic Methods 258
[ ] Using and Abusing the Builder Pattern 259
[ ] Builders in the Wild 259
[ ] Wrapping Up 260

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

PART III: Patterns for Ruby 281

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

Chapter 17: Creating Custom Objects with Meta-programming 297
[x] Custom-Tailored Objects, Method by Method 298
[x] Custom Objects, Module by Module 300
[x] Conjuring Up Brand-New Methods 301
[x] An Object's Gaze Turns Inward 306
[x] Using and Abusing Meta-programming 306
[x] Meta-programming in the Wild 308
[x] Wrapping Up 311

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

Chapter 19: Conclusion 331
[ ] Appendix A: Getting Hold of Ruby 333
[ ] Appendix B: Digging Deeper 335
[ ] Index 339
[ ] Preface
[ ] Download the preface
[ ] Foreword
[ ] Download the foreword
[ ] Index
[ ] Download the index