Software Design Spring 2008 For today: 1) design revision 2) quiz For next time: 1) read the handout from Domain-Driven Design http://wb/sd/handouts/evans_ch01.pdf 2) Read the Wikipedia page on the Observer pattern (below) 3) Register for Computational Modeling Quiz solution ------------- from TurtleWorld import * from RemoteObject import * class RemoteTurtleWorld(RemoteObject): def __init__(self, name): RemoteObject.__init__(self, name) self.world = TurtleWorld() def quit(self): self.stopLoop() self.join() World.quit(self.world) # or maybe self.world.quit() def run_message(self, message): self.world.inter.run_code(message, '') def main(script, name='bob'): world = RemoteTurtleWorld(name) world.threadLoop() world.mainloop() if __name__ == '__main__': main(*sys.argv) As an exercise, draw class and object diagrams for both versions. Computational Modeling ---------------------- Go to the Swampy directory and run TurmiteWorld.py "Turmite" is a deliberate misspelling of termite as a tribute to Alan Turing. My TurmiteWorld is an implementation of a cellular automaton sometimes called Langton's Ant. Here are the Ant Rules: def step(self): """this step function implements the rules for Langton's Ant (see http://mathworld.wolfram.com/LangtonsAnt.html) """ cell = self.get_cell() if cell.is_marked(): self.lt() else: self.rt() cell.toggle() self.fd() Simple world, simple rules, simple outcome, right? Computational Modeling is about discrete models of physical systems, including: 1) graphs 2) cellular automata 3) random (stochastic) models 4) agent-based simulations These topics fall under the general heading of Complexity Science http://en.wikipedia.org/wiki/Complex_adaptive_system One of the major themes of the class is Emergence http://en.wikipedia.org/wiki/Emergence The course will be a little different from last time, but you are welcome to check out the material at http://wb/cm Tuesday Friday 10-12. Subject/Observer Pattern ------------------------ http://en.wikipedia.org/wiki/Observer_pattern "The essence of this pattern is that one or more objects (called observers or listeners) are registered (or register themselves) to observe an event that may be raised by the observed object (the subject). (The object that may raise an event generally maintains a collection of the observers.)" In network program, this pattern is useful if many clients need to maintain shared state (like, for example, the state of a game). There are three roles an object might play: Subject: the object that contains the shared state. Observer: one of the objects that needs to maintain a view of the state. Modifier: an object that causes the state to change. You can download an implementation using RemoteObjects from wb/sd/code: Subject.py Observer.py Modifier.py In a distributed game, you probably want one server running a Subject that contains all game state. Then each player runs a client that contains both Observer and Modifier code.