User Tools

Site Tools


kurs:oo-factory

Files:

  • lib/Check
  • lib/Check/Ping.pm
  • lib/Check/Telnet.pm
  • lib/Check.pm
  • t/AvailCheck.t
## t/AvailCheck.t
use Test::More tests => 2;
BEGIN { use_ok('AvailCheck') };

use Data::Dumper;

my $opt = [
    {service => 'ping', server=>'server1'},
    {service => 'telnet', server=>'server1'},
    {service => 'telnet', server=>'server2'},
    {service => 'ping', server=>'server2'},
];

use Check;

my $check_factory = Check->new;

my $check = $check_factory->get_check_object('ping');

isa_ok($check, 'Check::Ping', 'returned Object Check::Ping');
$check->servername('server1');
$check->check;
## lib/Check.pm
package Check;

use Moose;

use Data::Dumper;
has 'servername' => (is => 'rw', isa => 'Str');

my $check_factory = {
   'ping'    => 'Check::Ping',
   'telnet'  => 'Check::Telnet',
};

sub get_check_object {
  my $self = shift;
  my $classname = shift;
  my $class = $check_factory->{$classname};
  eval "require $class";
  return $class->new;
}

1;
## Check/Ping.pm
package Check::Ping;

use Moose;
extends 'Check';

sub check {
  my $self = shift;
  print "Ping";
}

1;
## Check/Telnet.pm
package Check::Telnet;

use Moose;
extends 'Check';

sub check {
  my $self = shift;
  print "Telnet\n";
}

1;
kurs/oo-factory.txt · Last modified: 2014/09/10 21:22 (external edit)