NAME

Class::DBI::Plugin::Factory - Implementation of "factory pattern"

SYNOPSIS

require this module in your base CDBI class.

MyService::DBI;
use strict;
use base qw/Class::DBI/;
use Class::DBI::Plugin::Factory;

__PACKAGE__->set_db('Main', @datasource);

execute "set_factory" in the class you want to implement "factory pattern".

MyService::Member;
use strict;
use base qw/MyService::DBI/;
__PACKAGE__->table('member');
__PACKAGE__->columns(Primary => qw/member_id/);
__PACKAGE__->columns(Essential => qw/member_type name age/);

__PACKAGE__->set_factory(
    type_column => 'member_type',
    types => {
        -Base    => 'Basic',
        1        => 'Free',
        2        => 'VIP',
    },
);

# abstract method
sub is_free {}
sub monthly_cost {}

define subclasses. for example, MyService::Member::Basic in this case.

package MyService::Member::Basic;
use strict;
use base qw/MyService::Member/;
sub is_free { 0 }
sub monthly_cost { 500 }

define MyService::Member::Free.

package MyService::Member::Free;
use strict;
use base qw/MyService::Member/;
sub is_free { 1 }
sub monthly_cost { 0 }

define MyService::Member::VIP.

package MyService::Member::VIP;
use strict;
use base qw/MyService::Member/;
sub is_free { 0 }
sub monthly_cost { 250 }

after all setting. you can use them like follow.

package main;
use MyService::Member;

my @members = MyService::Member->retrieve_all;

foreach my $member ( @members ) {

    print $member->member_type;

    # if member_type is 1, follow line prints '0'.
    # and if member_type is 2, '250' will be printed.

    print $member->monthly_cost;
}    

DESCRIPTION

This plugin makes CDBI to implement "factory pattern".

set_factory

call this method in a package where you want to design as "factory pattern". and you need to set hashref as a argument of "set_factory". follow 2 keys are required.

type_column

According to a value of the column set with this key, this module rebless records.

types

hashref which defines a relations between type-parameters and subclasses. if undefined type-parameter is found, records will be reblessed to the subclass which defined as '-Base'

SEE ALSO

Class::DBI

AUTHOR

Basic idea and sample by Yasuhiro Horiuchi.

Plugin's code by Lyo Kato <kato@lost-season.jp>

COPYRIGHT AND LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 169:

You forgot a '=back' before '=head1'