Skip to the content.

Programming by Contract

Back to Homepage
Back to Week 2


Put the responsibility on the client to fufill the preconditions and postconditions

Classes: Levels of Complexity

  1. POD (Plain Old Data)
    • Dont have to worry about designing
  2. Computed Fields
    • Only rely on data inside class
      • Internal data
  3. Uses contained objects
    • Objects that contain other complicated objects
  4. Orchestrates interactions between objects
    • Two different objects, need to do something with both of them in a specific order or way

Room Class
Person Class

List<Person> occupants
  int maxOccupancy;     // If at max occ want to reject new person entering room
  
Room currentRoom;

How do you know if room can accept person? Can person move? Updating of fields?

Create mediator that helps keep track of everything. Knows about other objects, updates rooms and persons.


Programming by Contract

Design by Contract, other name

Method

BCPL (Basic Common Programming Language) -> B -> C -> C++ -> Java -> C#


Defensive Programming