DESIGN PATTERNS in PHP

In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. The advantage of knowing and using these patterns is save time as well as give developers a common language in software design. Here I discuss the implementation of some common design patterns that are easily adapted to PHP.

1. Strategy Pattern:

The strategy pattern is typically used when your programmer’s algorithm
should be interchangeable with different variations of the algorithm. For
example, if you have code that creates an image, under certain circumstances, you might want to create JPEGs and under other circumstances, you might want to create GIF files.
The strategy pattern is usually implemented by declaring an abstract
base class with an algorithm method, which is then implemented by inheriting concrete classes. At some point in the code, it is decided what concrete strategy is relevant; it would then be instantiated and used wherever relevant.
Our example shows how a download server can use a different file selection
strategy according to the web client accessing it. When creating the
HTML with the download links, it will create download links to either .tar.gz
files or .zip files according to the browser’s OS identification. Of course, this
means that files need to be available in both formats on the server. For simplicity’s sake, assume that if the word “Win” exists in $_SERVER[“HTTP_
USER_AGENT”], we are dealing with a Windows system and want to create .zip
links; otherwise, we are dealing with systems that prefer .tar.gz.
In this example, we would have two strategies: the .tar.gz strategy and
the .zip strategy, which is reflected as the following strategy hierarchy (see
Figure 1).


Figure 1: Strategy hierarchy.

The following code snippet should give you an idea of how to use such a
strategy pattern:
abstract class FileNamingStrategy {
abstract function createLinkName($filename);
}
class ZipFileNamingStrategy extends FileNamingStrategy {
function createLinkName($filename)
{
return “http://downloads.foo.bar/$filename.zip”;
}
}
class TarGzFileNamingStrategy extends FileNamingStrategy {
function createLinkName($filename)
{
return “http://downloads.foo.bar/$filename.tar.gz”;
}
}
if (strstr($_SERVER[“HTTP_USER_AGENT”], “Win”)) {
$fileNamingObj = new ZipFileNamingStrategy();
} else {
$fileNamingObj = new TarGzFileNamingStrategy();
}
$calc_filename = $fileNamingObj->createLinkName(“Calc101”);
$stat_filename = $fileNamingObj->createLinkName(“Stat2000″);
print <<<EOF
<h1>The following is a list of great downloads<</h1>
<br>
<a href=”$calc_filename”>A great calculator</a><br>
<a href=”$stat_filename”>The best statistics application</a><br>
<br>
EOF;
Accessing this script from a Windows system gives you the following
HTML output:
<h1>The following is a list of great downloads<</h1>
<br>
<a href=”http://downloads.foo.bar/Calc101.zip”>A great calculator<
/a><br>
<a href=”http://downloads.foo.bar/Stat2000.zip”>The best statistics
application</a><br>
<br>

Tip: The strategy pattern is often used with the factory pattern, which is
described later in this section. The factory pattern selects the correct strategy.

2. Singleton Pattern:
The singleton pattern is probably one of the best-known design patterns.
You have probably encountered many situations where you have an object that
handles some centralized operation in your application, such as a logger
object. In such cases, it is usually preferred for only one such application-wide
instance to exist and for all application code to have the ability to access it.
Specifically, in a logger object, you would want every place in the application
that wants to print something to the log to have access to it, and let the centralized
logging mechanism handle the filtering of log messages according to
log level settings. For this kind of situation, the singleton pattern exists.
Making your class a singleton class is usually done by implementing a
static class method getInstance(), which returns the only single instance of
the class. The first time you call this method, it creates an instance, saves it in
a private static variable, and returns you the instance. The subsequent
times, it just returns you a handle to the already created instance.
Here’s an example:
class Logger {
static function getInstance()
{
if (self::$instance == NULL) {
self::$instance = new Logger();
}
return self::$instance;
}
private function __construct()
{
}
private function __clone()
{
}
function Log($str)
{
// Take care of logging
}
static private $instance = NULL;
}
Logger::getInstance()->Log(“Checkpoint”);

The essence of this pattern is Logger::getInstance(), which gives you
access to the logging object from anywhere in your application, whether it is
from a function, a method, or the global scope.
In this example, the constructor and clone methods are defined as private.
This is done so that a developer can’t mistakenly create a second
instance of the Logger class using the new or clone operators; therefore, getInstance() is the only way to access the singleton class instance.

3. Factory Pattern:

