Comments on: Compose Yourself https://blogs.mathworks.com/developer/2015/05/01/composition-over-inheritance/?s_tid=feedtopost Developing, testing, and integrating production grade software using MATLAB. Tue, 04 Apr 2017 20:10:03 +0000 hourly 1 https://wordpress.org/?v=6.2.2 By: Properties of properties » Developer Zone https://blogs.mathworks.com/developer/2015/05/01/composition-over-inheritance/#comment-10793 Tue, 04 Apr 2017 20:10:03 +0000 https://blogs.mathworks.com/developer/?p=151#comment-10793 […] we should create our FancyString class. Note we leverage composition over inheritance (remember?) and we hold on to the "plain" string. However, the constructor passes all values through to the […]

]]>
By: The Diamond of Breadth » Developer Zone https://blogs.mathworks.com/developer/2015/05/01/composition-over-inheritance/#comment-1019 Mon, 08 Jun 2015 19:12:15 +0000 https://blogs.mathworks.com/developer/?p=151#comment-1019 […] 1 May Compose Yourself […]

]]>
By: Ben https://blogs.mathworks.com/developer/2015/05/01/composition-over-inheritance/#comment-986 Fri, 05 Jun 2015 08:13:31 +0000 https://blogs.mathworks.com/developer/?p=151#comment-986 Ah great, thanks for clearing things up! Being foreign to the concept of dependent properties I wasn’t sure if I was missing something important.

]]>
By: Andy Campbell https://blogs.mathworks.com/developer/2015/05/01/composition-over-inheritance/#comment-955 Wed, 03 Jun 2015 14:43:41 +0000 https://blogs.mathworks.com/developer/?p=151#comment-955 Hey Ben,

Yes you are correct. The comment about gaining the benefits of vectorization apply to *any* properties as you demonstrate. The original point I was making is that the C++/Java approach adds explicit getLength/setLength methods regardless of whether the class has a length field that is stored or calculated (i.e. a Dependent property). Not everyone fully realizes the power of using properties and property set/get methods with MATLAB this often results in people writing MATLAB code that follows ingrained practices of other languages. The reasons for these patterns on other languages do not apply with MATLAB and they can hinder the strengths of using MATLAB.

In short using the property based approach is what I was saying was important, and making them dependent is what applied to that specific property due to the delegation that it demonstrates.

]]>
By: Ben https://blogs.mathworks.com/developer/2015/05/01/composition-over-inheritance/#comment-954 Wed, 03 Jun 2015 13:31:29 +0000 https://blogs.mathworks.com/developer/?p=151#comment-954 It’s still not quite clear to me where the benefits of having a dependent property come into play in terms of being able to use Matlab’s vectorized operations.
Say I had the following class (without dependent properties)

classdef Spaceship

    properties
        Length;
        Weight;
    end

    methods
        function value = get.Length(obj)
            % ... some getter logic
            value = obj.Length;
        end
        
        function obj = set.Length(obj, val)
            % ... some setter logic
            obj.Length = val;
        end
    end

end

of which I created an array and set ‘Length’ for every element. Now I can also use vectorized operations such as

[spaceships([spaceships.Length]>3).Weight] = deal(10);
]]>
By: Andy Campbell https://blogs.mathworks.com/developer/2015/05/01/composition-over-inheritance/#comment-940 Tue, 02 Jun 2015 15:39:23 +0000 https://blogs.mathworks.com/developer/?p=151#comment-940 Hey Ben,

Sure thing. The key point to remember is that MATLAB is fundamentally an array based language and this applies to arbitrary objects just like it does to native data types like doubles/logicals, etc. The powerful vectorized/indexing operations I speak of can be demonstrated by looking at the array case.

For example, with the example we are working with here lets say above we have an array of Planets. Lets add a Name property and create an array of these using preallocation:

        function planets = Planet(names)
            if nargin < 1
                % Allow a zero argument constructor to allow preallocation. When creating
                % an array each element will be constructed through this path.
                return 
            end
            names = cellstr(names);
            planets(numel(names)) = Planet; % Let MATLAB preallocate
            [planets.Name] = names{:}; % Now lets assign all the names
        end

Now we can leverage MATLAB's array nature to quickly and conveniently create the whole solar system!

>> planets = Planet({'Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune'})

planets = 

  1x8 Planet array with properties:

    Name
    Moons
    Radius
    Mass

>> 

Alright now lets assume that we have further initialized these planets so that their radii are correct. Now we can do some fun stuff with just the big ones. This leverages vectorization across the get method of Radius (the dependent property) as well as the set method of Moons if it is defined:

planets([planets.Radius] > 1e7).Moons = createLargeMoons

The above call accesses the get method of Radius for every element and we can easily turn that into a logical vector to actually modify a subset of the planet array, going through the set method of Moons. This is something that would be quite difficult to do when using C++ style setters/getters. Make sense?

Also, a bit more info here.

]]>
By: Ben https://blogs.mathworks.com/developer/2015/05/01/composition-over-inheritance/#comment-864 Thu, 28 May 2015 07:40:12 +0000 https://blogs.mathworks.com/developer/?p=151#comment-864 Hi Andy,
Thanks for putting in the work for this blog!

Could you elaborate on the sentence (and maybe give an example):
“Using Dependent properties for this operation in MATLAB as opposed to writing C++ or Java style set/get methods is very important because it allows powerful MATLAB indexing and vectorized operations on the property.”

]]>
By: Andy Campbell https://blogs.mathworks.com/developer/2015/05/01/composition-over-inheritance/#comment-656 Mon, 11 May 2015 21:40:33 +0000 https://blogs.mathworks.com/developer/?p=151#comment-656 Hi Julian,

Sorry for the delayed response!

Thanks for passing along that FEX submission, it does indeed look like a good example of this principle.

Sometimes people forget (myself included!) that you can benefit from other code in real concrete ways while still defining your own interface and allowing change down the road. For example, because Nicholas used composition, he can easily switch out the implementation of his class to use something other than table in the future. In fact, for your part, (and aided a little bit by hindsight which is not really fair!) you can imagine that in an alternate world where you used composition of datasets you might have been able to easily migrate your code away from datasets and onto tables easily by composing first datasets and then changing the implementation later to use tables when available. You could even have an adapter layer in place which used tables if the MATLAB version supported it but used datasets if not (and stats was available). Make sense?

Hindsight is 20/20 though, things certainly don’t always end up so nice and tidy.

Thanks again for the insightful comment. Good luck!
Andy

]]>
By: Julian https://blogs.mathworks.com/developer/2015/05/01/composition-over-inheritance/#comment-579 Wed, 06 May 2015 08:46:53 +0000 https://blogs.mathworks.com/developer/?p=151#comment-579 Thanks for this post which has got me thinking a bit. A while back I came across this interesting contribution to the FeX
DataFrame
by Nicholas. If I am right it seems to me to be a great practical example of the principles you describe, but both you & Nicholas are somewhat in front of me here!
I really like the idea of being able to extend the Table class in the way that Nicholas describes (just as soon as I get the chance to upgrade my daily use MATLAB to a version that supports tables, and migrate my extensive code-base away from dataset arrays!)

]]>
By: Andy Campbell https://blogs.mathworks.com/developer/2015/05/01/composition-over-inheritance/#comment-536 Sat, 02 May 2015 03:55:07 +0000 https://blogs.mathworks.com/developer/?p=151#comment-536 Thanks for the kudos Kevin!

]]>