开发者

OOP design suggestion

开发者 https://www.devze.com 2023-02-14 08:57 出处:网络
I have set of files that needs to either emailed or FTPed(read from config). Before doing either of these I need to so some common operation on the files, like changing filenames, sanity check, so on.

I have set of files that needs to either emailed or FTPed(read from config). Before doing either of these I need to so some common operation on the files, like changing filenames, sanity check, so on.

        package Class::Foo::Partners;

    use Carp;
    use Data::Dumper;
    # Sanity check and Blessing
    sub new ($) {
        my $class = shift;
        my %attr = @_;
        Carp::confess('Config undefined') unless defined $attr{cfg};
        my $self = bless({}, $class);
        %$self = @_;
        return $self;
    }

    sub process {
        my $self = shift;

        my %filestoupload = ();
        if ($self->{dbh}->sql($sql, \%filestoupload)) {
            my $stats;
            if (defined $self->{cfg}->{$self->{section}}->{pdf_email_rcpt}) {
                $stats = Class::Foo::Email->new(section => $self->{cfg}->{$self->{section}}, filestoupload => \%filestoupload);
                $stats->sendfiles;
            } else {
                $stats = Class::Foo::FTP->new(section => $self->{cfg}->{$self->{section}}, filestoupload => \%filestoupload);
                $stats->sendfiles;
            }
        } elsif ($self->{dbh}->{_error}) {
            Carp::confess($self->{dbh}->{_error});
        } else {
            print "NO FILES";
        }
    }


    package Class::Foo::FTP;

    use Carp;
    use Data::Dumper;
    use POSIX qw( strftime );
    use File::Temp qw (tempdir) ;
    use File::Copy;
    use Net::FTP;

    # Sanity check and Blessing
    sub new ($) {
        my $class = shift;
        my %attr = @_;
        Carp::confess('Section undefined') unless defined $attr{section};
        Carp::confess('undefined ftp_host') unless defined $attr{section}->{ftp_host};


        my $self = bless({}, $class);
        %$self = @_;

        return $self;
    }

    sub sendfiles {
        my $self = shift;
        return unless(keys %{$self->{filestoupload}});
        #DO SOME COMMON TASK
        ..
        $self->ftp_connect();
        ..
        ..
    }

    packa开发者_JS百科ge Class::Foo::Email;

    use Data::Dumper;
    use Mail::Sender;
    use POSIX qw( strftime );
    use File::Temp qw (tempdir) ;
    use File::Copy;

    sub new ($) {
        my $class = shift;
        my %attr = @_;
        Carp::confess('Config: undefined pdf_email_subject') unless defined $attr{section}->{pdf_email_subject};
        Carp::confess('Config: undefined pdf_email_from') unless defined $attr{section}->{pdf_email_from};
        my $self = bless({}, $class);
        %$self = @_;

        return $self;
    }

    sub sendfiles {
        my $self = shift;
        return unless(keys %{$self->{filestoupload}});
        #DO SOME COMMON TASK
        ..
        my $mailrcpt = $self->{section}->{pdf_email_rcpt};
        my $sender = new Mail::Sender {smtp => 'localhost', from => $self->{section}->{pdf_email_from}};
        $sender->MailFile({ to => $mailrcpt, 
                            subject => $self->{section}->{pdf_email_subject}, 
                            msg => "Attached is A1 of today's WSJE. ",
                            ctype => 'application/pdf',
                            file => @files } );

        $self->{uploaded_count} = @files;
    }

Where to do the common operation and when and how to call respective child classes?

Should I use abstraction?

thanks for your help


Check out the implementation of MT::FileMgr:

https://github.com/openmelody/melody/tree/master/lib/MT

It should give you a lot of ideas on how to do Perl OOP for something like this.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号