|o||a base class, which provides methods to convert marked-up text into runnable code,|
|o||an extension which enhances security by runing the code in a Safe compartment,|
|o||an extension which feeds output through an HTML-escaping filter, and|
|o||an extension which records internal profiling data for benchmarking purposes.|
A naive implementation of this might use a subclass for each behaviour, and look like the following:
The well-known problem with this implementation appears when you want to combine several features:
This is the dreaded diamond inheritance problem: if Base provides a compile() method, which Filter and Safe each override to perform additional actions before or after calling SUPER::compile(), how can we ensure they are all called in the correct sequence?
The standard software engineering solution is to replace the use of inheritance with decomposition into several different classes of objects, which then cooperate through decoration and delegation; for example, using separate classes for a resolver, a lexer, a parser, a compiler, and an output channel. (See HTML::Mason for an example of this design.)
Indeed, composition is an underutilized design technique, and there are many times when inheritance is not the best tool to use. But of course, in Perl theres more than one way to solve this problem, one of which is facilitated by this distribution.
We can rearrange our class hierarchy to avoid diamond inheritance by using a base and a collection of mixin classes, which dont directly inherit from the base class:
However, in this condition our mixin classes cant call SUPER methods at all! Instead, another redispatch mechanism is needed, one that is able to back-track through the inheritance tree and explore other branches. (See NEXT for such an implementation.)
The order in which mixins are stacked is significant, so the caller does need to have some understanding of how their behaviors interact. For example, youd typically want to ensure that the Benchmarking mixin was the first in the chain, so that it could time everything later in the sequence.
The Class::MixinFactory distribution provides serveral elements to facilitate tihs kind of dynamic mixin architecture. The top level package is just a facade that loads the other necessary classes and provides a few import options for compile-time convenience. (See Class::MixinFactory.)
To generate an object with some combination of mixins, you first use a mixin factory to generate a mixed class. If a class with that combination of classes has already been created, it is reused. You can add a factory method to your base class, create a separate factory object, or inherit to produce a factory class. (See Class::MixinFactory::Factory.)
To allow mixin classes to redispatch to subsequent classes, all mixed classes also inherit from a class which provides a NEXT() method. (If you would prefer, your mixin class can alternately use the AUTOLOAD solution provided by the NEXT::ACTUAL module from CPAN, or any other equivalent re-dispatch mechanism.) (See Class::MixinFactory::NEXT.)
There are number of other modules on CPAN that also support mixins, method importing, or run-time multiple inheritance, while others dont use mixins but are addressing a similar area of concern.
o The mixin, Class::Mixin, and Spiffy modules support mixin classes but dont have a configurable factory object or support run-time mixin selection. o The Class::Mix and Class::Mutator modules provide run-time class generation with multiple inheritance, but dont provide a configurable factory object or a redispatch technique. o The Class::Factory module has a factory interface, but doesnt support multiple inheritance. o The NEXT module provides a backtracking equivalent to SUPER similar to the NEXT method included here, but uses AUTOLOAD rather than a universal method. o The Class::Delegate and other modules support decoration to address this problem via decomposition. o The Class::Role, Class::Roles and Class::Trait modules support composing shared behaviors into your class.
This is version 0.92.
This module is new and relatively untested.
Please report any problems you encounter to the author at the below address.
This module should work with any version of Perl 5, without platform dependencies or additional modules beyond the core distribution.
perl -MCPAN -e install Class::MixinFactory
After downloading the distribution, follow the normal procedure to unpack and install it, using the commands shown below or their local equivalents on your system:
tar xzf Class-MixinFactory-*.tar.gz cd Class-MixinFactory-* perl Makefile.PL make test && sudo make install
If you have questions or feedback about this module, please feel free to contact the author at the below address. Although there is no formal support program, I do attempt to answer email promptly.
I would be particularly interested in any suggestions towards improving the documentation, correcting any Perl-version or platform dependencies, as well as general feedback and suggested additions.
Bug reports that contain a failing test case are greatly appreciated, and suggested patches will be promptly considered for inclusion in future releases.
If youve found this module useful or have feedback about your experience with it, consider sharing your opinion with other Perl users by posting your comment to CPANs ratings system (http://cpanratings.perl.org/rate/?distribution=Class-MixinFactory).
For more general discussion, you may wish to post a message on PerlMonks (http://perlmonks.org/?node=Seekers%20of%20Perl%20Wisdom) or on the comp.lang.perl.misc newsgroup (http://groups.google.com/groups?group=comp.lang.perl.misc).
Developed by Matthew Simon Cavalletto at Evolution Softworks. You may contact the author directly at evo#cpan.org or simonm#cavalletto.org, replacing # with @.
Custom development and technical consulting are available at www.evolutionsoftworks.com. More free Perl software is available at www.evoscript.org.
My sincere thanks to the Perl Monks community for their feedback on earlier versions of this commentary.
Copyright 2004 Matthew Simon Cavalletto.
You may use, modify, and distribute this software under the same terms as Perl.
|perl v5.20.3||MIXINFACTORY::README (3)||2004-11-19|