OOP设计建议

发布于 2024-10-19 22:57:47 字数 3306 浏览 1 评论 0原文

我有一组文件需要通过电子邮件发送或通过 FTP 发送(从配置中读取)。在执行这些操作之前,我需要对文件进行一些常见操作,例如更改文件名、健全性检查等。

        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();
        ..
        ..
    }

    package 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;
    }

公共操作在哪里进行,何时以及如何调用各自的子类?

我应该使用抽象吗?

感谢您的帮助

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();
        ..
        ..
    }

    package 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

血之狂魔 2024-10-26 22:57:47

查看 MT::FileMgr 的实现:

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

它应该给你很多关于如何针对此类事情进行 Perl OOP 的想法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文