State pattern???
Posted: Wed Dec 15, 2010 10:20 am
I'm using a State-pattern-like pattern
and I need your comments on it.
I'm developing a CLI code generator framework.
So ... I have a Plugin_Factory that creates an object. The object created class inherits the Plugin_Abstract class. So they have similar API. The main loop should execute plugin methods like this:
But some of the plugins should change their behavior depending of the CLI options (and related). Such an example is a plugin that reads an XML file (MySQLWorkbench file), reads the MySQLWorkbench version from it and provides a subplugin that can handle this version. So, I changed the above loop like this:
and the Plugin_Abstract code:
So, the MySsqlWorkbench Importer Plugin becomes:
I'm planing to use such strategy for providing help messages (i.e. when the --help option is provided). Although I feel the AbstractFactory pattern would be a better solution...
I'm using the same strategy to read options from a config file instead of the CLI (except the --config option).
I'm developing a CLI code generator framework.
So ... I have a Plugin_Factory that creates an object. The object created class inherits the Plugin_Abstract class. So they have similar API. The main loop should execute plugin methods like this:
Code: Select all
$pluginFactory = new Plugin_Factory();
// +++LOOP
$this->plugin = $pluginFactory($this->optionProcessor->import);
$this->plugin->provideBootstrap($this->bootstrap);
$this->plugin->provideOptions($this->optionProcessor);
$this->plugin->init();
$this->plugin->run();Code: Select all
$pluginFactory = new Plugin_Factory();
$this->plugin = $pluginFactory($this->optionProcessor->import);
$this->plugin = $this->plugin->getInstance();
$this->plugin->provideBootstrap($this->bootstrap);
$this->plugin = $this->plugin->getInstance();
$this->plugin->provideOptions($this->optionProcessor);
$this->plugin = $this->plugin->getInstance();
$this->plugin->init();
$this->plugin = $this->plugin->getInstance();
$this->plugin->run();Code: Select all
/**
* @var Plugin_Abstract
*/
protected $self;
public function __construct()
{
$this->self = $this;
}
public function getInstance()
{
$this->self->optionProcessor = $this->optionProcessor;
$this->self->bootstrap = $this->bootstrap;
$this->self->self = $this->self;
return $this->self;
}
Code: Select all
...
public function provideOptions(Application_Options_Abstract $optionProcessor)
{
parent::provideOptions($optionProcessor);
$optionProcessor->addOptions(array
(
'file=s' => 'MySQLWorkbench file [required]',
'encoding=s' => 'File encoding',
));
if (empty($optionProcessor->file))
{
throw new \Exception('--file option is required.');
}
if (empty($optionProcessor->encoding))
{
$optionProcessor->encoding = 'utf-8';
}
$this->versionReader = new Importer_MySqlWorkbench_VersionReader
(
new Importer_MySqlWorkbench_Loader($optionProcessor->file),
$optionProcessor->encoding
);
$factory = new Importer_MySqlWorkbench_Factory();
$this->self = $factory->create($this->versionReader);
}
I'm using the same strategy to read options from a config file instead of the CLI (except the --config option).