OOP: objects as properties of objects; objects with no methods; objects with no properties?

3 views (last 30 days)
I am new to programming in general (~3 months experience). So, as an exercise, I am building a set of solvers for different PDES; let's say I am solving the Poisson equation. I'd like to use an object oriented approach. I have three questions about best practices in OOP that I am hoping I can get answers to:
  1. Is it OK to store other objects as properties of an object?
  2. Is it OK if "subobject A" has no properties?
  3. Is it OK if "subobject B" has no methods?
An example might help illustrate: My basic object is a problem object, with class definition like this:
classdef problem
domain % object
boundaryCondition % object
sourceFunction % function handle
solver % object
result % matrix
% constructor
function problem = problem(domain,bc,source)
if nargin > 0
problem.domain = domain;
problem.boundaryConditions = bc;
problem.sourceFunction = source;
% set solution method
function problem = setsolver(solver)
problem.solver = solver;
% solve, calls a method also called 'solve', in the chosen solver
function problem = solve(problem)
problem.result = problem.solver.solve;
Here, the properties domain, boundaryConditions, and solver are, themselves, other objects. So Question 1: Is it OK to store other objects as properties of an object?
The idea is to mix and match different versions of these objects to solve different problems. For example, I have written solvers called poissonFE and poissonCCFD, which use different solution methods. BUT neither solver object actually stores any data. Each is just a collection of methods that act on data stored elsewhere in problem. The reason I have written it this way is so I can equip problem with one solution method, solve a problem, then switch to a different solution method and solve the same problem. Long story short, my different solver objects have methods, but no properties. So Question 2: Is it OK if "subobject A" has no properties?
Finally, the boundaryCondition property is, itself, an object; but this object has properties and no methods. The boundaryCondition object stores numerical data like leftEdge = 1 and string data like leftEdgeType = 'Neumann', but it doesn't actually have any methods of its own. I could have used a cell array, but I like that my approach leads to syntax like problem.boundaryConditions.leftEdgeType = 'Neumann' as this statement just seems intuitive to me. In short, I have objects with properties, but no methods. So Question 3: Is it OK if "subobject B" has no methods?
In very short: How many "best principles" am I violating here? I want to be good at this, so any criticisms are much appreciated.

Answers (2)

Chunru on 27 Aug 2022
Edited: Chunru on 27 Aug 2022
Is it OK to store other objects as properties of an object?
Yes. Actually it is common for MATLAB class.
Is it OK if "subobject A" has no properties?
Yes. But it may not be that common for an object has no properties.
Is it OK if "subobject B" has no methods?
Yes. You can use class to manage properties only (which is then similar to struct but has some "advantages" over struct.

Steven Lord
Steven Lord on 27 Aug 2022
Is it OK to store other objects as properties of an object?
Yes, but you will need to be careful in certain situations (most notably using handle objects as properties of another object, and when you initialize those objects.) See this documentation page that discusses some of those situations you need to consider.
Is it OK if "subobject A" has no properties?
Yes. An object that is only a "doer" is allowed in MATLAB. One example of this that I write as part of my day job is a simple test class. [Many test classes also have properties but they don't have to.] If I told the testing infrastructure to run testPlus as a test it will iterate through the Test methods, executing each in turn.
classdef testPlus < matlab.unittest.TestCase
function onePlusOne(testcase)
testcase.verifyEqual(1+1, 2)
% Add additional test cases to qualify the plus function
Is it OK if "subobject B" has no methods?
Yes, a "knower" class is fine. An example of this is an enumeration class (okay, the drinkSizes class below inherits methods defined for int32, but you're not defining any new methods in the class itself.)
classdef drinkSizes < int32
small (12), medium (20), large (32)
Another example is a class with Constant properties. I could use mathConstants.g in my code to define the gravity of Earth in only one place in my code. If I then wanted to switch and use this code to simulate something on Mars I'd only have to modify this one file.
classdef mathConstants
g = 9.8; % m/s/s
c = 299792458; % m/s


Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by