pluginfactory / 1.0.3
| Short description: | Mixin for making dynamically-loadable plugin classes | |||||
|---|---|---|---|---|---|---|
| Category: | Library/Design Patterns | |||||
| Status: | stable | |||||
| Created: | 2004-03-08 21:38:48 GMT | |||||
| Last update: | 2008-04-16 14:20:16 GMT | |||||
| Owner: | Michael Granger (Projects of this owner) | |||||
| Homepage: | http://www.deveiate.org/projects/PluginFactory/ | |||||
| Download: | http://www.deveiate.org/code/PluginFactory-1.0.3.tar.gz | |||||
| License: | Ruby's | |||||
| Dependency: |
|
|||||
| Description: | PluginFactoryPluginFactory is a mixin module turns an including class into a factory for its derivatives, capable of searching for and loading them by name. This is useful when you have an abstract base class which defines an interface and basic functionality for a part of a larger system, and a collection of subclasses which implement the interface for different underlying functionality. An example of where this might be useful is in a program which talks to a database. To avoid coupling it to a specific database, you use a Driver class which encapsulates your program’s interaction with the database behind a useful interface. Now you can create a concrete implementation of the Driver class for each kind of database you wish to talk to. If you make the base Driver class a PluginFactory, too, you can add new drivers simply by dropping them in a directory and using the Driver’s create method to instantiate them: Synopsisin driver.rb:
require "PluginFactory"
class Driver
include PluginFactory
def self::derivative_dirs
["drivers"]
end
end
in drivers/mysql.rb:
require 'driver'
class MysqlDriver < Driver
...implementation...
end
in /usr/lib/ruby/1.8/PostgresDriver.rb:
require 'driver'
class PostgresDriver < Driver
...implementation...
end
elsewhere require 'driver' config[:driver_type] #=> "mysql" driver = Driver.create( config[:driver_type] ) driver.class #=> MysqlDriver pgdriver = Driver.create( "PostGresDriver" ) |
|||||
| Versions: | [1.0.3 (2008-04-16)] [1.0.1 (2005-03-05)] [1.0.0 (2004-09-08)] [0.01 (-)] | |||||