Designing Programs with Class

Sam Tobin-Hochstadt and David Van Horn

This book introduces the fundamental elements of class-based program design.

The book is also available in PDF form here.

    Preface

      Design Recipes

      The Choice of Language and Environment

      The Parts of the Book

      Acknowledgments

    I Basic Design with Objects

    2 Objects = Data + Function

      2.1 Functional rocket

        2.1.1 The next function

        2.1.2 The render function

        2.1.3 Lift off

      2.2 Object-oriented rocket

        2.2.1 A class of rockets

        2.2.2 The next and render methods

        2.2.3 A big-bang oriented to objects

      2.3 A Brief History of Objects

      2.4 Exercises

        2.4.1 Complex, with class

      1.1 Circles

    3 Classes of Objects: Data Definitions

      3.1 Atomic and Compound Data

      3.2 Enumerations

      3.3 Unions and Recursive Unions

      3.4 Revisiting the Rocket

        3.4.1 Landing and taking off

        3.4.2 Adding a satellite

      3.5 Exercises

        3.5.1 Lists of Numbers

        3.5.2 Home on the Range

    4 Classes of Objects: Interface Definitions

      4.1 Lights, revisited

      4.2 A light of a different color

      4.3 Representation inpedendence and extensibility

      4.4 Sharing Interfaces

      4.5 Exercises

        4.5.1 Beings, Zombies, and You

        4.5.2 Super Zombie!

        4.5.3 Modulo Zombie!

        4.5.4 Mixed Zombie!

    5 Parameterized Data and Interfaces

      5.1 Parametric data

      5.2 Parametric interfaces

      5.3 Parameteric methods

      5.4 Exercises

        5.4.1 Parametric Lists

    6 Solidifying what we’ve done

      6.1 Data Definitions

      6.2 Interface Definitions

      6.3 Contracts

      6.4 Design Recipe

      6.5 Design Choices

      6.6 Exercises

        6.6.1 JSON, Jr.

        6.6.2 JSON

    VII Schemes of a Larger Design

    8 Larger system design: Snakes on a plane

      8.1 Information in the Snake Game

      8.2 The world

      8.3 Coordinate interface

      8.4 An implementation of coordinates: segments

      8.5 Another implementation of coordinates: food

      8.6 Representing the snake

      8.7 Seeing the world

      8.8 The whole ball of wax

      8.9 Exercises

        8.9.1 Different representation of Snakes

        8.9.2 Zombie!

        8.9.3 Primum non copy-and-paste

      2.1 Space Invaders!

    9 Universe

      9.1 A look at the Universe API

      9.2 Messages

      9.3 Simple world

      9.4 Simple world, broadcasting to server

      9.5 Simple universe, receiving broadcasts

      9.6 Simple world, receiving messages from the server

      9.7 Rules of engagement: protocols and enforcement

      9.8 Exercises

        9.8.1 Tron

    10 Guess my number

      10.1 One Player Guess my Number

        10.1.1 The GmN server

        10.1.2 The GmN Client

        10.1.3 Many Players, One Number

        10.1.4 Guessing Big

      10.2 Two player guess my number

    XI Abstraction with Objects

    12 Abstraction via Delegation

      12.1 Constructor design issue in modulo zombie (Assignment 3, Problem 3)

      12.2 Abstracting list methods with different representations

      12.3 Delegation

    13 Abstraction via Inheritance

      13.1 Method inheritance with binary trees

      13.2 The class/1 language

      13.3 “Abstract” classes

      13.4 Data inheritance with binary trees

      13.5 Inheritance with shapes

      13.6 Revisiting the Rocket with Inheritance

      13.7 Exercises

        13.7.1 Abstract Lists

        13.7.2 Shapes

    14 Abstraction via Functions

      14.1 Functions as objects: abstracting predicates

      14.2 Parametric data and separation of concerns

      14.3 Functions as objects: abstracting comparisons

      14.4 Functions as data as objects: infinite sequences

      14.5 Exercises

        14.5.1 Functional programming with objects

        14.5.2 Lists and functional objects

        14.5.3 Searching JSON with String predicates

    15 Overriding

    16 Extensional Equality

      16.1 Structural equality for recursive unions

      16.2 Abstracting equality with double dispatch

      16.3 Equality over interpretation

      16.4 Detatching objects from interpretation

      16.5 Multiple Representations

      16.6 Parameterized Data Defintions and Equality

      16.7 Exercises

        16.7.1 Extensional equality JSON

    17 Visitors and Folds

      17.1 The Visitor Pattern

      17.2 Folds

      17.3 Generators

      17.4 Exercises

        17.4.1 Quick visits

        17.4.2 Folds vs Visitors

        17.4.3 JSON visitor

    XVIII Invariants

    19 Invariants, Testing, and Abstraction Barriers

      19.1 Invariants of Data Structures

      19.2 Properties of Programs and Randomized Testing

      19.3 Abstraction Barriers and Modules

      19.4 Exercises

        19.4.1 Quick Lists

    20 Constructors

      20.1 Canonical forms

      20.2 Integrity checking

      20.3 Ordered binary trees

      20.4 Exercises

        20.4.1 Queues

    XXI Mutation

    22 Ch-Ch-Ch-Ch-Changes

    23 Circular Data

    24 Back-channels

    25 Intensional equality

    XXVI Java

    27 Java

      27.1 Two Ideas: Java and Types

      27.2 Programming in Java

        27.2.1 Java Syntax

      27.3 Running Java Programs

      27.4 A More Complex Example

      27.5 Recursive Unions

      27.6 Enumerations

      27.7 Parameterized Data Definitions

      27.8 Abstraction

    28 ???

      28.1 Types

    29 Extensional Equality in Java

      29.1 Posn

      29.2 Equality in Java

      29.3 List of Posn

      29.4 Equality and Parameterized Types

      29.5 Comparing different kinds of things

        29.5.1 equals and hashCode

    XXX A Class of Your Own

    31 Under the Hood: Implementing OO

      31.1 Pulling back the veil from object-oriented programming

      31.2 Objects as Functions

      31.3 Objects as Structures

    XXXII Solutions

    33 Solutions

      33.1 Solution: Complex, with class

      33.2 Solution: Lists of Numbers

      33.3 Solution: Home on the Range

      33.4 Solution: Zombie!

      33.5 Solution: Parametric Lists

      33.6 Solution: Shapes

      33.7 Solution: Abstract Lists

      33.8 Solution: Functional programming with objects

    Index