I'm working to design an application using OO, this is the first time I'm doing this so any advice y'all can give is very much appreciated.

Background:

I have an entity( one among many ) let's call "Person" and I have many things I need to do with/to this entity, for instance
1. I need to fetch a list of "People" from the database and return to the user
2. Add a "Person" to the database.
3. Delete a "Person" in the database.

My approach is a file/class containing one operation(fetch, add, delete ). and a file/class which uses all these files/classes.
so I would have the top level class like so
##################### Package Person sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; $self->{FETCH} = Fetch->new(); $self->{ADD} = Add->new(); bless ($self, $class); return $self; } sub fetch { my $self = shift; return $self->{FETCH}; } sub add { my $self = shift; return $self->{ADD}; } 1; ##################### and the underlying classes will look like so.. ##################### Package Fetch sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; $self->{FIELDS} = []; $self->{FILTERS} = {}; $self->{RECORD_COUNT} = 0; $self->{RECORD} = []; $self->{DB_TYPE} = 'oracle'; $dbh = connect_db( 'login', 'password', 'dsn' ); bless ($self, $class); return $self; } sub fields { ..... } sub fetch_records { ..... } ##################### and the usage of the package is as follows ##################### use Person; $per = new Person(); $fch = $per->fetch(); # Set Fields $fch->fields( 'first_name', 'last_name', 'middle_name' ); # Get Fields print "The fileds being fetched are " . $fch->fields . "\n"; # Fetch People. while( (@prsn) = @{$fch->fetch_records()} ) { print "@prsn\n"; } print "No. of persons fetched are " . $fch->record_count(); #####################
My question is, is it ok for me to be placing each of these actions on the entity( adding person, fetching people ) etc in a class/file of it's own or should they all be in the same class/file. The reason I'm thinking I would like to split this into seperate classes is because I want the methods like record_count() specifically associated with the fetch object, since it has no relevance in the "Add person" context.
for instance, it all the actions were in the same class/file.
I would have a situation like
$per = new Person(); $ppl_fetched = $per->record_count();
where the record_count() method has meaning only in a "record fetching" scenario and not really with the whole Person object (which will have methods to add records, delete records etc ).
Sorry for the long post, Any and all help is much appreciated.

In reply to OO design. by lanta

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.