How to use OOP?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
3 views
Hi all. Just started learning Perl, but that's not the point. In programming, a self-taught, and I have a problem I do not really understand how to use OOP.


Here's an example, there are the pricer module.pm which has a set of methods for the calculation of any price value the subject.

For example, the function calc_total().


Question worries me is how to use this module.

Creating a new object which is so
$obj = pricer->new( $item_id );
$obj->calc_total();


or
$obj = pricer->new( );
$obj->calc_total( item_id => $item_id )


Please do not kick, and to help the novice programmer.

The idea is the correct first option, but the second is also not so bad?

Thanks in advance.
by | 3 views

6 Answers

0 like 0 dislike
I don't write Perl, more on Java, but I'll try to tell.
\r
If you use only one item_id, both have the right to life, but the first is definitely more elegant.
\r
If you have a lot item_id, the second option is preferable if you need to calculate the sum of the results for a set of item_id. So as not to produce a set of objects pricer.
\r
If I'm not mistaken in my assumptions about your problem.
by
0 like 0 dislike
If your module is tied to the pricer item_id well, or if half of it methods requires this ID, then the first option is quite suitable. If ID only requires calc_total method, then it is better to use the second option.
by
0 like 0 dislike
Depends on the fact that you want to invest.
For example, if this object is a "model" and gives access to operations with any item of AMI, the second option is out. But if the object is able to work with only one item, it is with him, and to initialize (the first option).
by
0 like 0 dislike
In perl there is a rule/expression — There is More Than One Way To Do It!
Your example demonstrates it well :-)
My opinion: Right and so and so!
by
0 like 0 dislike
Judging by the fact that you are concerned about this question, your module is relatively small, and for him both are acceptable. When/if your pricer.pm will be large enough so that there is a need in OOP, the correct option will be obvious.
\r
Generally would be correct to do so:
\r
$item->calc_total_price(); 

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

I.e. a special object (or function) that calculates the data item, use not openly, but inside the method of the Item class. But this is all theory, which is in the pearl of the few who reached.
\r
Just want to warn you against studying PLO for example, pearl. It is there made badly and only partially corrected with crutches. Risk getting hurt for life, like the GOTO programmers in basic. To better understand what is OOP and how to use it, see how it is implemented in Java/C++, Python/Javascript, LISP/Scheme.
by
0 like 0 dislike
If item_id is not used anywhere else except in calc_total, it is better the second option. Otherwise — first.
To the constructor are usually passed, some are common to all object fields.
by

Related questions

0 like 0 dislike
7 answers
asked Mar 24, 2019 by SLY_G
0 like 0 dislike
4 answers
asked Apr 12, 2019 by fyapy
0 like 0 dislike
5 answers
0 like 0 dislike
2 answers
asked Aug 2, 2019 by Flappy1488
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
28,048 users