O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Practical Object-Oriented Design: An Agile Primer Using Ruby, Second Edition

Book Description

The Complete Guide to Writing Maintainable, Manageable, Pleasing, and Powerful Object-Oriented Applications 


Object-oriented programming languages have a point of view about how best to model the world. They exist to help you create beautiful, straightforward applications that are easy to change and simple to extend. Unfortunately, the world is awash with object-oriented (OO) applications that are difficult to understand and expensive to change. Even though they’re written in OO languages, these applications fail to achieve the promise of OO because their code doesn’t reflect the right mindset. The purpose of Practical Object-Oriented Design, Second Edition, is to solve that problem by immersing you in an OO mindset. It teaches powerful, real-world, object-oriented design techniques using simple and practical examples. It will change the way you think about code.


Sandi Metz has distilled a lifetime of conversations and presentations about object-oriented design into a proven set of OO practices for crafting manageable, extensible, and pleasing code. She demonstrates how to build new applications that can “survive success” and repair existing applications that have become impossible to change. Each technique is illustrated with extended examples in the easy-to-understand Ruby programming language, all downloadable from the companion Web site, poodr.com. Fully updated for Ruby 2.5, this guide shows how to:

  • Decide what belongs in a single class
  • Avoid entangling objects that should be kept separate
  • Define flexible interfaces among objects
  • Reduce programming overhead costs with duck typing
  • Successfully apply inheritance
  • Build objects via composition
  • Design cost-effective tests
  • Solve common problems associated with poorly designed object-oriented code

Whatever your previous object-oriented experience, this concise guide will help you achieve the superior outcomes you’re looking for.

Table of Contents

  1. Cover Page
  2. Title Page
  3. Table of Contents
  4. Contents
  5. Introduction
    1. Who Might Find This Book Useful?
    2. How to Read This Book
    3. How to Use This Book
    4. Software Versions Used In This Book
  6. About the Author
  7. 1. Object-Oriented Design
    1. 1.1. In Praise of Design
    2. 1.2. The Tools of Design
    3. 1.3. The Act of Design
    4. 1.4. A Brief Introduction to Object-Oriented Programming
    5. 1.5. Summary
  8. 2. Designing Classes with a Single Responsibility
    1. 2.1. Deciding What Belongs in a Class
    2. 2.2. Creating Classes That Have a Single Responsibility
    3. 2.3. Writing Code That Embraces Change
    4. 2.4. Finally, the Real Wheel
    5. 2.5. Summary
  9. 3. Managing Dependencies
    1. 3.1. Understanding Dependencies
    2. 3.2. Writing Loosely Coupled Code
    3. 3.3. Managing Dependency Direction
    4. 3.4. Summary
  10. 4. Creating Flexible Interfaces
    1. 4.1. Understanding Interfaces
    2. 4.2. Defining Interfaces
    3. 4.3. Finding the Public Interface
    4. 4.4. Writing Code That Puts Its Best (Inter)Face Forward
    5. 4.5. The Law of Demeter
    6. 4.6. Summary
  11. 5. Reducing Costs with Duck Typing
    1. 5.1. Understanding Duck Typing
    2. 5.2. Writing Code That Relies on Ducks
    3. 5.3. Conquering a Fear of Duck Typing
    4. 5.4. Summary
  12. 6. Acquiring Behavior Through Inheritance
    1. 6.1. Understanding Classical Inheritance
    2. 6.2. Recognizing Where to Use Inheritance
    3. 6.3. Misapplying Inheritance
    4. 6.4. Finding the Abstraction
    5. 6.5. Managing Coupling Between Superclasses and Subclasses
    6. 6.6. Summary
  13. 7. Sharing Role Behavior with Modules
    1. 7.1. Understanding Roles
    2. 7.2. Writing Inheritable Code
    3. 7.3. Summary
  14. 8. Combining Objects with Composition
    1. 8.1. Composing a Bicycle of Parts
    2. 8.2. Composing the Parts Object
    3. 8.3. Manufacturing Parts
    4. 8.4. The Composed Bicycle
    5. 8.5. Deciding Between Inheritance and Composition
    6. 8.6. Summary
  15. 9. Designing Cost-Effective Tests
    1. 9.1. Intentional Testing
    2. 9.2. Testing Incoming Messages
    3. 9.3. Testing Private Methods
    4. 9.4. Testing Outgoing Messages
    5. 9.5. Testing Duck Types
    6. 9.6. Testing Inherited Code
    7. 9.7. Summary
  16. Afterword