Tiny discussion on good practices in object oriented programming (OOP) through a simple example

17 visualizaciones (últimos 30 días)
Hi guys,
I'm interested in finding more about good practices of the OOP programming and titles of decent books for non-programmers is also welcome.
Let's consider the following example:
- Class Point with some properties and constructor with input arguments.
Class Point
classdef Point
properties(Access = public)
ID
X
Y
end
methods(Access = public)
function obj = Point(id, x, y)
% Constructor
obj.ID = id;
obj.X = x;
obj.Y = y;
end
end
end
- Class Points with Items property that should contain Point objects, and methods such as to AddPoint objects
classdef Points
properties(Access = public)
Item = Point()
Count = 0
end
methods(Access = public)
function obj = Points()
%
end
function obj = AddCount(obj)
obj.Count = obj.Count + 1;
end
function obj = AddPoint(obj, id, x, y)
obj = obj.AddCount()
obj.Item(obj.Count) = Point(id, x, y)
end
% function obj = AddPoint(obj, id, x, y)
% obj.Item(1) = Point(id, x, y)
% end
end
end
The questions are, how should class Points be structured:
  1. Should I use method AddPoint within Points to return a Point object? If yes, how should I get Point object within Points class? Do I initialize private property as instance of Point and assign the object to it, or is there another better way of doing this? There will be a lot of Point objects so an array of objects should be created beforehand.
  2. As you can see, there is an Item property in Points class, is it common (in simple programs) to assign an object to property then invoke it's methods by dot notation on assigned object? I am aware that there are access lists, but it is a little bit complicated for me at the moment.
I believe answers are subjective and dependent on the complexity of the program (not so complex),therefore I am open to hear your ideas.

Respuestas (2)

Steven Lord
Steven Lord el 17 de Sept. de 2021
First, having two classes whose names differ only by casing or by the difference between singular and plural seems likely to cause confusion if you mistype.
Second, what's the benefit of having the Points class here over simply having a vector of Point objects?
>> P = Point('Boston', 10, 7)
P =
Point with properties:
ID: 'Boston'
X: 10
Y: 7
>> P(2) = Point('New York', 9, 6.5)
P =
1×2 Point array with properties:
ID
X
Y
>> P(3) = Point('Los Angeles', 1, 3)
P =
1×3 Point array with properties:
ID
X
Y
>> NY = P(2)
NY =
Point with properties:
ID: 'New York'
X: 9
Y: 6.5
>> numel(P)
ans =
3
  1 comentario
Mario Malic
Mario Malic el 17 de Sept. de 2021
Editada: Mario Malic el 17 de Sept. de 2021
I agree on the first statement.
About the second one: well, the idea is from the Excel, which has complicated COM object unlike mine. Points class would just be collection of Point objects. Maybe they have this collection class due to used programming language. I'll consider to put everything in a single class and simplify things a bit.

Iniciar sesión para comentar.


per isakson
per isakson el 20 de Sept. de 2021
Editada: per isakson el 30 de Sept. de 2021
"[...]titles of decent books for non-programmers"
AFAIK: There is no book on object oriented design and programming with Matlab examples. However, there are many with Python. Python OO is close enough to Matlab OO to make a Python book useful. With a little Visual Basic background I once worked through most of Head First Design Pattern (Java examples). I found that useful, however today I would look for a Python book, since Python is "dynamic" like Matlab. (See Design Patterns in Dynamic Programming by Peter Norvig.)
I asked google for "a good beginners book on object oriented Python" and received links to several useful reviews.
"Should I use method AddPoint within Points to return a Point object? [...] is there another better way"
There is too little context to answer this question. It depends on how you are going to use these classes. I cannot guess. Why the properties Item and PointObject? Are they scalars? The names are singular.
properties(Access = public)
Item = Point()
Count = 0
PointObject = Point()
end
obj.Item( 1 ) the value of the index (ONE) is that a typo?
obj.Item(1) = Point(id, x, y);
"There will be a lot of Point objects" Be careful, operating on thousands of simple point object can turn out to be very slow with Matlab.
"is it common (in simple programs) to assign an object to property then invoke it's methods by dot notation on assigned object" Yes, that's common. Added later: But be aware of the train wreck anti pattern
  2 comentarios
Mario Malic
Mario Malic el 20 de Sept. de 2021
Editada: Mario Malic el 20 de Sept. de 2021
Thank you Per for your suggestions.
I will take a look on this presentation and will look for beginner books on OOP in Python.
The reason why I created these two different classes, is partially because of the slowness. When I had Point class, creating object one by one make MATLAB slow as expected. Since I want these objects to be in an array, I wanted to initialise as many as I need and then afterwards set their properties which is obviously a lot faster. Because it's a vector, one can use numel to check the number of elements, but since it counts the empty elements as well, there's no use of numel in this case. I couldn't think of way of indexing into an array other than wrapping it up into another class whose objective is to hold on to the Point objects and with help of Count property to correctly index into an array of Item.
"obj.Item( 1 ) the value of the index (ONE) is that a typo?"
I updated the code for the Points class and now should be more understandable what was my intention.
Other than Points, I will also deal with Line(s) class(es) which contain Point objects, Surface(es) class(es) which contain Line objects etc etc. It's getting nasty preeeeeety quickly. 😁
per isakson
per isakson el 21 de Sept. de 2021
" It's getting nasty preeeeeety quickly." This is not a beginners exercise. Find out how others solved the task.

Iniciar sesión para comentar.

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by