Design Patterns for Marine Biology Simulation Dung Zung
Design Patterns for Marine Biology Simulation Dung “Zung” Nguyen Mathias Ricken Stephen Wong Rice University
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 as. I? 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
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 does Design Pattern NOT provide x-y coordinates!
Send a. Icommands message totothe The Unblocked appropriate Blocked fish Can Aresponds Fish’s move case command Dilemma forward? command accordingly! command is selected Send localenvironment! local Unblocked! Blocked! X
makes a Aappropriate ALocal fishdoes isenvironment decoupled not need from tois know the The If desired, Move fish command applies Or… command sent move to command selected fish move command global its“how” x-yenvironment! coordinate! to move! X Unblocked! Move Blocked!Command A fish can ONLY move as prescribed by its environment!
Fish-Environment Interaction Simple. Fish Class Delegate to the local environment _local. Env. try. Move. Fwd(this, new ILambda() { public Object apply(Object not. Used) { turn. Right(Math. PI); Command return null; to execute if blocked } }, new ILambda() { public Object apply(Object move. Cmd) { ((ILambda)move. Cmd). apply(null); return null; Command to execute if } unblocked });
Fish-Environment Interaction Depending on Make local Local. Environment whether the. Class new environment in the local environment public Object try. Move. Fwd(AFish f, ILambda blocked, direction the fish is empty or not… ILambda { wants unblocked) to move … either make a command that ILocal. Env le = make. Move. Fwd. Local. Env(); actually moves the fish… return le. execute(new ILocal. Env. Visitor() { public Object empty. Case(ILocal. Env h, Object nu) { ILambda move. Cmd = new Move. Lambda(le); return unblocked. apply(move. Cmd); … and } execute the unblocked public Object non. Empty. Case(ILocal. Env h, Object nu) { command return blocked. apply(null); } }, null); } … or execute the blocked command
Design Patterns for Decoupling MVC Decouples a model from its view VISITOR Decouples structure and its behaviors. OBSERVER-OBSERVABLE Decouples sender and multiple receivers FACTORY Decouples framework from object creation COMPOSITE Decouples a whole from its parts. COMMAND Decouples decisions from actions. INTEGRAL COMPONENTS OF A UNIFIED PEDAGOGY
The Culmination of a Unified OO Pedagogy ~ end of 2 nd semester. MBS Unit Tests Design Patterns Polymorphism Abstract Structure and Behavior
Studying the Case Study White-box Framework: Extension by Subclassing Re-implement as per given specs. Unbounded. Env Re-implement as per given specs. Variant Environment Variant Fish Behavior Simple. Fish
Inheritance vs. Composition Static Behavior! Separate the variants from the invariants + void move() Strategy Pattern + void move()
Comparative study vs. APMBS Abstracted, decoupled design Enhanced capabilities § Replaceable environments § Robustness § Security
It’s Is itall really about Abstraction! Fish? ! !! Learning to abstract the problems at hand master their complexity Why design patterns? Design patterns are expressions of recurring abstractions that embody fundamental computing principles Design patterns are “tangible abstractions” Design patterns empower students to design software that is correct, flexible, extensible and robust.
For more information: § http: //www. exciton. cs. rice. edu/research/SIGCSE 04/ § To download code files: – Username: sigcse 04 – Password: ricembs § E-mail: – dxnguyen@rice. edu – mgricken@rice. edu – swong@rice. edu
User-Environment Interaction Make local AGlobal. Env Class Depending on environment for themake. Edit. Cmd(Point new public whether ILambda p, location of the mouse local environment click IFish. Factory factory) { is empty or not… … either add a final ILocal. Env le = make. Local. Env(p); fish there using the factory return (ILambda)le. execute(new ILocal. Env. Visitor() { public Object empty. Case(ILocal. Env h, Object nu) { return add. Fish(local. Env, factory. create. Fish()); } public Object non. Empty. Case(ILocal. Env h, Object nu) { return edit. Fish(host); } }, null); } … or edit the fish’s local environment
- Slides: 17