I have a class displaying a figure that depends on a parameter. Each time the parameter is changed, the figure has to be redrawn. My code looks like this:
classdef Foo < handleproperties (SetObservable, AbortSet)paramendmethodsfunction set.param(obj, param)obj.param = paramobj.draw()endfunction draw(obj)% draw stuffendendend
This works fine. However, I would like to be able to add subclasses of Foo that prepare other things to be displayed when param is changed. This needs to happen before the call to draw. Now I am wondering how to do that as cleanly as possible. Here is what I tried and why it doesn't work:
- Listening to the 'PostSet' event does not work because it is triggered after the call to draw.
- Triggering a custom even in set.param before the call to draw is not allowed, only standard event are allowed in set/get methods.
- Setting up 'draw' as a callback on 'PostSet' does not work because callbacks cannot be ordered. In fact, it is specified that callbacks are called synchronously.
- My current "least-evil"solution is to add another function called set_param:
classdef Foo < handleproperties (SetObservable, AbortSet, SetAccess=protected)paramendmethodsfunction set.param(obj, param)obj.param = paramendfunction set_param(obj, param)obj.param = paramobj.draw()endfunction draw(obj)% draw stuffendendend
One minor draw back is that I need to call obj.set_param(param) instead of obj.param = param. One major drawback is that child classes still have access to set.param and therefore may erroneously call obj.param = param instead of obj.set_param. Indeed, if param is set to SetAccess=private, the child class cannot add listeners to that property.
Any idea of a better solution?