I'm an average perl programmer. I haven't problem with the language itself, but with the "good" object design. While I'm able understand (most of) CPAN modules, without serious problems, i'm unable design myself even the simple object hierarchy.
Example - now facing an really simple application (web and command line interface):
commands.txt
) and zero or more imagesJobRepository
(another directory)JobRepository
to global rendering queue (again, another directory)JobRepository/result
directoryIn bash
it is doable with few "not to complicated" bash script - but i want do it in Perl (because web interface) - and want practice perlish (Moose) object design…
And here starts my problems.
Tried "visual" noun-analysis approach and make the next image.
Posted the image, because it is "shorter" as:
package Iren::JobRepo;
use Moose;
use warnings;
has 'Jobs' => (is => 'rw', isa=>ArrayRef[Iren::Job]);
…
method AddJob {
...
}
etc.
As you can see, it is really simple - but immediatelly facing some decision problems, e.g.:
$user->send_email
- comes me silly, because we sending email TO users and not the user to itself…JobRepo->SendJobToRenderQueue
or i should call some RenderQueue->addJob
method?IPC::DirQueue
- (should be the implememtation of RenderQueue)As you can see, no Roles, no Traits here - nothing - it is simple… - but full of questions :(
Can anybody help clear the mess? What should be the "good" package hierarchy?
So, i'm really lost and I have started to disappointed myself. Additional questions (i know, these are opinion based) - but I must ask them...
$cat->diets
:) - but mastering something new - is bad for me…Sorry for the wall of text. I would be much happy getting any pointer to good book or anything what helps...
Stuff @daxim didn't answer:
- How to learn good object design for perl/Moose? (I'm probably will never use another language)
- searching google about object design (and Stackoverflow too) many times are cited the "Gand of Four" book (and few others). But usually for Java. It is worth to buy for the perl/Moose? Or is here some another good books for perl/Moose?
- is some good technique how to check the proper object design?
Frankly, the best way to check is: (1) Design review with a second pair of eyes and (2) see how your deign holds up to maintenance and reuse. Good design does NOT need to change much with every update. I'm not aware of any specific technical/procedural ways to check.
A good rule of thumb is: if you need to change ways something behaves, perhaps drastically, how much of your code will need to change? The best design is one that minimizes the changes.
- simply - how do you mastering your object hierarchy/Roles/Traits etc…? While i'm reading the examples - I understand the $cat->diets :) - but mastering something new - is bad for me…
Practice. Ideally, find a problem solved by a tutorial/book, solve yourself first, then see how they solve it. Then post your best combined solution to codereview.SE and ask if it can be improved :)
circular definitions. The User has JobRepository, The Repository has many Jobs, but the Job has? an User? (need to know to whom belonging the job) - and so on..
No, you do NOT need to know whom the job belongs to. I explained why a couple of weeks ago - a VERY similar question was asked and answered by me here: OO Design Patterns with Perl .
Short version: In real code, you are very unlikely to ever start with a job without a user - by the time you get to the job, you most likely already started with a known user and got the job repository from that user. You don't need to re-find the user anymore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With