I'm trying to create a simple Matlab GUI which displays some data from an external sensor on potentially different views. The goal is to implement some kind of MVP (Model View Presenter) pattern. However, some questions occured. First a short description of my classes:
classdef (Abstract)IView < handle
properties (Abstract)
% define required properties, to be implemented by subclass
ConnectedSensorSerialnumbers
SensorsAreConnected
...
end
events
% define some required events
ConnectButtonPressed
...
end
methods (Abstract)
end
end
A concrete View could look like
classdef ViewAppDesigner < IView % implement the interface
properties (private)
ViewInstance matlab.apps.AppBase
end
properties
ConnectedSensorSerialnumbers
SensorsAreConnected
end
events
ConnectButtonPressed
end
methods
% ctor
function obj = ViewAppDesigner ()
% create View made by Appdesigner, a GUI tool
obj.ViewInstance = CreateAppDesignerGui();
end
% bind the events to some GUI elements, notify if something changed etc.
...
end
end
A presenter could look like
classdef Presenter < handle
properties
Model IModel = DefaultModel();
View IView = DefaultView();
end
methods
function obj = Presenter(view, model)
% input error checks etc.
...
% make model and view known to Presenter
obj.Model = model;
obj.View = view;
obj.registerViewCallbacks();
end
function registerViewCallbacks(obj)
addlistener(obj.View, 'ConnectButtonPressed', @obj.onConnectButtonPressed);
...
end
function onConnectButtonPressed(obj, src, evt)
obj.Model.connectSensor
if obj.Model.IsConnected
obj.View.ConnectedSensorSerialnumbers = obj.Model.GetListOfConnectedSensors.SerialNumber;
obj.View.SensorsAreConnected = true;
else
% trigger some error message
end
end
end
end
For clarity I did not write down the Model + Interface.
However, these are my questions:
-
In this implementation the View is decoupled by an Interface. In the classical MVP pattern each View belongs to exactly one Presenter. So the Presenter could use the View's internals. Why do we need an Interface then?
-
The above approach feels not right, if we used an appropiate Interface one Presenter could handle multiple Views, because it is decoupled. For example one could create a second View showing the data on a console. The Interface remains the same, even the Presenter remains the same, but the View has to include some logic. For example SensorsAreConnected could be a green lamp in the GUI implementation or a plain text message in a console View. But that would require the concrete View knows how to display that information. Is that design okay or has the View to be as dump as possible? That would lead to #1 and we don't need an Interface anymore.
In the end, is some logic in the View allowed and can we use multiple Views in one Presenter?
Thank you very much!
Aucun commentaire:
Enregistrer un commentaire