Class::TransparentFactory - Transparently choose a provider class with an automatic facade


Class-TransparentFactory documentation  | view source Contained in the Class-TransparentFactory distribution.

Index


NAME

Top

Class::TransparentFactory - Transparently choose a provider class with an automatic facade

SYNOPSIS

Top

  package LooksLikeOneClass;
  use Class::TransparentFactory qw(new foo bar);
  sub impl {
      $class = today_is_a_weekday() ? "WeekdayProvider" : "WeekendProvider";
      require $class;
      return  $class;
  }

  package UserCode;
  use LooksLikeOneClass;
  LooksLikeOneClass->foo();  # WeekdayProvider::foo or WeekendProvider::foo
                             # depending on whether today_is_a_weekday().




DESCRIPTION

Top

This module is intended for developers who find they need to refactor code away from one provider of functionality to a factory + set of API-compatible providers. It's not that factories are very difficult to write or maintain, but code that uses them tends to be somewhat cluttered.

With Class-TransparentFactory, your user code remains exactly as it was before you split off to several providers; the original module class turns into a facade in which class methods are automatically turned into proxies for the appropriate provider class.

To use Class-TransparentFactory in user code, no change is needed. (That is the point!) To use it in your libraries, you need to follow these steps:

Move all your actual implementation into another module.

Its name is not important to Class::TransparentFactory, but let's call it ProviderOne here. The old namespace by which the implementation was known we will call Facade.

Declare Facade as a transparent factory.

See import below for details. For a typical OOPish module with no special class methods, this will suffice:

  package Facade;
  use Class::TransparentFactory qw(new);

Implement your factory.

See impl for details. Here is where you put the business logic that determines which provider is suitable and should be used for this particular call.

FUNCTIONS

Top

import

The import directive is your declarative way of specifying which class methods belong to the transparently facaded API. Supply a simple list of names. Instance methods need not be specified here, since subsequent method dispatches on objects created by provider classes will presumably go to the correct place directly.

impl

This is not a method of Class::TransparentFactory, but rather one that you must implement in your facade class yourself.

It is here that you do the actual factorty work. You have the actual class method call arguments for your inspection if you need them. impl must do the following:

Determine the appropriate provider for this call

You may base this decision on the method arguments, the call stack, phase of the moon, or whatever you wish. Only you know why you needed a variety of providers, so you should know how to pick among them.

Make sure it is loaded

You can use a require or any other means. (In a static setup, it is perfectly reasonable to say use ProviderOne; use ProviderTwo etc. at the top of the facade class and not worry about this step in impl.)

Return it

Simply arrange for impl to return a string with the class name. Class::TransparentFactory will handle the dispatching.

EXPERIMENTATIONAL STATUS

Top

This module is highly experimental! I am looking for improvements, from ideas for a better name via clever features. Please contact me at the address below if you have a suggestion.

AUTHOR

Top

Gaal Yahas, <gaal at forum2.org>

BUGS

Top

Please report any bugs or feature requests to bug-class-transparentfactory at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Class-TransparentFactory. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Class::TransparentFactory

You can also look for information at:

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Class-TransparentFactory

* CPAN Ratings

http://cpanratings.perl.org/d/Class-TransparentFactory

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Class-TransparentFactory

* Search CPAN

http://search.cpan.org/dist/Class-TransparentFactory

ACKNOWLEDGEMENTS

Top

Thanks to Zsban Ambrus for pointing me at the *__ANON__ hack for naming closures, and to Yitzchak Scott-Thoennes for posting about it here: http://perlmonks.org/?node_id=304883.

Thanks also to Yuval Kogman, <nothingmuch@woobling.org> for some discussion. I stole none of his good ideas yet, so all suckage here is my fault.

COPYRIGHT (The "MIT" License)

Top


Class-TransparentFactory documentation  | view source Contained in the Class-TransparentFactory distribution.