Re: oo design question - how to avoid a factory
| From: | Stefan Ohrmann | Date: | Tue, 21 Dec 2004 21:50:38 +0000 |
| Subject: | Re: oo design question - how to avoid a factory | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-35213@lists.php.net to get a copy of this message | ||
Hello,
Am 21.12.2004 15:48 schrieb Andrew Nagy:
> The problem with this is getPaging is specific to the html renderer.
> This can't go into the core dg.
Then you also have to erase the
Structures_DataGrid_Renderer:_parseHttpRequest() function, or it is used
by every renderer?
But you could solve your Problem, if you use a lazy loading mechanism
for the renderer that creates the renderer only if you really render the
data grid. With this mechanism the renderer will created only once. This
could achived by following sample implementation of
Structures_DataGrid_Renderer. I left out all functions, which don't
touch rendering issues.
<?php
class Structures_DataGrid_Renderer extends Structures_DataGrid_Core
{
var $rendererClass = null;
var $renderer;
function Structures_DataGrid_Renderer(
$renderer = DATAGRID_RENDER_TABLE,
$limit = null,
$page = 1
) {
if (PEAR::isError($this->setRenderer($renderer))) {
$this->setRenderer(DATAGRID_RENDER_TABLE);
}
parent::Structures_DataGrid_Core($limit, $page);
}
function render()
{
if (is_null($this->rendererClass)) {
return new PEAR_Error('No renderer');
}
$this->createRenderer();
return $this->renderer->render();
}
function _createRenderer()
{
if (
isset($this->renderer)
&& is_a($this->renderer, $this->renderClass)
) {
return true;
};
$this->renderer = new $this->rendererClass($this);
return true;
}
function &getRenderer()
{
$this->_createRenderer();
return $this->renderer;
}
function setRenderer($renderer)
{
$class = 'Structures_DataGrid_Renderer_' . $renderer;
$file = 'Structures/DataGrid/Renderer/' . $renderer . '.php';
if (
@include_once($file)
&& class_exists($class)
) {
$this->rendererClass = $class;
} else {
$this->rendererClass = null;
return new PEAR_Error('Invalid renderer');
}
return true;
}
}
?>