CSE143 Au03 03-1
10/9/2003 (c) 2001-3, University of Washington 03-1
CSE 143 Java
Interfaces Reading: Ch. 15.1.3
10/9/2003 (c) 2001-3, University of Washington 03-2
A Problem – Object Model for a Simulation
- Suppose we are designing the classes for a simulation
game like the Sims, or Sim City
- We might want to model
- People (office workers, police/firemen, politicians, …)
- Pets (cats, dogs, ferrets, lizards, …)
- Vehicles (cars, trucks, buses, …)
- Physical objects (buildings, streets, traffic lights, …)
- Object model – use inheritance
- Base classes for People, Pets, Vehicles, PhysicalThings, …
- Extended classes for specific kinds of things (Cat extends Pet,
Dog extends Pet, Truck extends Vehicle…)
10/9/2003 (c) 2001-3, University of Washington 03-3
Making it Tick
- A time-based simulation has some sort of clock that ticks
regularly
- On each tick, every object in the simulation needs to, for instance,
update its state, maybe redraw itself, …
- We would like to write methods in the simulation engine that can
work with any object in the simulation
/** update the state of simulation object thing for one clock tick */ public void updateState(??? thing) { thing.tick( ); thing.redraw( ); }
- Question: What is the type of parameter thing in this method?
10/9/2003 (c) 2001-3, University of Washington 03-4
Type Compatibility
- We want to be able to write something like
public void updateState(SimThing thing) { … }
where “SimThing” is a type that is compatible with Cats, Cars, People, Buildings. How?
- Could create an additional superclass SimThing and
have People, Pets, Vehicles, PhysicalThings, …, all extend it, but:
- People, Pets, etc. don’t have a real “is-a” relationship
- What if we wanted to have other polymorphic methods that, for
example, only apply to breathing things?
- Deep inheritance hierarchies are brittle, hard to modify