The Artima Developer Community
Sponsored Link

Java Buzz Forum
Using a lot lately: Factory + implementation autodiscovery

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Chris Winters

Posts: 931
Nickname: cwinters
Registered: Jul, 2003

Daytime: Java hacker; nighttime: Perl hacker; sleeptime: some of both.
Using a lot lately: Factory + implementation autodiscovery Posted: Feb 10, 2005 11:01 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Chris Winters.
Original Post: Using a lot lately: Factory + implementation autodiscovery
Feed Title: cwinters.com
Feed URL: http://www.cwinters.com/search/registrar.php?domain=jroller.com®istrar=sedopark
Feed Description: Chris Winters on Java, programming and technology, usually in that order.
Latest Java Buzz Posts
Latest Java Buzz Posts by Chris Winters
Latest Posts From cwinters.com

Advertisement
If you're not using IoC and you want decoupled code while still figuring out at runtime how to perform a particular action you've probably got a number of factories in your application. But how to let your factory know about all the implementations it can create?

Using Class::Factory you can add classes to the factory dynamically like this:

# declare the factory:
package My::Factory:
use base qw( Class::Factory );
 
# and add classes to it from elsewhere:
My::Factory->register_factory_type( hat   => 'My::Factory::Hat' );
My::Factory->register_factory_type( ascot => 'My::Factory::Ascot' );

And using Module::Find you can find all the classes to add to the factory at runtime with something like this:

use Module::Find;
useall( 'My::Factory' );

(NB: I haven't used M::F but have my own implementation of something that appears to do exactly the same thing.)

So why not have all the subclasses register themselves with the factory?

package My::Factory::Hat;
use base qw( My::Factory );
My::Factory->register_factory_class( hat => __PACKAGE__ );

And then have the factory find all its implementations?

package My::Factory:
use base qw( Class::Factory );
use Module::Find;
 
useall( __PACKAGE__ );

Those two simple steps create a system where a user can just drop a class in the right place and have it found by your framework. This is particularly useful if your set of objects gets collected and run together (using some additional metadata for ordering if necessary) as in my discussion of OpenInteract2::Setup a few weeks ago. There, a user can just add a class with the right name and dependencies (the metadata) and when the website starts up it gets found, imported, and executed in the right order.

Or in another example that I'm not quite done with: how do we associate an incoming URL (or some other identifier) with the action object responsible for generating its content?

Currently this is touched in a couple of places, always a bad smell. The request is responsible for parsing the URL into action and task names and then the controller (underdocumented in last release) uses this name to create the action. But the request doesn't know anything about actions -- it got put there just because we were already parsing URLs and it seemed appropriate to extract a little more data there.

So why not let a separate object figure this out? This whole thing got started because I wondered how hard it would be to create userdirs like the CPAN urls in the last paragraph -- that could just be one of the objects in a chain of responsibility that looks at the URL (or whatever else it wants) and decides what action to create.

Like I said, I'm not done with it yet. But the factory plus autodiscovery pattern makes this a snap to implement and test out. Even better, it will be extremely easy for other people using OpenInteract2 to add their own behaviors that are indistinguishable from the internal ones to perform this core job.

Read: Using a lot lately: Factory + implementation autodiscovery

Topic: Users aren't dangerous Previous Topic   Next Topic Topic: How to run javac 1.5 (or beyond) compiler for JSP compilation in Tomcat 5.5 with generics...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use