Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
object oriented programming, class, subclass communication

Subject: object oriented programming, class, subclass communication

From: Roland

Date: 1 Jun, 2012 16:30:25

Message: 1 of 6

my scripts get very messy and I realised I have to rewrite my code in an object oriented manner to manage complexity. And now I am stuck at a very very basic level....

I have an object, which consists of some other objects. I have no Idea how these objects can communicate with each other.

lets say, i have a car, my car has a tire and an engine.

property of my car is velocity,
property of tire is radius,
property of engine is number of revolutions per second

I want to calculate the velocity of the car, which is something like radius of tire times revolutions per time of engine.

so I would write

classdef car
   properties
      velocity
   end
   methods
      function obj=calculateVelocity(obj)
          % something like radius of tire times revolutions per time of engine
          % but how does my car class know about these properties of the subclasses????
      end
   end
end

classdef engine < car % is subclass of car
   properties
      radius=1;
   end
end

classdef tire < car
   properties
      U_s=1000;
   end
end
 
I would appreciate very much if someone can help me getting started...

Subject: object oriented programming, class, subclass communication

From: Steven_Lord

Date: 1 Jun, 2012 17:01:16

Message: 2 of 6



"Roland " <burgmann@gmx.de> wrote in message
news:jqaqn1$9h2$1@newscl01ah.mathworks.com...
> my scripts get very messy and I realised I have to rewrite my code in an
> object oriented manner to manage complexity. And now I am stuck at a very
> very basic level....
>
> I have an object, which consists of some other objects. I have no Idea how
> these objects can communicate with each other.
> lets say, i have a car, my car has a tire and an engine.

So the tire and engine should be properties of the car class, right?

> property of my car is velocity,
> property of tire is radius,
> property of engine is number of revolutions per second
>
> I want to calculate the velocity of the car, which is something like
> radius of tire times revolutions per time of engine.
> so I would write
>
> classdef car properties
> velocity

You probably want to make this a Dependent property:

http://www.mathworks.com/help/techdoc/matlab_oop/f2-74190.html#f2-85430

> end
> methods
> function obj=calculateVelocity(obj)

and change the name of this to get.velocity to make it an accessor method
for the Dependent property, like the Modulus property example on the page to
which I linked above.

> % something like radius of tire times revolutions per time of
> engine
> % but how does my car class know about these properties of the
> subclasses????

If those properties were Constant properties of the classes, just ask for
them.

http://www.mathworks.com/help/techdoc/matlab_oop/bso_zpq-1.html

This would require tire to be:

classdef tire
    properties(Constant)
        % Access this property using "tire.radius"
        radius = 1;
    end
end

and you would need to make a similar change to engine.

If those properties are properties of specific instances of those objects
that are included in the car class, ask those instances for the values of
those properties.

classdef car
    properties(Dependent)
        velocity
    end
    properties
        thetire = tire(); % Use the default tire constructor to provide the
default thetire
        theengine = engine();
    end
    methods
        function v = get.velocity(obj)
            v = thetire.radius*theengine.itsproperty;
        end
    end
end

> classdef engine < car % is subclass of car

No. An engine is not substitutable for a car, so it shouldn't be a subclass.

*snip*

> classdef tire < car

Ditto.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: object oriented programming, class, subclass communication

From: Roland

Date: 2 Jun, 2012 15:21:30

Message: 3 of 6

thanks steve, I was not aware that a property of an object can be an object itself. that makes everything easy! I thought i have to deal with subclasses...

now, when e.g. the radius of the tire changes, I want to update the velocity of the car. Do I have to use events in this case or is there another method. I know, i could make the velocity a dependent property, but having a lot of dependent properties makes the code slow. it seems like it is calculating dependent properties very often.
I have some strange results when using events and listeners. as soon as I use listeners, it seems objects can not be removed any more from workspace...

Subject: object oriented programming, class, subclass communication

From: Steven_Lord

Date: 4 Jun, 2012 14:42:50

Message: 4 of 6



"Roland " <burgmann@gmx.de> wrote in message
news:jqdb1q$1gc$1@newscl01ah.mathworks.com...
> thanks steve, I was not aware that a property of an object can be an
> object itself. that makes everything easy! I thought i have to deal with
> subclasses...
>
> now, when e.g. the radius of the tire changes, I want to update the
> velocity of the car. Do I have to use events in this case or is there
> another method. I know, i could make the velocity a dependent property,
> but having a lot of dependent properties makes the code slow. it seems
> like it is calculating dependent properties very often. I have some
> strange results when using events and listeners. as soon as I use
> listeners, it seems objects can not be removed any more from workspace...

