Quote:Original post by ToohrVyk
However, must the controller objects necessarily interact with the model through polling? Is there any reason to forbid the Observer design pattern from being used? Listeners? Callbacks?
Not at all. Think of separate GameModel and GameModelData objects. The GameModel object applies changes to the data, updates observers, etc. It's only the data itself that gets saved.
Say you have AI methods in Character objects. Whatever is calling those methods (eg, GameModel::run) should be a publisher, not the Character itself (except perhaps in an intra-model context). This makes sense for a number of reasons, including efficiency, lack of extra complexity, and decoupling.
And just to be clear, pickle handles this kind of pattern without issue:
import pickleclass Observer: def __init__(self, name): self.name = name def update(self, data): print "%s: %s" % (self.name, data)class Publisher: def __init__(self): self.observers = [] def attach(self, o): self.observers.append(o) def notify(self, data): for o in self.observers: o.update(data)class World: def init(self): self.pub = Publisher() self.ob1 = Observer('One') self.ob2 = Observer('Two') self.ob3 = Observer('Three') self.pub.attach(self.ob1) self.pub.attach(self.ob2) self.pub.attach(self.ob3) def do_something(self): self.pub.notify('foo')def test_pickle(): w = World() w.init() fout = file('test.pkl', 'wb') pickle.dump(w, fout) fout.close()def test_unpickle(): fin = open('test.pkl', 'rb') w = pickle.load(fin) w.do_something()test_pickle()test_unpickle()
(edit) Sorry for mixing my terms...Observer/Subject, Publisher/Subscriber, it's all the same anyway.