Studyon Minte9.com
ZCE 5.3

Study

Design Patterns



1) Singleton Pattern provide access to a single resource that is never duplicated.

<?php class Zend_Controller_Front { protected static $_instance = null; public function __construct() { echo "Class load"; } public static function getInstance() { if (null == self::$_instance) { self::$_instance = new self(); // self() points to the class in which it is written } return self::$_instance; } } $fc = Zend_Controller_Front::getInstance(); // Output: Class load $fc = Zend_Controller_Front::getInstance(); // Output: nothing (previews instance used) // --- Look Here --- // $fcn = new Zend_Controller_Front(); // Output: Class load
  2) With Front Controller Pattern a single component is responsible for handling all requests to other parts of an application.
# .htaccess RewriteEngine on RewriteRule !.(js|ico|gif|jpg|png|css|html)$ html/index.php # index.php <?php $fc = Zend_Controller_Front::getInstance(); // see Singleton Pattern for class Zend_Controller_Front ... $fc->setRouter(); $fc->setView(); ... $fc->dispatch(); // --- Look Here --- // # routes.ini ;HOMEPAGE routes.homepage.route = "" routes.homepage.defaults.module = "default" routes.homepage.defaults.controller = "frontend" routes.homepage.defaults.action = "index" ;ADMIN routes.admin.route = "admin" routes.admin.defaults.module = "default" routes.admin.defaults.controller = "admin" routes.admin.defaults.action = "index"
3) Factory Method Pattern use a generic class (the factory) for managing specialized classes.
<?php $db = Zend_Db::factory('PDO_MYSQL', array( 'host' => 'localhost', 'username' => 'root', 'password' => '', 'dbname' => 'testdb', )); class Zend_Db { public function factory($adapter, $config = array()) { $class = "Zend_Db_Adapter_{$adapter}"; // --- Look Here --- // /* Zend/Db/Adapter/ PDO/ Mysql/ Sqlite/ Oracle/ */ $class::connect(); } } class Zend_Db_Adapter_PDO_MYSQL { public function connect() { echo "connected"; } }
4) Registry Pattern allows any object to be used as Singleton, without it being written as Singleton.
<?php class Article_Controller_Front extends MyLibrary_Controller_Action { public function listAction() { // undefined articleModel (__get called) $articles = $this->articleModel->getArticles(); } } class MyLibrary_Controller_Action { private $_register = array(); // Magic method (called for undefined attributes of an class) public function __get($var) { if (strstr($var, 'Model')) { $modelKey = substr($var, 0, -5); // any object is used as Singleton, // without it being written specifically that way if (in_array($modelKey, $this->_register)) { return $this->_register[$modelKey]; // --- Look Here --- // } else { eval('$model = new Models_' . ucfirst($modelKey) . '();'); $this->_register[$modelKey] = $model; return $model; } } } } class Models_Article { public function getArticles() { echo 'articles'; } } $test = new Article_Controller_Front(); $test->listAction(); // Output: articles
5) Model View Controller separates business logic (MODEL) from display (VIEW) and decisional (CONTROLLER)
<?php // controller class Article_Controller_Front extends MyLibrary_Controller_Action { public function listAction() { $articles = $this->articleModel->getArticles(); // --- Look Here --- // $this->render('article.phtml'); } } // model class Models_Article { public function getArticles() { ... } } // view <html> <body> This is article.phtml </body> </html>
6) Active Record Pattern is used to encapsulate access to a data source.
<?php // Sql call to an abstract object (select) class Models_Article extends Zend_Db_Table { public function getArticles() { $sql = $this->select()->from('article'); } } // Table object class Zend_Db_Table { public function select() { $select = new Zend_Db_Table_Select($this); return $select; } public function insert() { } } // Select object class Zend_Db_Table_Select { public function from($name) { echo "SELECT * FROM {$name}"; // simplefied assemble for learning reasons } } $modelArticle = new Models_Article(); $modelArticle->getArticles();