I would expect, from your description, that the velocity of the car only
changes when you change its tires or its engine. If so, change the SetAccess
for those properties to 'protected' or 'private' so that only car methods
can modify them, then write methods (changeTire, changeEngine) that replace
the tires or engine respectively and then recomputes the value of the car's
velocity property. That way you only recompute when the tires or engine
actually change. Alternately just write set.tire and set.engine methods to
replace the tires or engine and then recompute the velocity.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: object oriented programming, class, subclass communication

From: Roland

Date: 8 Jun, 2012 08:29:20

Message: 5 of 6

"Steven_Lord" <slord@mathworks.com> wrote in message <jqihha$k2h$1@newscl01ah.mathworks.com>...
>
>
> "Roland " <burgmann@gmx.de> wrote in message
> news:jqdb1q$1gc$1@newscl01ah.mathworks.com...
> > thanks steve, I was not aware that a property of an object can be an
> > object itself. that makes everything easy! I thought i have to deal with
> > subclasses...
> >
> > now, when e.g. the radius of the tire changes, I want to update the
> > velocity of the car. Do I have to use events in this case or is there
> > another method. I know, i could make the velocity a dependent property,
> > but having a lot of dependent properties makes the code slow. it seems
> > like it is calculating dependent properties very often. I have some
> > strange results when using events and listeners. as soon as I use
> > listeners, it seems objects can not be removed any more from workspace...
>
> I would expect, from your description, that the velocity of the car only
> changes when you change its tires or its engine. If so, change the SetAccess
> for those properties to 'protected' or 'private' so that only car methods
> can modify them, then write methods (changeTire, changeEngine) that replace
> the tires or engine respectively and then recomputes the value of the car's
> velocity property. That way you only recompute when the tires or engine
> actually change. Alternately just write set.tire and set.engine methods to
> replace the tires or engine and then recompute the velocity.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

okay, I think to understand. The 'base' object car should be in charge of changing ALL properties, even if a property of car is an object by itself. Like that, the 'base' object has full control and knows, which properties have changed and which have to be recalculated.
thank you for the explanation.

Subject: object oriented programming, class, subclass communication

From: Steven_Lord

Date: 8 Jun, 2012 14:06:15

Message: 6 of 6



"Roland " <burgmann@gmx.de> wrote in message
news:jqsd50$k67$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <jqihha$k2h$1@newscl01ah.mathworks.com>...
>>
>>
>> "Roland " <burgmann@gmx.de> wrote in message
>> news:jqdb1q$1gc$1@newscl01ah.mathworks.com...
>> > thanks steve, I was not aware that a property of an object can be an
>> > object itself. that makes everything easy! I thought i have to deal
>> > with subclasses...
>> >
>> > now, when e.g. the radius of the tire changes, I want to update the
>> > velocity of the car. Do I have to use events in this case or is there
>> > another method. I know, i could make the velocity a dependent
>> > property, but having a lot of dependent properties makes the code slow.
>> > it seems like it is calculating dependent properties very often. I have
>> > some strange results when using events and listeners. as soon as I use
>> > listeners, it seems objects can not be removed any more from
>> > workspace...
>>
>> I would expect, from your description, that the velocity of the car only
>> changes when you change its tires or its engine. If so, change the
>> SetAccess for those properties to 'protected' or 'private' so that only
>> car methods can modify them, then write methods (changeTire,
>> changeEngine) that replace the tires or engine respectively and then
>> recomputes the value of the car's velocity property. That way you only
>> recompute when the tires or engine actually change. Alternately just
>> write set.tire and set.engine methods to replace the tires or engine and
>> then recompute the velocity.
>
> okay, I think to understand. The 'base' object car should be in charge of
> changing ALL properties,

IMO the car object should manage its direct properties, which in this case
are "tire", "engine", and "velocity". It should not IMO try to "micromanage"
the properties of its properties; let the tire and engine objects handle
those.

> even if a property of car is an object by itself. Like that, the 'base'
> object has full control and knows, which properties have changed and which
> have to be recalculated.

If you want the car not to need to know that the velocity depends on the
tire and the engine, then just recompute the velocity every time the
property is queried. Then the only function that would need to know upon
what information the velocity depends is the function that computes the
velocity itself.

If you want to cache the velocity information so you don't need to recompute
it each time, you need to know when to update that cached information and
that's why the car (or more specifically the function that updates the
properties upon which velocity depends) needs to know about those
dependencies.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us