필터 지우기
필터 지우기

How to communicate among objects from different classes in a solid way?

조회 수: 3 (최근 30일)
Xh Du
Xh Du 2017년 3월 14일
댓글: Xh Du 2017년 3월 14일
Hi all,
This is a really long question, thanks for your patience!
Imagine I have two classes like this:
classdef beam < handle
properties
stiff_mtx1
stiff_mtx2
stiff_mtx
end
methods
function obj = beam(stiff_mtx1, stiff_mtx2)
obj.stiff_mtx1 = stiff_mtx1;
obj.stiff_mtx2 = stiff_mtx2;
end
end
end
and
classdef parameter < handle
properties
pm1
pm2
end
methods
function obj = parameter(pm1, pm2)
obj.pm1 = pm1;
obj.pm2 = pm2;
end
end
end
and a main script to start objects:
clear; clc;
stiff_mtx1 = % a stiffness matrix.
stiff_mtx2 = % another stiffness matrix with same size.
myBeam = beam(stiff_mtx1, stiff_mtx2);
pm1 = 5;
pm2 = 10;
myPm = parameter(pm1, pm2);
So far so good, but then I want 'stiff_mtx' in beam class to be obtained like this:
myBeam.stiff_mtx = myBeam.stiff_mtx1 * myPm.pm1 + myBeam.stiff_mtx2 * myPm.pm2;
Which I tested, it works. obviously this is a communication between beam class and parameter class, and property 'stiff_mtx' is assigned after this line of command. My question is: is this the SOLID way to allow objects communicate with each other? I read in book saying in OOP, inheritance should be avoided while composition is recommended, which I agree. In the above example I could define pm1 and pm2 as properties of beam, which results in one beam class only, but I believe this is not SOLID because: 1. beam is obviously different from parameter; 2. simply adding properties in one class makes a class unnecessarily complex, which violates the SOLID principle.
I'm not sure how to allow communication among different objects, and is it SOLID to define different classes and related objects, make them communicate, or just define one big class and create one object?
Also the above line
myBeam.stiff_mtx = myBeam.stiff_mtx1 * myPm.pm1 + myBeam.stiff_mtx2 * myPm.pm2;
can it be defined as a method and put in a class? My thought is this line can be defined as a function and can be called when needed, and my program becomes a combination of OO classes and traditional functions, is this SOLID?

채택된 답변

Adam
Adam 2017년 3월 14일
편집: Adam 2017년 3월 14일
I'm rusty on my SOLID principles and don't tend to have time to get back up to date so I tend to just go with intuition instead. One simple rule of thumb though for working out where something should go is to look at what objects are being used in the expression. If one object is having its properties referred to often (more often than others) then it immediately jumps to the front of the queue as a candidate to have that function as a member of its class.
So in your case, if you have decided that beam and parameter are two distinct objects then I would add that function to the beam class and pass it the parameter object since it sets a property of beam based on two other properties of beam. Then, if those other two properties of beam are not used externally they can be made private for better encapsulation e.g.
function calculateStiff_mtx( obj, myParam )
validateattributes( myParam, { 'parameter' }, { 'scalar' } ) % Optional fussiness - if performance is crucial and this is called very frequently don't include this
obj.stiff_mtx = obj.stiff_mtx1 * myParam.pm1 + obj.stiff_mtx2 * myParam.pm2;
end
I would strongly advise you to come up with more meaningful names though unless 'parameter' is contained within a package which gives it context.
  댓글 수: 5
Adam
Adam 2017년 3월 14일
That sounds fine. Then just inject it into whichever classes require it when they do so.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Software Development Tools에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by