On dependent properties: Is this poor Object Oriented Programming?

Hi, I have a class C1 with a dependent property d and another class C2 which inherits from C1.
When property d is called from C2, it naturally goes through C1. I would like the d coming from C2 to be a transformation of the d coming from C1.
What I did is to create an auxiliary function (not a new method) that is called by C1. The function does what C1 would do to get d and then checks whether the input is a C2 object in which case it transforms d.
Is this bad programming? Would you do it differently?
Thanks,

 採用された回答

Adam
Adam 2014 年 8 月 18 日
編集済み: Adam 2014 年 8 月 19 日

0 投票

I would usually use the idea of a protected function that extends the base class.
So the base class function is of the type:
function d = get.d( obj )
% base class implementation here
d = doGetD( obj, d )
end
with an empty protected implementation of doGetD in the base class and an implementation that extends the base class in the derived class.

2 件のコメント

Patrick Mboma
Patrick Mboma 2014 年 8 月 18 日
編集済み: Patrick Mboma 2014 年 8 月 18 日
Besides the fact that one has to modify the base class, this definitely works. GREAT THANKS !!!
I ended up doing the following
classdef C1
properties(dependent)
d
end
methods
function d=get.d(this)
d=first_pass(this);
d=doGetD(this,d);
end
function d=doGetD(~,d)
end
end
end
classdef C2 < C1
methods
function d=doGetD(~,d)
d=transform(d);
end
end
end
Guillaume
Guillaume 2014 年 8 月 19 日
編集済み: Guillaume 2014 年 8 月 19 日
You really should move your doGetD into a protected block just as in my example.

サインインしてコメントする。

その他の回答 (1 件)

Guillaume
Guillaume 2014 年 8 月 18 日

0 投票

No, it's not very good as your base class C1 now contains implementation that belongs to the derived class C2.
Rather than calling an auxiliary function in C1 d getter, you should call a private class method, that you then override in class C2.

4 件のコメント

Patrick Mboma
Patrick Mboma 2014 年 8 月 18 日
Guillaume, Thanks for the reply. I totally agree with the fact that C1 contains an implementation that belongs to a derived class and that is exactly why I did not like the solution.
I do not, however, understand exactly what you are saying in the rest of your reply. Could you, please, be more explicit? property d whether in C1 or in C2 has to be called with a function with the same name.
Guillaume
Guillaume 2014 年 8 月 18 日
編集済み: Guillaume 2014 年 8 月 18 日
Actually the method you call needs to be protected
classdef C1
properties (Dependent)
d;
end
methods
function value = get.d(this)
value = getdimplementation(this);
end
end
methods (Access = protected)
function value = getdimplementation(this)
%actual implementation of dependent property for C1
end
end
end %end of C1 definition
classdef C2 < C1
methods (Access = protected)
function value = getdimplementation(this)
%actual implementation of dependent property for C2
end
end
end
Patrick Mboma
Patrick Mboma 2014 年 8 月 18 日
Thanks Guillaume,
There is nowhere I can see that d is first processed in C1 and then transformed in C2. Any workaround or maybe I am missing something?
per isakson
per isakson 2014 年 8 月 25 日
編集済み: per isakson 2014 年 8 月 25 日
"There is nowhere I can see" &nbsp Use the "debugger" to step through the code and you will see that it works automagically.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeConfigure and View Diagnostics についてさらに検索

タグ

質問済み:

2014 年 8 月 18 日

編集済み:

2014 年 8 月 25 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by