Polymorphism and the use of base class is really the center of OOP. However,
at some stage, a concrete instance of the base class’s subclasses must be created.
This is usually done using the factory pattern. A Factory class has a
static method that receives some input and, according to that input, it decides
what class instance to create (usually a subclass).
Say that on your web site, different kinds of users can log in. Some are
guests, some are regular customers, and others are administrators. In a common
scenario, you would have a base class User and have three subclasses:
GuestUser, CustomerUser, and AdminUser. Likely User and its subclasses would
contain methods to retrieve information about the user (for example, permissions
on what they can access on the web site and their personal preferences).
The best way for you to write your web application is to use the base class
User as much as possible, so that the code would be generic and that it would
be easy to add additional kinds of users when the need arises.
The following example shows a possible implementation for the four User
classes, and the UserFactory class that is used to create the correct user object
according to the username:
abstract class User {
function __construct($name)
{
$this->name = $name;
}
function getName()
{
return $this->name;
}
// Permission methods
function hasReadPermission()
{
return true;
}
function hasModifyPermission()
{
return false;

}
function hasDeletePermission()
{
return false;
}
// Customization methods
function wantsFlashInterface()
{
return true;
}
protected $name = NULL;
}
class GuestUser extends User {
}
class CustomerUser extends User {
function hasModifyPermission()
{
return true;
}
}
class AdminUser extends User {
function hasModifyPermission()
{
return true;
}
function hasDeletePermission()
{
return true;
}
function wantsFlashInterface()
{
return false;
}
}
class UserFactory {
private static $users = array(“Andi”=>”admin”, “Stig”=>”guest”,
“Derick”=>”customer”);
static function Create($name)
{
if (!isset(self::$users[$name])) {
// Error out because the user doesn’t exist
}
switch (self::$users[$name]) {

case “guest”: return new GuestUser($name);
case “customer”: return new CustomerUser($name);
case “admin”: return new AdminUser($name);
default: // Error out because the user kind doesn’t exist
}
}
}
function boolToStr($b)
{
if ($b == true) {
return “Yes\n”;
} else {
return “No\n”;
}
}
function displayPermissions(User $obj)
{
print $obj->getName() . “‘s permissions:\n”;
print “Read: ” . boolToStr($obj->hasReadPermission());
print “Modify: ” . boolToStr($obj->hasModifyPermission());
print “Delete: ” . boolToStr($obj->hasDeletePermission());
}
function displayRequirements(User $obj)
{
if ($obj->wantsFlashInterface()) {
print $obj->getName() . ” requires Flash\n”;
}
}
$logins = array(“Andi”, “Stig”, “Derick”);
foreach($logins as $login) {
displayPermissions(UserFactory::Create($login));
displayRequirements(UserFactory::Create($login));
}
Running this code outputs
Andi’s permissions:
Read: Yes
Modify: Yes
Delete: Yes
Stig’s permissions:
Read: Yes
Modify: No
Delete: No
Stig requires Flash
Derick’s permissions:
Read: Yes
Modify: Yes
Delete: No
Derick requires Flash
This code snippet is a classic example of a factory pattern. You have a class
hierarchy (in this case, the User hierarchy), which your code such as displayPermissions() treats identically. The only place where treatment of the classes differ is in the factory itself, which constructs these instances. In this example, the factory checks what kind of user the username belongs to and creates its class accordingly. In real life, instead of saving the user to user-kind mapping in a static array, you would probably save it in a database or a configuration file.

Tip: Besides Create(), you will often find other names used for the factory
method, such as factory(), factoryMethod(), or createInstance().

4. Observer Pattern:

PHP applications, usually manipulate data. In many cases, changes to one
piece of data can affect many different parts of your application’s code. For
example, the price of each product item displayed on an e-commerce site in the
customer’s local currency is affected by the current exchange rate. Now,
assume that each product item is represented by a PHP object that most likely
originates from a database; the exchange rate itself is most probably being
taken from a different source and is not part of the item’s database entry. Let’s
also assume that each such object has a display() method that outputs the
HTML relevant to this product.
The observer pattern allows for objects to register on certain events
and/or data, and when such an event or change in data occurs, it is automatically
notified. In this way, you could develop the product item to be an observer
on the currency exchange rate, and before printing out the list of items, you
could trigger an event that updates all the registered objects with the correct
rate. Doing so gives the objects a chance to update themselves and take the
new data into account in their display() method.
Usually, the observer pattern is implemented using an interface called
Observer, which the class that is interested in acting as an observer must
implement.
For example:

interface Observer {
function notify($obj);
}
An object that wants to be “observable” usually has a register method
that allows the Observer object to register itself. For example, the following
might be our exchange rate class:

class ExchangeRate {
static private $instance = NULL;
private $observers = array();
private $exchange_rate;
private function ExchangeRate() {
}
static public function getInstance() {
if (self::$instance == NULL) {
self::$instance = new ExchangeRate();
}
return self::$instance;
}
public function getExchangeRate() {
return $this->$exchange_rate;
}
public function setExchangeRate($new_rate) {
$this->$exchange_rate = $new_rate;
$this->notifyObservers();
}
public function registerObserver($obj) {
$this->observers[] = $obj;
}
function notifyObservers() {
foreach($this->observers as $obj) {
$obj->notify($this);
}
}
}
class ProductItem implements Observer {
public function __construct() {
ExchangeRate::getInstance()->registerObserver($this);
}
public function notify($obj) {
if ($obj instanceof ExchangeRate) {
// Update exchange rate data
print “Received update!\n”;
}
}
}
$product1 = new ProductItem();
$product2 = new ProductItem();
ExchangeRate::getInstance()->setExchangeRate(4.5);
This code prints
Received update!
Received update!

