Held Wednesday, September 6, 2000
Summary
Today, we return to issues of object-oriented design, considering them
in more depth than at the beginning of the semester. To ground our
considerations, we consider the objects that we might create as we
build our project.
Notes
- Assignments:
- Read labs O2 and
O3
for Friday.
- Bring questions on homework 2
for Friday.
- As I mentioned at the end of class yesterday, I'd like to hear any
comments you have on student spaces for computer science in Phase II.
- Today our emphasis will be on theory of object-oriented design. Friday's
class will cover specifics in Java.
- Thanks to everyone who handed in
homework 1.
- I'll try to get it graded by Monday.
- Those of you who didn't turn it in yesterday need to talk to me.
Overview
- About the project
- Object-oriented design
- Basics: The three (four?) key aspects
- Detour: Designing methods
- The role of narrative
- Sample OOD: Our project
- As we decided in class yesterday, we will do the four-year planner
as our project.
- I'll do my best to find an assistant to enter the raw data, so that
your work is on design and programming.
- In order to successfully work in an object-oriented paradigm
(and an object-oriented language), you need to become accustomed
to thinking in objects.
- Last semester, you emphasized thinking in functions.
- You'll see some similarities and some differences.
- 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.
- In the next class, we'll look at how these concepts are implemented
in Java.
- 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.
- As you read 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.
- Encapsulation: Clients of objects know what the objects
do, but not how.
- You know that Java can multiply doubles, but not necessarily how
it represents them or how it multiplies.
- Inheritance: You can build new classes based on existing
classes.
- To add capabilities to our Fraction class (e.g., a new print method),
we shouldn't have to define a new class.
- Polymorphism: If two objects provide similar capabilities,
then you can use them "interchangably".
- For example, we can square anything we know how to multiply.
- Similarly, we can make a rainbow pattern from anything we can draw
in different colors.
- 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 talk about the four-year planner.
- We'll tell some stories:
- From the student's perspective
- From the advisor's perspective
- From other perspectives
- We'll identify objects
- We'll consider capabilities
- We'll talk about the interactions between methods in the objects.