Daniel Muey
and 1 contributors

NAME

Role::Multiton - Add a multiton constructor to your class

VERSION

This document describes Role::Multiton version 0.2

SYNOPSIS

Object:

    package ZeroCool

    ## no critic (RequireUseStrict) - Moo does strict
    use Moo;

    with 'Role::Multiton';

    …

Code:

    use ZeroCool;

    my $z3r0 = ZeroCool->new(…); # returns a new object every time, not a multiton (see Role::Multiton::New if you want to do that)
    my $z3r1 = ZeroCool->multiton(@args); # returns the same object each time when given the same args
    my $z3r2 = ZeroCool->instance(@args); # alias to multiton()

DESCRIPTION

See http://en.wikipedia.org/wiki/Multiton_pattern for info about multitons.

INTERFACE

It adds these methods:

multiton()

Creates a new object initialized with the arguments provided and then returns it.

Subsequent calls to multiton() with the same arguments will return that same object.

You might think of it as an argument based singleton since the arguments are used to make the key in the multiple singleton mapping.

If you’d rather have new() be a multiton use Role::Multiton::New instead.

instance()

Alias to multiton(). This allows for compatibility with other constructor roles (e.g. MooseX::Singleton, Moox::Singleton).

It is nice because it allows you to switch between what type of instance you get without having to update your code.

For example, if your code does ZeroCool->instance() a million times and you see the need to change from using a singleton to a multiton (or some other instance() defining role), all you have to do is change your class and the consumers will not need to do anything besides update their ZeroCool install (i.e. not update their code in a million places).

Of course, there are also cases when being explicit is important so you’d use multiton().

You can also have both singleton and multiple support (the first one that is with()d will defined instance()):

This will give you multiton(), singleton(), and instance() is a singleton:

    with 'Role::Singleton';
    with 'Role::Multiton';

This will give you multiton(), singleton(), and instance() is a multiton:

    with 'Role::Multiton';
    with 'Role::Singleton';

If you’d rather have new() be a multiton use Role::Multiton::New instead.

DIAGNOSTICS

Throws no warnings or errors of its own.

CONFIGURATION AND ENVIRONMENT

Role::Multiton requires no configuration files or environment variables.

DEPENDENCIES

Role::Tiny, Role::_Multiton

SEE ALSO

Role::Multiton::New, Role::Singleton, Role::Singleton::New

INCOMPATIBILITIES

None reported.

Definitely works w/ Moo based objects since it uses Role::Tiny.

I’d like to add POD/tests/fixes about what, if anything, needs done to make it with()able for M, Mo, Moose, Mouse, Meese, Meeses, Mooses, etc.

The goal is to be object system agnostic and as minimal as possible.

BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to bug-role-multiton@rt.cpan.org, or through the web interface at http://rt.cpan.org.

AUTHOR

Daniel Muey <http://drmuey.com/cpan_contact.pl>

LICENCE AND COPYRIGHT

Copyright (c) 2013, Daniel Muey <http://drmuey.com/cpan_contact.pl>. All rights reserved.

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

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.