Object composition and property updating
17 views (last 30 days)
Show older comments
I'm struggling with an implementation of Matlab's OOP and object composition. For simplicity I'll use a car example. Im constructing a of series classes. One is a kind of Master class (like a car) and a series of other classes, (tire, window, door, etc). When I give the car properties using said objects, I'm having trouble updating the properties of the car parts. If for example a property of the car changes AND that change affects its tire property class how should this be handled?
I'm assuming I need use listeners but am concerned it may be overkill for what I am trying to achieve. However I can't wrap my head around this if things get more and more intertwined- Ex: a tire now has a hubcap property that is an object and its color property is dependent on the color of the car and so on and so on.
Any help with this is appreciated and I can provide clarification if necessary but I'm hoping someone will recognize either the flaw in my construction or what approach best handles this. Thanks!
%define a car
classdef car < handle
properties
tire
door
window
paintcolorOfCar
end
%In another file Define a tire
classdef tire < handle
properties
carAttachedTo % I'm setting this property to the car object
%but think that may be a badthing
tireColor
end
methods
function colorout = get.tireColor(obj)
colorout = obj.carAttachedTo.paintcolorOfCar ;
end
0 Comments
Accepted Answer
Jacob Halbrooks
on 10 Apr 2012
You should think about whether the tire needs to be exposed as a public property on the car, or whether it can be private. In any case, one approach for your problem is to use SET methods for properties. An example is below, where the car has a public color, which when set updates the car's bodyColor and its tire's paintColor.
classdef Car < handle
properties(Dependent)
color;
end
properties(Access = private)
tire;
bodyColor;
end
methods
function set.color(this, v)
this.bodyColor = v;
this.tire.paintColor = v;
end
function v = get.color(this)
v = this.bodyColor;
end
function obj = Car
obj.tire = Tire;
obj.color = 'black';
end
end
end
classdef Tire < handle
properties
paintColor = 'black';
end
end
Using that approach, the tire doesn't even need to know what car it is attached to. Now if it is the case that you need to have the tire exist separately from the car, another approach would be to have the tire's color dependent upon the car it is attached to:
classdef Car < handle
properties(Dependent)
color;
end
properties(Access = private)
tire;
bodyColor;
end
methods
function set.color(this, v)
this.bodyColor = v;
end
function v = get.color(this)
v = this.bodyColor;
end
function obj = Car(aTire)
obj.tire = aTire;
obj.tire.car = obj;
obj.color = 'black';
end
end
end
classdef Tire < handle
properties
car;
end
properties(Dependent, SetAccess = private)
paintColor = 'black';
end
methods
function v = get.paintColor(this)
v = this.car.color;
end
end
end
2 Comments
More Answers (1)
Jarrod Rivituso
on 10 Apr 2012
I think the way that I would do this is to set the actual properties to be private
properties (SetAccess=private,GetAccess=public)
paintedColorOfCar
end
and then, provide a "set" method that allows external code to modify it.
methods
function setColorOfCar(obj,inputColor)
obj.paintedColorOfCar = inputColor;
%<do any other color-change related tasks
end
end
Then, in the tire class, you could force the user to use the set method in the Car class
t = Tire;
t.carAttachedTo.setColorOfCar('blue');
Also, if your Tire class must have a color property of its own, and it is truly derived from the Car's color, then just provide a get method in the tire class that calculates it
methods
function out = getColorOfTire(obj)
out = obj.carAttachedTo.paintedColorOfCar;
end
end
The benefits of what I've outlined are that it provides one place (the Car) where the color property is stored, and the various other objects provide helpful methods to either modify or interpret that value.
You could also use listeners, but I agree with you that it might not be worth the effort.
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!