Although the example isn’t complete (the ProductItem class doesn’t do
anything useful), when the last line executes (the setExchangeRate() method), both $product1 and $product2 are notified via their notify() methods with the new exchange rate value, allowing them to recalculate their cost.
This pattern can be used in many cases; specifically in web development,
it can be used to create an infrastructure of objects representing data that
might be affected by cookies, GET, POST, and other input variables.

Thanks to the advances of PHP 5, using common OO methodologies, such as
design patterns, has now become more of a reality than with past PHP versions. For more you can visit http://www.cetus-links.org/.

Design Pattern – Part 1

In software engineering, a design pattern is a general reusable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.

Patterns originated as an architectural concept by Christopher Alexander (1977/79). In 1987, Kent Beck and Ward Cunningham began experimenting with the idea of applying patterns to programming and presented their results at the OOPSLA conference that year. Design patterns gained popularity in computer science after the book Design Patterns: Elements of Reusable Object-Oriented Software was published in 1994.

Classification:

Design Patterns originally grouped design patterns into the categories Creational Patterns, Structural Patterns, and Behavioral Patterns, and described them using the concepts of delegation, aggregation, and consultation.

Creational patterns

These patterns have to do with class instantiation. They can be further divided into class-creation patterns and object-creational patterns. While class-creation patterns use inheritance effectively in the instantiation process, object-creation patterns use delegation to get the job done.

  • Abstract Factory groups object factories that have a common theme.
  • Builder constructs complex objects by separating construction and representation.
  • Factory Method creates objects without specifying the exact class to create.
  • Prototype creates objects by cloning an existing object.
  • Singleton restricts object creation for a class to only one instance.

Structural patterns

These concern class and object composition. They use inheritance to compose interfaces and define ways to compose objects to obtain new functionality.

  • Adapter allows classes with incompatible interfaces to work together by wrapping its own interface around that of an already existing class.
  • Bridge decouples an abstraction from its implementation so that the two can vary independently.
  • Composite composes one-or-more similar objects so that they can be manipulated as one object.
  • Decorator dynamically adds/overrides behaviour in an existing method of an object.
  • Facade provides a simplified interface to a large body of code.
  • Flyweight reduces the cost of creating and manipulating a large number of similar objects.
  • Proxy provides a placeholder for another object to control access, reduce cost, and reduce complexity.

Behavioral patterns

These design patterns are about classes objects communication. They are specifically concerned with communication between objects.

  • Chain of responsibility delegates commands to a chain of processing objects.
  • Command creates objects which encapsulate actions and parameters.
  • Interpreter implements a specialized language.
  • Iterator accesses the elements of an object sequentially without exposing its underlying representation.
  • Mediator allows loose coupling between classes by being the only class that has detailed knowledge of their methods.
  • Memento provides the ability to restore an object to its previous state (undo).
  • Observer is a publish/subscribe pattern which allows a number of observer objects to see an event.
  • State allows an object to alter its behavior when its internal state changes.
  • Strategy allows one of a family of algorithms to be selected on-the-fly at runtime.
  • Template method defines the skeleton of an algorithm as an abstract class, allowing its subclasses to provide concrete behavior.
  • Visitor separates an algorithm from an object structure by moving the hierarchy of methods into one object.

Concurrency patterns

Active Object: The Active Object design pattern decouples method execution from method invocation that reside in their own thread of control. The goal is to introduce concurrency, by using asynchronous method invocation and a scheduler for handling requests.

Balking: The Balking pattern is a software design pattern that only executes an action on an object when the object is in a particular state.

Double checked locking : Double-checked locking is a software design pattern also known as “double-checked locking optimization”. The pattern is designed to reduce the overhead of acquiring a lock by first testing the locking criterion (the ‘lock hint’) in an unsafe manner; only if that succeeds does the actual lock proceed.

The pattern, when implemented in some language/hardware combinations, can be unsafe. It can therefore sometimes be considered to be an anti-pattern.

Guarded In concurrent programming, guarded suspension is a software design pattern for managing operations that require both a lock to be acquired and a precondition to be satisfied before the operation can be executed.

Monitor object A monitor is an approach to synchronize two or more computer tasks that use a shared resource, usually a hardware device or a set of variables.

Read write lock A read/write lock pattern or simply RWL is a software design pattern that allows concurrent read access to an object but requires exclusive access for write operations.

Scheduler The scheduler pattern is a software design pattern. It is a concurrency pattern used to explicitly control when threads may execute single-threaded code.

Thread pool In the thread pool pattern in programming, a number of threads are created to perform a number of tasks, which are usually organized in a queue. Typically, there are many more tasks than threads.

Thread-specific storage Thread-local storage (TLS) is a computer programming method that uses static or global memory local to a thread.

Reactor The reactor design pattern is a concurrent programming pattern for handling service requests delivered concurrently to a service handler by one or more inputs. The service handler then demultiplexes the incoming requests and dispatches them synchronously to the associated request handlers.