Z
Z
zemka2010-12-30 19:45:19
Perl
zemka, 2010-12-30 19:45:19

How to use OOP correctly?

Hello. I recently sat down to learn Perl, but that's not the point. I'm self-taught in programming, and I have a problem, I don't really understand how to use OOP correctly.
Here is an example, there is a pricer.pm module that has a set of methods for calculating any price values ​​for an item.
For example, the calc_total() function.
The question torments me is how to use this module correctly.
By creating a new object that is like this
$obj = pricer->new( $item_id );
$obj-&gt;calc_total();<br/>
or
$obj = pricer-&gt;new( );
$obj-&gt;calc_total( item_id =&gt; $item_id )
I ask you not to kick much, but to help a novice programmer.
In theory, the first option is correct, but the second is also not so bad?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
R
Rafael Osipov, 2010-12-30
@Rafael

I don't write in Perl, more in Java, but I'll give you a hint.
If you use only one item_id, then both options are valid, but the first one is definitely more elegant.
If you have a lot of item_id, then the second option is preferable if you need to calculate the sum of the results for the item_id set. In order not to produce a lot of pricer objects.
If I'm not mistaken in my assumptions about your task.

V
v1z, 2010-12-30
@v1z

If your pricer module is tied to item_id, or if half of its methods require this id, then the first option is fine. If the ID requires only the calc_total method, then it is better to use the second option.

A
Alexander Belugin, 2010-12-30
@unkinddragon

Depends on the essence you want to invest.
For example, if this object is a "model" and gives access to operations in the database with all sorts of items, then the second option is out. And if an object can work with only one item, then it is logical to initialize it with it (the first option).

P
perl_demon, 2010-12-30
@perl_demon

Perl has a rule/expression - There Is More Than One Way To Do It!
Your example demonstrates this well :-)
My opinion: Correct and so and so!

T
TimTowdy, 2010-12-30
@TimTowdy

Judging by the fact that you care about such a question, your module is relatively small, and both options are acceptable for it. When/if your pricer.pm gets big enough that there is a need for OOP, the right option will become obvious to you.
In general, it would be better to do this:

$item->calc_total_price();

Where calc_total_price would look something like this:
my $item = shift;
return $pricer->calc_total( item_id => $item->{id});

Those. a special object (or function) that calculates item data should not be used explicitly, but inside the Item class method. But this is all a theory, which few people get their hands on in pearl.
At once I want to warn you against studying OOP on an example of a pearl. It is done terribly there and is only partially corrected with crutches. You run the risk of injury for life, like GOTO programmers in BASIC. To better understand what OOP is and how to use it, see how it is implemented in Java/C++, Python/Javascript, LISP/Scheme.

I
Ivan, 2010-12-30
@iSage

If item_id is not used anywhere else except in calc_total, then the second option is better. Otherwise, the first one.
The constructor is usually passed some fields common to the entire object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question