Factory pattern in Zend data grid

In the abstract class Bvb_Grid, there is static method named factory() which acts as a factory to create specific grid deploy class.

    public static function factory($defaultClass = 'Table', $options = array(), $id = '', $classCallbacks = array(), $requestParams = false)
    {
        self::initDeployClass();//initialize Zend_Loader_PluginLoader and add the prefix path
        try {
            $defaultClass = self::loadDeployClass($defaultClass);// load the deploy class 
        } catch (Zend_Loader_PluginLoader_Exception $e) {            
            if (!class_exists($defaultClass)) {
                throw $e;
            }
        }
        if (false === $requestParams) {
            // use request parameters
            $requestParams = Zend_Controller_Front::getInstance()->getRequest()->getParams();
        }
        if ($options instanceof Zend_Config) {
            $options = $options->toArray();
        }
        // use this as request parameters
        if (!isset($options['grid'])) {
            $options['grid'] = array('requestParams' => $requestParams);
        } else {
            $options['grid']['requestParams'] = $requestParams;
        }

        // handle _exportTo parameter compatible with calling with grid id and without
        if (isset($requestParams['_exportTo' . $id])) {
            $exportTo = $requestParams['_exportTo' . $id];
        } elseif (isset($requestParams['_exportTo'])) {
            $exportTo = $requestParams['_exportTo'];
        } else {
            $exportTo = false;
        }
        if (false === $exportTo) {
            // return instance of the main Bvb object, because this is not and export request
            $grid = new $defaultClass($options);
            $lClass = $defaultClass;
        } else {
            $lClass = strtolower($exportTo);
            // support translating of parameters specifig for the export initiator class
            if (isset($requestParams['_exportFrom'])) {
                // TODO support translating of parameters specifig for the export initiator class
                $requestParams = $requestParams;
            }
            // now we need to find and load the right Bvb deploy class
            // TODO support user defined classes
            $className = self::loadDeployClass($exportTo);
            if (Zend_Loader_Autoloader::autoload($className)) {
                $grid = new $className($options);
            } else {
                $grid = new $defaultClass($options);
                $lClass = $defaultClass;
            }
        }
        // add the powerfull configuration callback function
        if (isset($classCallbacks[$lClass])) {
            $grid->_configCallbacks = $classCallbacks[$lClass]; // register callbacks
        }
        if (is_string($id)) {
            $grid->setGridId($id);
        }
        return $grid;
    }
If we want to use one specific grid deploy class, we can also use it directly.

<?php

class Bvb_Grid_Deploy_Table extends Bvb_Grid implements Bvb_Grid_Deploy_DeployInterface {

public function __construct (array $options = array())
    {
        $this->_setRemoveHiddenFields(true);

        parent::__construct($options);

        if (isset($this->_options['grid']['id'])) {
            $this->setGridId($this->_options['grid']['id']);
        }

        $this->_gridSession = new Zend_Session_Namespace('Bvb_Grid_' . $this->getGridId());
        $this->addTemplateDir('Bvb/Grid/Template', 'Bvb_Grid_Template', 'table');

        if ($this->getParam('add') || $this->getParam('edit')) {

            if ($this->getParam('add')) {
                $this->_willShow[] = 'form';
                $this->_willShow[] = 'formAdd';
            }
            if ($this->getParam('edit')) {
                $this->_willShow[] = 'form';
                $this->_willShow[] = 'formEdit';
            }
        } else {
            if ($this->getParam('detail') ||  $this->getParam('delete') ){
                $this->_willShow[] = 'detail';
            } else {
                $this->_willShow[] = 'listing';
            }
        }
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Abstract Factory Pattern是一种设计模式,它是创建对象的工厂模式的变体,允许对象在运行时被替换。 Abstract Factory模式提供了一种方法,可以创建一组相关或相互依赖的对象,而不需要明确指定其具体类。 下面是一个C语言的代码示例,该代码实现了一个抽象工厂模式,该模式创建一组车辆: ```c #include <stdio.h> typedef struct IVehicle IVehicle; struct IVehicle { void (*Drive)(IVehicle *); }; typedef struct Car Car; struct Car { IVehicle base; int wheelCount; }; void Car_Drive(IVehicle *vehicle) { Car *car = (Car *)vehicle; printf("Driving a car with %d wheels\n", car->wheelCount); } typedef struct Bike Bike; struct Bike { IVehicle base; int pedalCount; }; void Bike_Drive(IVehicle *vehicle) { Bike *bike = (Bike *)vehicle; printf("Riding a bike with %d pedals\n", bike->pedalCount); } typedef struct IVehicleFactory IVehicleFactory; struct IVehicleFactory { IVehicle *(*CreateVehicle)(IVehicleFactory *); }; typedef struct CarFactory CarFactory; struct CarFactory { IVehicleFactory base; }; IVehicle *CarFactory_CreateVehicle(IVehicleFactory *factory) { Car *car = (Car *)malloc(sizeof(Car)); car->base.Drive = &Car_Drive; car->wheelCount = 4; return (IVehicle *)car; } typedef struct BikeFactory BikeFactory; struct BikeFactory { IVehicleFactory base; }; IVehicle *BikeFactory_CreateVehicle(IVehicleFactory *factory) { Bike *bike = (Bike *)malloc(sizeof(Bike)); bike->base.Drive = &Bike_Drive; bike->pedalCount = 2; return (IVehicle *)bike; } int main(int argc, char *argv[]) { CarFactory carFactory = { { &CarFactory_CreateVehicle } }; IVehicle *vehicle = carFactory.base.CreateVehicle((IVehicleFactory *)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值