lanta has asked for the wisdom of the Perl Monks concerning the following question:
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.##################### 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(); #####################
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 ).$per = new Person(); $ppl_fetched = $per->record_count();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: OO design.
by chromatic (Archbishop) on Jan 20, 2006 at 01:53 UTC | |
|
Re: OO design.
by moot (Chaplain) on Jan 20, 2006 at 02:15 UTC | |
|
Re: OO design.
by srdst13 (Pilgrim) on Jan 20, 2006 at 10:52 UTC | |
|
Re: OO design.
by ruoso (Curate) on Jan 20, 2006 at 17:46 UTC | |
|
Re: OO design.
by zombie_fred (Scribe) on Jan 20, 2006 at 20:27 UTC |