I think in this case the implementation of the static interface is pretty good. Define your classes as follows:
classdef ACalculation < handle methods (Abstract,Public,Static) [result] = calculate (data); plot (data, limX, limY); end end classdef Maximum < ACalculation methods (Public,Static) function [result] = calculate (data) %... end function plot (data, limX, limY) %... end end
Then you can write a function that expects an ACalculation type:
function foo(acalc,data) assert(isa(acalc,'ACalculation')); acalc.calculate(data); acalc.plot(data,[100 200]); end
Then create an empty Maximum instance and pass it to foo :
foo ( Maximum.empty(0), [1 2 3]);
If you want to change the calculation method, call
foo ( Minimum.empty(0), [1 2 3]);
When you say that such a pattern will not work, you think as a Java / C # / C ++ developer. But unlike C ++, where the static and virtual keywords cannot coexist, Matlab does not have such a restriction, because everything is executed at runtime, and the "instance" can be empty or an array of n elements.
Andrey Rubshtein
source share