Designing Programs with Class

Sam Tobin-Hochstadt, David Van Horn and Benjamin Lerner

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 Case Study: Mobiles

      4.5 Sharing Interfaces

    5 PacMan

      5.1 Basic game mechanics

      5.2 Defining the playing space

      5.3 The player

      5.4 Delegation

      5.5 Ghosts

      5.6 Drawing the game

      5.7 Changing Places

        5.7.1 Player in motion, or, I ain’t afraid of no ghosts!

      5.8 World-building

    6 Parameterized Data and Interfaces

      6.1 Parametric data

      6.2 Parametric interfaces

      6.3 Parameteric methods

      6.4 Exercises

        6.4.1 Parametric Lists

    7 Solidifying what we’ve done

      7.1 Data Definitions

      7.2 Interface Definitions

      7.3 Contracts

      7.4 Design Recipe

      7.5 Design Choices

      7.6 Exercises

        7.6.1 JSON, Jr.

        7.6.2 JSON

    VIII Abstraction with Objects

    9 Abstraction via Delegation

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

      9.2 Abstracting list methods with different representations

      9.3 Delegation

    10 Abstraction via Inheritance

      10.1 Method inheritance with binary trees

      10.2 The class/1 language

      10.3 “Abstract” classes

      10.4 Data inheritance with binary trees

      10.5 Inheritance with shapes

      10.6 Revisiting the Rocket with Inheritance

      10.7 Exercises

        10.7.1 Abstract Lists

        10.7.2 Shapes

    XI Invariants

    12 Invariants, Testing, and Abstraction Barriers

      12.1 Invariants of Data Structures

      12.2 Properties of Programs and Randomized Testing

      12.3 Abstraction Barriers and Modules

      12.4 Exercises

        12.4.1 Quick Lists

    XIII Schemes of a Larger Design

    14 Larger system design: Snakes on a plane

      14.1 Information in the Snake Game

      14.2 The world

      14.3 Coordinate interface

      14.4 An implementation of coordinates: segments

      14.5 Another implementation of coordinates: food

      14.6 Representing the snake

      14.7 Seeing the world

      14.8 The whole ball of wax

      14.9 Exercises

        14.9.1 Different representation of Snakes

        14.9.2 Zombie!

        14.9.3 Primum non copy-and-paste

        14.9.4 Space Invaders!

    15 Universe

      15.1 A look at the Universe API

      15.2 Messages

      15.3 Simple world

      15.4 Simple world, broadcasting to server

      15.5 Simple universe, receiving broadcasts

      15.6 Simple world, receiving messages from the server

      15.7 Rules of engagement: protocols and enforcement

      15.8 Exercises

        15.8.1 Tron

    16 Guess my number

      16.1 One Player Guess my Number

        16.1.1 The GmN server

        16.1.2 The GmN Client

        16.1.3 Many Players, One Number

        16.1.4 Guessing Big

      16.2 Two player guess my number

    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 Mutation

    19 Ch-Ch-Ch-Ch-Changes

    20 Circular Data

    21 Back-channels

    22 Intensional equality

    XXIII Java

    24 Java

      24.1 Two Ideas: Java and Types

      24.2 Programming in Java

        24.2.1 Java Syntax

      24.3 Running Java Programs

      24.4 A More Complex Example

      24.5 Recursive Unions

      24.6 Enumerations

      24.7 Parameterized Data Definitions

      24.8 Abstraction

    25 ???

      25.1 Types

    26 Extensional Equality in Java

      26.1 Posn

      26.2 Equality in Java

      26.3 List of Posn

      26.4 Equality and Parameterized Types

      26.5 Comparing different kinds of things

        26.5.1 equals and hashCode

    27 Iterating over Data

    XXVIII A Class of Your Own

    29 Under the Hood: Implementing OO

      29.1 Pulling back the veil from object-oriented programming

      29.2 Objects as Functions

      29.3 Objects as Structures

    XXX Solutions

    31 Solutions

    Index