Held Friday, February 4, 2000
Overview
Today, we investigate basic techniques for doing object oriented
design. To ground our investigations, we consider the objects
that we might create as we build our project.
Notes
- Are there questions on
assignment 2?
- Assigned:
- Read labs O2 and O3.
- Phase 1 of the project:
Short narratives (Due Monday)
Contents
Summary
- Project discussion
- (Re)Introduction to object-oriented design
- Motivation
- Key attributes
- Sample object-oriented designs
- The six P's
- Due:
- Reading of Lab O1: Object-Oriented Design (we will not
do this lab)
- Since I didn't receive email from any of you (at least not at the
time of this writing), I get to dictate the project.
- We're going to do some form of game. We will vote on the following
issues.
- One player, two player, or multiplayer?
- If two or more players, are they all on the same computer or
is the game networked?
- Do we want to do a board game (othello, scrabble, etc.) or
a move interactive, video-game-like thing?
- Can we vary some of the rules?
- Can we vary some of the rules while playing?
- During today's class, we may discuss a possible architecture for
the game.
- In order to successfully work in an object-oriented paradigm
(and an object-oriented language), you need to become accustomed
to thinking in objects.
- This is in contrast to the thinking in functions that
you did last semester.
- What is an object?
- Essentially, a collection of information and methods (functions).
- You can think of the information as giving a state to the
object.
- In a watch, part of the state is the time that it represents.
- In a drawer, part of the state is the position of the drawer.
(We also care whether it's full or empty.)
- In a game, part of the state of a piece might be its position
on the board or its color.
- As you may know, object-oriented design has become one of the most
important design trends in the mid-to-late 1990's. Why?
- Object-oriented design makes it easier to encapsulate
parts of the program.
- Someone working on one object or class need only know about the
capabilities of another object, and not its internals.
- Using information hiding it becomes possible to modify
the inside of one class without affecting the operation of the
program as a whole.
- This is why we make the fields of a method
protected
;
to say that ``other objects can't know about this''.
- Object-oriented design can be appropriate for modeling ``real''
problems, particularly simulations.
- Object-oriented design can better support code reuse through
inheritance and polymorphism.
- We'll return to these concepts on Monday.
- O2 and O3 should help.
- To successfully engage in object-oriented design, you need to be
able to identify the key objects and classes in your program.
- You also need to think about how they communicate.
- In lab O1, you considered
some techniques for thinking about objects.
- Write narratives.
- Circle nouns. These are potential objects.
- Box verbs. These are potential methods.
- When writing narratives, you may also want to consider what's going
on ``behind the scenes'' as it were.
- I've found that it helps to have a rubric to use when designing
methods. Last semester, we came up with ``the six P's'' that
you should think about whenever you write a method.
- The purpose of the method.
- The parameters of the method.
- The preconditions of the method. That is, requirements
that must be met in order for the method to succeed.
- The postconditions of the method. That is, what you
can guarantee will hold after the method finishes successfully.
- Potential problems that may arise and what you do about
them.
- The type and value of results the method produces.
(The return value and the return type. It took a little effort
to fit this round peg into the square hole of ``begins with p''.)
- You will typically start with the first two P's and then add the
others as you go.
- As a first example, we'll work together to consider the objects one
might use in an elevator simulation (or even the software to
control that elevator).
- Assume there are N elevators and M floors.
- This is a fairly traditional first exercise in object-oriented
design. Surpringly, different books come up with very different
solutions. I'll try not to bias the results.
- We'll tell some stories:
- From the rider's perspective
- From the elevator's perspective
- From other perspectives
- We'll identify objects
- We'll consider capabilities
- We'll talk about the interactions of methods in the objects.
- Let's try to do the same thing (story telling) for the project.
- You will continue this exercise over the weekend.