Held: Wednesday, 7 April 2004
Today we reconsider how to design new objects.
- Introduction to Object-Oriented Design.
- Why do Object-Oriented Design.
- Some basic strategies.
- A group exercise.
- 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
- 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 often assign protection levels to the fields and methods of a class to say that
certain other kinds of objects can't know about this.
- Object-oriented design can be appropriate for modeling
problems, particularly simulations.
- Object-oriented design can better support code reuse through
inheritance and polymorphism.
- There are four kinds of
protection for your fields and methods in Java.
public fields and methods can be accessed by any other object or class.
protected fields and methods can be accessed by any object in the same or subclasses, class methods and subclass methods, and by objects and class methods for classes in the same package.
- package fields and methods (the ones you get if you don't specifiy a protection) can be accessed by objects and class methods for classes in the same package.
private fields and methods can be accessed only by objects and methods associated with the same class.
- 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.
- Here's a basic technique for designing 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
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
- 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.
- We'll do the wonderful and traditional exercise of designing an elevator simulation.
- Why would you want that simulation?
- Describe typical interactions with an elevator.