Design Patterns for Design Patterns for Marine Biology SimulationMarine Biology Simulation
Dung “Zung” NguyenDung “Zung” NguyenMathias RickenMathias RickenStephen WongStephen WongRice UniversityRice University
AP Marine Biology SimulationAP Marine Biology Simulation
Unenforceable contracts
Insecure architecture
a few pitfalls...a few pitfalls...
Key issue:Key issue:
Limited extensibility
Replicated data
Tight Tight CouplingCoupling
Where am I?Where am I?
Latitude = 29.65 Longitude = 95.28
On some global coordinate grid?On some global coordinate 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?
Think Simple.Think Simple.I know where I am: right I know where I am: right herehere..
X
X
Which is Which is notnot the same as the same as herehere..
X
And And notnot the same as the same as herehere either. either.
Each fish has its Each fish has its ownown local local environmentenvironment
XX
X
X
X
The local environment is an The local environment is an indirection layer between the fish indirection layer between the fish
and the global environment.and the global environment.
Fish and their EnvironmentFish and their EnvironmentA fish has a local
environmentThe local
environment provides services
to the fish
The local environment does NOT provide x-y
coordinates!
The local environment is
instantiated by the global environment
Factory Method Design Pattern
Local environment communicates with global environment
but fish do not.
A Fish’s DilemmaA Fish’s Dilemma
Unblocked!Blocked!
Can I move forward?Can I move forward?Send a message to the Send a message to the local environment!local environment!
Blocked case commandBlocked case commandUnblocked case commandUnblocked case commandSend commands to Send commands to local environmentlocal environment
The appropriate command is selectedThe appropriate command is selectedThe fish responds accordingly!The fish responds accordingly!
X
Or…Or…The appropriate command is selectedThe appropriate command is selectedLocal environment makes a Local environment makes a move commandmove command
Move command sent to fishMove command sent to fishIf desired, fish applies move command If desired, fish applies move command A fish does not need to know A fish does not need to know its x-y coordinate! its x-y coordinate!
A fish does not need to know A fish does not need to know “how” to move!“how” to move!
A fish is decoupled from the A fish is decoupled from the global environment!global environment!
MoveCommand
Unblocked!
Blocked!X
A fish can ONLY move as
prescribed by its environment!
Fish-Environment InteractionFish-Environment Interaction
_localEnv.tryMoveFwd(this,
,
);
SimpleFish Class
new ILambda() { public Object apply(Object moveCmd) { ((ILambda)moveCmd).apply(null); return null; }}
Command to execute if blocked
Command to execute if unblocked
new ILambda() { public Object apply(Object notUsed) { turnRight(Math.PI); return null; }}
Delegate to the local environment
Fish-Environment InteractionFish-Environment Interactionpublic Object tryMoveFwd(AFish f, ILambda blocked, ILambda unblocked) {
ILocalEnv le = makeMoveFwdLocalEnv();
return le.execute(new ILocalEnvVisitor() {
public Object emptyCase(ILocalEnv h, Object nu) { ILambda moveCmd = new MoveLambda(le); return unblocked.apply(moveCmd); }
public Object nonEmptyCase(ILocalEnv h, Object nu) { return blocked.apply(null); }
}, null);}
LocalEnvironment Class Make local environment in the direction the fish
wants to move… either make a command that actually moves
the fish…
Depending on whether the new
local environment is empty or not…
… and execute the unblocked command
… or execute the blocked command
Design Patterns for DecouplingDesign Patterns for Decoupling
MVC Decouples a model
from its view
OBSERVER-OBSERVABLEDecouples sender and
multiple receivers
VISITOR Decouples structure and
its behaviors.
FACTORYDecouples framework from object creation
COMMANDDecouples decisions
from actions.
COMPOSITEDecouples a whole
from its parts.
INTEGRAL COMPONENTS OF A UNIFIED PEDAGOGY
The Culmination of a The Culmination of a Unified OO PedagogyUnified OO Pedagogy
Abstract Structure and BehaviorAbstract Structure and Behavior
PolymorphismPolymorphism
Design PatternsDesign Patterns
Unit TestsUnit Tests
MBSMBS ~ end of 2nd semester.
Studying the Case StudyStudying the Case Study
SimpleFishUnboundedEnv Variant Fish Behavior
Variant Environment Behavior
Re-implement as per given specs.
Re-implement as per given specs.
White-box Framework:Extension
by Subclassing
Inheritance vs. CompositionInheritance vs. Composition
Strategy Pattern
+ void move()
+ void move() + void move()
Static Behavior! Separate the variants from the invariants
Comparative study vs. APMBSComparative study vs. APMBS
Replaceable environmentsReplaceable environments RobustnessRobustness SecuritySecurity
Enhanced capabilitiesEnhanced capabilities
Abstracted, decoupled designAbstracted, decoupled design
Is it really about Fish?Is it really about Fish? !! !
Design patterns are expressions of recurring abstractionsthat embody fundamental computing principles
Learning to abstract the problems at hand and master their complexity
Design patterns are “tangible abstractions”
Design patterns empower students to design software that is correct, flexible, extensible and robust.
It’s all about Abstraction!It’s all about Abstraction!
Why design patterns?
For more information:For more information:
http://www.exciton.cs.rice.edu/research/SIGCSE04/http://www.exciton.cs.rice.edu/research/SIGCSE04/ To download code files:To download code files:
– Username: Username: sigcse04sigcse04– Password: Password: ricembsricembs
E-mail:E-mail:– [email protected]@rice.edu– [email protected]@rice.edu– [email protected]@rice.edu
User-Environment InteractionUser-Environment Interaction
public ILambda makeEditCmd(Point p, IFishFactory factory) {
final ILocalEnv le = makeLocalEnv(p);
return (ILambda)le.execute(new ILocalEnvVisitor() {
public Object emptyCase(ILocalEnv h, Object nu) { return addFish(localEnv, factory.createFish()); }
public Object nonEmptyCase(ILocalEnv h, Object nu) { return editFish(host); } }, null);}
AGlobalEnv ClassMake local
environment for the location of the mouse
click
… either add a fish there using
the factory
Depending on whether the new
local environment is empty or not…
… or edit the fish’s local
environment