Python Forum
Replacing model in MV(C) application - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Replacing model in MV(C) application (/thread-24653.html)



Replacing model in MV(C) application - Oolongtea - Feb-25-2020

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:
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 whatever
Models are bound the following way, possibly to multiple views.
model = Observable()
view1 = View()
view2 = View()
view1.bind_model(model)
view2.bind_model(model)