The Daily Static
  The Daily Static
UF Archives
Register
UF Membership
Ad Free Site
Postcards
Community

Geekfinder
UFie Gear
Advertise on UF

Forum Rules
& FAQ


Username

Password


Create a New Account

 
 

Back to UserFriendly Strip Comments Index

Okay, my programming friends. by Didactylos2005-05-07 12:13:08
  DAOs are peasy in Perl with DBI. by LionsPhil2005-05-07 14:00:55
    From my University's Wiki, and modified: by LionsPhil 2005-05-07 14:32:45
(I wrote this, so there isn't a copyright/security problem.)
package FinancialTransaction;
# You may ask, "if this is an abstract class, why does it have a constructor"?
# Simple: I don't want to rewrite the same code for every subclass. That's
# a much bigger source of potential bugs than the risk of some numptie who didn't
# read the docs trying to instanciate the superclass.
# If you're a real pedant, add a runtime check, as with getall().
sub new { my ($proto, $id) = @_;
    my $this = { id => $id };
    bless $this, (ref($proto) or $proto);
}

sub getall { my ($proto) = @_;
    my $class = (ref($proto) or $proto);
    die "Subclasses only" if $class eq "FinancialTransaction";
    my $dnasample = bless({}, $class);
    # Get @ids from somewhere... (arguments? database? file?)
    # eg. the result of "SELECT id FROM $class"
    foreach (@ids) { $_ = $dnasample->new($_); }
    \@ids;
}

# General accessor/mutator
sub _am { my ($this, $field, $val) = @_;
    if(defined $val) {
        # Execute "UPDATE ".$this->TABLE." SET $field = ? WHERE id=?"
        # For bindings $val and $this->{'id'}
    } else {
        # Return the result of a similar SELECT statement
    }
}

package Deposit;
use base qw(FinancialTransaction);
use constant TABLE => 'DEPOSIT';
sub someattribute { $this->_am('someattribute', @_); }

package Withdrawl;
use base qw(FinancialTransaction);
use constant TABLE => 'WITHDRAWL';
sub someotherattribute { $this->_am('someotherattribute', @_); }
You can now do my $deposits = Deposit->getall;, and my $withdrawls = Withdrawl->getall;, but you haven't had to redefine what is essentially the same constructor in multiple places. This is a royal PITA to try to do in Java (believe me, I made the mistake of trying in my E-Business coursework), and basically forces you to use the Factory pattern instead, which means even more classes to maintain.
Be warned: I haven't tested this fully because I've hacked it about a bit in ARS to better fit your scenario and add the '_am' trick. You can also now do $depositinstance->someattribute('foo') and $depositinstance->someattribute() to set/access someattribute of a Deposit. You can break '_am' into seperate get/set, too, if you prefer.
[ Reply ]
      Don't worry... by Didactylos2005-05-07 15:04:36
        Ta. by LionsPhil2005-05-07 17:21:38

 

[Todays Cartoon Discussion] [News Index]

Come get yer ARS (Account Registration System) Source Code here!
All images, characters, content and text are copyrighted and trademarks of J.D. Frazer except where other ownership applies. Don't do bad things, we have lawyers.
UserFriendly.Org and its operators are not liable for comments or content posted by its visitors, and will cheerfully assist the lawful authorities in hunting down script-kiddies, spammers and other net scum. And if you're really bad, we'll call your mom. (We're not kidding, we've done it before.)