Datasheet
16
x
CHAPTER 1 INTRODUCING SYMFONY, CAKEPHP, AND ZEND FRAMEWORK
conferred by the authors of the frameworks. So we will call all frameworks Model-View-Controller,
even if the Controller does the majority of data-transferring work.
View
Result
Model
Updates
Presenter
Data Manipulation and Delivery
Action
FIGURE 118: Model-View-Presenter pattern
Overview of Other Design Patterns
Design patterns can be divided into creational, behavioral, and structural patterns. Full description
of all design patterns is well beyond the scope of this book, but you can fi nd it in the most infl u-
ential book on this subject: Design Patterns: Elements of Reusable Object-Oriented Software, by
Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides (the Gang Of Four). However, we
want to provide you with just a short overview of design patterns that are commonly used in web
frameworks.
Singleton
This design pattern, which is so trivial it is often
called an antipattern, is very useful. The purpose of
the Singleton pattern is to ensure that a class has only
one instance and to make this instance globally acces-
sible. Whenever another object needs access to the
Singleton, it calls a static, globally accessible function
that returns reference to the single instance. You can
see the structure of the Singleton in Figure 1-19.
The trick behind Singleton is to make the instance and all its constructors private. So there is no
way to demand creation of a
Singleton class instance. How is the fi rst and only instance cre-
ated? The
instance() method checks whether this object already exists; if not, it creates the single
instance before returning it. Let’s look at how this works with the PHP code.
<?php
class CarSingleton {
private $make = ‘Dodge’;
private $model = ‘Magnum’;
private static $car = NULL;
Singleton
+static instance()
Client
FIGURE 119: Singleton pattern structure
c01.indd 16c01.indd 16 1/24/2011 5:45:18 PM1/24/2011 5:45:18 PM