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, 2/e

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 how to repair those 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 website, 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
  2. Title Page
  3. Copyright Page
  4. Dedication
  5. Contents
  6. About This E-Book
  7. Introduction
  8. Acknowledgments
  9. About the Author
  10. 1 Object-Oriented Design
    1. 1.1 In Praise of Design
      1. 1.1.1 The Problem Design Solves
      2. 1.1.2 Why Change Is Hard
      3. 1.1.3 A Practical Definition of Design
    2. 1.2 The Tools of Design
      1. 1.2.1 Design Principles
      2. 1.2.2 Design Patterns
    3. 1.3 The Act of Design
      1. 1.3.1 How Design Fails
      2. 1.3.2 When to Design
      3. 1.3.3 Judging Design
    4. 1.4 A Brief Introduction to Object-Oriented Programming
      1. 1.4.1 Procedural Languages
      2. 1.4.2 Object-Oriented Languages
    5. 1.5 Summary
  11. 2 Designing Classes with a Single Responsibility
    1. 2.1 Deciding What Belongs in a Class
      1. 2.1.1 Grouping Methods into Classes
      2. 2.1.2 Organizing Code to Allow for Easy Changes
    2. 2.2 Creating Classes That Have a Single Responsibility
      1. 2.2.1 An Example Application: Bicycles and Gears
      2. 2.2.2 Why Single Responsibility Matters
      3. 2.2.3 Determining If a Class Has a Single Responsibility
      4. 2.2.4 Determining When to Make Design Decisions
    3. 2.3 Writing Code That Embraces Change
      1. 2.3.1 Depend on Behavior, Not Data
      2. 2.3.2 Enforce Single Responsibility Everywhere
    4. 2.4 Finally, the Real Wheel
    5. 2.5 Summary
  12. 3 Managing Dependencies
    1. 3.1 Understanding Dependencies
      1. 3.1.1 Recognizing Dependencies
      2. 3.1.2 Coupling Between Objects (CBO)
      3. 3.1.3 Other Dependencies
    2. 3.2 Writing Loosely Coupled Code
      1. 3.2.1 Inject Dependencies
      2. 3.2.2 Isolate Dependencies
      3. 3.2.3 Remove Argument-Order Dependencies
    3. 3.3 Managing Dependency Direction
      1. 3.3.1 Reversing Dependencies
      2. 3.3.2 Choosing Dependency Direction
    4. 3.4 Summary
  13. 4 Creating Flexible Interfaces
    1. 4.1 Understanding Interfaces
    2. 4.2 Defining Interfaces
      1. 4.2.1 Public Interfaces
      2. 4.2.2 Private Interfaces
      3. 4.2.3 Responsibilities, Dependencies, and Interfaces
    3. 4.3 Finding the Public Interface
      1. 4.3.1 An Example Application: Bicycle Touring Company
      2. 4.3.2 Constructing an Intention
      3. 4.3.3 Using Sequence Diagrams
      4. 4.3.4 Asking for “What” Instead of Telling “How”
      5. 4.3.5 Seeking Context Independence
      6. 4.3.6 Trusting Other Objects
      7. 4.3.7 Using Messages to Discover Objects
      8. 4.3.8 Creating a Message-Based Application
    4. 4.4 Writing Code That Puts Its Best (Inter)Face Forward
      1. 4.4.1 Create Explicit Interfaces
      2. 4.4.2 Honor the Public Interfaces of Others
      3. 4.4.3 Exercise Caution When Depending on Private Interfaces
      4. 4.4.4 Minimize Context
    5. 4.5 The Law of Demeter
      1. 4.5.1 Defining Demeter
      2. 4.5.2 Consequences of Violations
      3. 4.5.3 Avoiding Violations
      4. 4.5.4 Listening to Demeter
    6. 4.6 Summary
  14. 5 Reducing Costs with Duck Typing
    1. 5.1 Understanding Duck Typing
      1. 5.1.1 Overlooking the Duck
      2. 5.1.2 Compounding the Problem
      3. 5.1.3 Finding the Duck
      4. 5.1.4 Consequences of Duck Typing
    2. 5.2 Writing Code That Relies on Ducks
      1. 5.2.1 Recognizing Hidden Ducks
      2. 5.2.2 Placing Trust in Your Ducks
      3. 5.2.3 Documenting Duck Types
      4. 5.2.4 Sharing Code between Ducks
      5. 5.2.5 Choosing Your Ducks Wisely
    3. 5.3 Conquering a Fear of Duck Typing
      1. 5.3.1 Subverting Duck Types with Static Typing
      2. 5.3.2 Static versus Dynamic Typing
      3. 5.3.3 Embracing Dynamic Typing
    4. 5.4 Summary
  15. 6 Acquiring Behavior through Inheritance
    1. 6.1 Understanding Classical Inheritance
    2. 6.2 Recognizing Where to Use Inheritance
      1. 6.2.1 Starting with a Concrete Class
      2. 6.2.2 Embedding Multiple Types
      3. 6.2.3 Finding the Embedded Types
      4. 6.2.4 Choosing Inheritance
      5. 6.2.5 Drawing Inheritance Relationships
    3. 6.3 Misapplying Inheritance
    4. 6.4 Finding the Abstraction
      1. 6.4.1 Creating an Abstract Superclass
      2. 6.4.2 Promoting Abstract Behavior
      3. 6.4.3 Separating Abstract from Concrete
      4. 6.4.4 Using the Template Method Pattern
      5. 6.4.5 Implementing Every Template Method
    5. 6.5 Managing Coupling between Superclasses and Subclasses
      1. 6.5.1 Understanding Coupling
      2. 6.5.2 Decoupling Subclasses Using Hook Messages
    6. 6.6 Summary
  16. 7 Sharing Role Behavior with Modules
    1. 7.1 Understanding Roles
      1. 7.1.1 Finding Roles
      2. 7.1.2 Organizing Responsibilities
      3. 7.1.3 Removing Unnecessary Dependencies
      4. 7.1.4 Writing the Concrete Code
      5. 7.1.5 Extracting the Abstraction
      6. 7.1.6 Looking Up Methods
      7. 7.1.7 Inheriting Role Behavior
    2. 7.2 Writing Inheritable Code
      1. 7.2.1 Recognize the Antipatterns
      2. 7.2.2 Insist on the Abstraction
      3. 7.2.3 Honor the Contract
      4. 7.2.4 Use the Template Method Pattern
      5. 7.2.5 Preemptively Decouple Classes
      6. 7.2.6 Create Shallow Hierarchies
    3. 7.3 Summary
  17. 8 Combining Objects with Composition
    1. 8.1 Composing a Bicycle of Parts
      1. 8.1.1 Updating the Bicycle Class
      2. 8.1.2 Creating a Parts Hierarchy
    2. 8.2 Composing the Parts Object
      1. 8.2.1 Creating a Part
      2. 8.2.2 Making the Parts Object More Like an Array
    3. 8.3 Manufacturing Parts
      1. 8.3.1 Creating the PartsFactory
      2. 8.3.2 Leveraging the PartsFactory
    4. 8.4 The Composed Bicycle
    5. 8.5 Deciding between Inheritance and Composition
      1. 8.5.1 Accepting the Consequences of Inheritance
      2. 8.5.2 Accepting the Consequences of Composition
      3. 8.5.3 Choosing Relationships
    6. 8.6 Summary
  18. 9 Designing Cost-Effective Tests
    1. 9.1 Intentional Testing
      1. 9.1.1 Knowing Your Intentions
      2. 9.1.2 Knowing What to Test
      3. 9.1.3 Knowing When to Test
      4. 9.1.4 Knowing How to Test
    2. 9.2 Testing Incoming Messages
      1. 9.2.1 Deleting Unused Interfaces
      2. 9.2.2 Proving the Public Interface
      3. 9.2.3 Isolating the Object under Test
      4. 9.2.4 Injecting Dependencies Using Classes
      5. 9.2.5 Injecting Dependencies as Roles
    3. 9.3 Testing Private Methods
      1. 9.3.1 Ignoring Private Methods during Tests
      2. 9.3.2 Removing Private Methods from the Class under Test
      3. 9.3.3 Choosing to Test a Private Method
    4. 9.4 Testing Outgoing Messages
      1. 9.4.1 Ignoring Query Messages
      2. 9.4.2 Proving Command Messages
    5. 9.5 Testing Duck Types
      1. 9.5.1 Testing Roles
      2. 9.5.2 Using Role Tests to Validate Doubles
    6. 9.6 Testing Inherited Code
      1. 9.6.1 Specifying the Inherited Interface
      2. 9.6.2 Specifying Subclass Responsibilities
      3. 9.6.3 Testing Unique Behavior
    7. 9.7 Summary
  19. Afterword
  20. Index
  21. Code Snippets