Hello everyone,
I am new to object oriented programming and have some issues getting the behavior of my class in the way I want it. I have several input properties and a "dependent" output property. Because the algorithm to compute the output is numerically intensive, I do not want to handle it as an actual dependent property. It should only be updated if necessary. Also I want to be able to save my results to my hard drive, which would not work with a dependent property.
I got this basically working if my class is a handle class as shown in the example down below. But I do not like how handle classes function. In particular it can be very unintuitive to the user if they try something like this:
A = myclass(1,2); B = A; A.input1 = 5;
So here is my example with the handle class. Can anyone tell me if that can also be achieved with a value class? General improvement suggests are welcome as well 🙂
Thanks a lot in advance!
classdef myclass < handle properties input1 input2 output update_required end methods function obj = myclass(value1, value2) obj.input1 = value1; obj.input2 = value2; obj.output = []; end function set.input1(obj, value) if obj.input1 == value return end obj.input1 = value; obj.update_required = true; end function set.input2(obj, value) if obj.input2 == value return end obj.input2 = value; obj.update_required = true; end function value = get.output(obj) if obj.update_required disp('Update required.') obj.output = obj.input1 + obj.input2; obj.update_required = false; else disp('No update required.') end value = obj.output; end endend
Best Answer