Feb-25-2020, 02:03 PM
I am building a program using what is hopefully an MV architecture (see example below).
The main data structure in my program is bound to a number of views, and observed by a number of objects. I am confused at what is the best strategy to completely replace the data I am working with (new file load for example).
Should I be working with the same instance and clear and replace the data within the object (meaning that if an error occurs it may be complicated got back to the previous state)
or should I create a new instance and somehow transfer the bindings from the old instance to the new? (I don't see a simple and clean way of achieving this)
any suggestions?
The models are based on the class below:
The main data structure in my program is bound to a number of views, and observed by a number of objects. I am confused at what is the best strategy to completely replace the data I am working with (new file load for example).
Should I be working with the same instance and clear and replace the data within the object (meaning that if an error occurs it may be complicated got back to the previous state)
or should I create a new instance and somehow transfer the bindings from the old instance to the new? (I don't see a simple and clean way of achieving this)
any suggestions?
The models are based on the class below:
class Observed: def __init__(self): self._observers = [] self._mutex = False def attach(self, obs): if obs not in self._observers: self._observers.append(obs) def detach(self, obs): if obs in self._observers: self._observers.remove(obs) def notify(self, event): for obs in self._observers: obs.handle_event(event, self)the views implement the following methods
class View: def bind_model(self, model): self.model = model self.model.attach(self) # do update def handle_event(event, origin): # do whateverModels are bound the following way, possibly to multiple views.
model = Observable() view1 = View() view2 = View() view1.bind_model(model) view2.bind_model(model)