Design Patterns for Marine Biology Simulation

advertisement
Marine Biology Simulation
Part I: Introduction
Dung “Zung” Nguyen
Mathias Ricken
Stephen Wong
TeachJava 2004!
June 25, 2004
The Role of Programming
Important skill
Should be taught early in a student’s career
Usually taught imperative-first
Concentrates on language elements like
expressions, operators, functions, pointers…
Problems Teaching Imperative-First
Focus on syntactical elements
Exercises too simple to be motivating
Too little emphasis on design
Biased against students without prior
knowledge
Object-Orientation
Beneficial to software quality
Correct, robust, flexible, and extensible
Extensively covers software design
Examples are more challenging and
motivating
Usually considered “advanced concept”
Assignments
Assignments have to be complex
Highlight benefits of object-orientation
Large design portion
Find proper abstractions, variants / invariants
Solve family of problems
Challenge to find suitable assignments
Example: Marine Biology Simulation
AP Marine Biology Simulation
Case studies in AP curriculum since 1994
Argument:
“Some good reasons to use a case study in AP
Computer Science include:
• Working with a program of significant length
• Thinking through issues of good program
design
• Learning from an expert programmer”
(AP Marine Biology Simulation Case Study – Teacher’s Manual)
AP Marine Biology Simulation
Goal:
“Through the AP Marine Biology Simulation Case
Study, the strategies, vocabulary, and
techniques of object-oriented design will be
emphasized.”
(AP Marine Biology Simulation Case Study – Teacher’s Manual)
Shortcomings
Improper abstractions, incorrect delineation of
responsibilities  not robust
Tight coupling  not flexible and extensible
AP Marine Biology Simulation
Demo
AP Marine Biology Simulation
a few pitfalls...
Insecure architecture
Replicated data
Unenforceable contracts
Limited extensibility
Key issue:
Tight Coupling
And
On
Which
I know
some
notisthe
where
not
global
Where
Think
same
the
ISimple.
coordinate
am:
am
same
asI?
right
here
as here.
either.
here.
grid?
Do I know my own coordinates?
Does anyone else know my coordinates?
Should I know anyone else’s coordinates?
Do I really care where Greenwhich is?
X
X
X
Latitude = 29.65
Longitude = 95.28
The local
is an
Each
fish environment
has its own local
indirectionenvironment
layer between the fish
and the global environment.
X
X
X
X
X
Fish and their Environment
A fish has a local
environment
Local environment
communicates with
global environment
but fish do not.
The local
environment
provides services
to the fish
The local environment
is instantiated
by the
The local
Factory
Method
global
environment
environment
does
Design
Pattern
NOT provide x-y
coordinates!
Send
aIcommands
message
totothe
The
The
Unblocked
appropriate
Blocked
fish
Can
Aresponds
Fish’s
move
case
case
command
Dilemma
forward?
command
accordingly!
command
is selected
Send
localenvironment
environment!
local
Unblocked!
Blocked!
X
Local
environment
makes
a
The
If desired,
appropriate
Movefish
command
applies
Or…
command
sent
move
is
tocommand
selected
fish
move command
X
Unblocked!
Move
Blocked!Command
A fish can ONLY
move as
prescribed by its
environment!
A fish does not need to know
its x-y coordinate!
A fish does not need to know
“how” to move!
A fish is decoupled from the
global environment!
Fish-Environment Interaction
Delegate to the local
environment
SimpleFish Class
_localEnv.tryMoveFwd(this, new ILambda() {
public Object apply(Object notUsed) {
turnRight(Math.PI);
Command
return null;
to execute
if blocked
}
}, new ILambda() {
public Object apply(Object moveCmd) {
((ILambda)moveCmd).apply(null);
return null;
Command
to execute if
}
unblocked
});
Fish-Environment Interaction
Make local
Depending on
LocalEnvironment
whether theClass
new
local environment
is empty or not…
environment in the
direction the fish
wants to move
… either make a
command that
actually moves
the fish…
Code is used in a class assignment
and has been removed
… and
execute the
unblocked
command
… or execute
the blocked
command
Design Benefits
Decoupled, abstract design
Enhanced capabilities
Replaceable
environments
Different fish
Robustness
Rice Marine Biology Simulation
Demo
The Culmination of a
Unified OO Pedagogy
MBS
~ end of 2nd semester.
Unit Tests
Design Patterns
Polymorphism
Abstract Structure and Behavior
Problems Using This in AP Classes
Defined curriculum
Students must perform well in exams
Teach what exams require
Current curriculum disagrees with our
approach
Rice MBS is for university-level classes
Results
Objects-first is programming for everyone
Addresses problems
of imperative-first
Focus on design
Motivating
assignments
Not biased against
Lack of experience
Gender
Declared Major
Year in School
SAT Scores
AP curriculum will hopefully change
Future Work
Make simulation guarantee progress
Prevent one fish from blocking others
Extra credit competition using MBS
Game of life, outbreed other species?
Study MBS along several cognitive
dimensions
Improve assignment quality
Download