Design Patterns
April 7th, 2008Design Pattern is quite an interesting topic to talk about for those fellow programmers. These days, most of the imperative programming languages have been implemented the object orented model. Therefore, Design Pattern has played an important role to provide solutions to common problems in OO designs. Gang of Four, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, are well-know gurus when people talk about Design Pattern.
So, what is a Design Pattern?
Christopher Alexander syas “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can user this solution a million time over, without ever doing it the same way twice”. In general, a pattern has four elements, The pattern name, The problem, The solution, and the consequences.
There are 3 main categories of design pattern as follow,
1. Creational Patterns
- Abstract Factory: Creates an instance of several families of classes
- Builder: Separates object construction from its representation
- Factory Method: Creates an instance of several derived classes
- Prototype: A fully initialized instance to be copied or cloned
- Singleton: A class of which only a single instance can exist
2. Structural Patterns
- Adapter: Match interfaces of different classes
- Bridge: Separates an object’s interface from its implementation
- Composite: A tree structure of simple and composite objects
- Decorator: Add responsibilities to objects dynamically
- Facade: A single class that represents an entire subsystem
- Flyweight: A fine-grained instance used for efficient sharing
- Proxy: An object representing another object
3. Behavioral Patterns
- Chain of Responsibility: A way of passing a request between a chain of objects
- Command: Encapsulate a command request as an object
- Interpreter: A way to include language elements in a program
- Iterator: Sequentially access the elements of a collection
- Mediator: Defines simplified communication between classes
- Memento: Capture and restore an object’s internal state
- Observer: A way of notifying change to a number of classes
- State: Alter an object’s behavior when its state changes
- Strategy: Encapsulates an algorithm inside a class
- Template Method: Defer the exact steps of an algorithm to a subclass
- Visitor: Defines a new operation to a class without change
Anyway, I have a little example of how to implement Singleton pattern in PHP. Okay, let’s say I would like to have a page object for my PHP page so that whenever I want to add additional style sheet or JavaScript text to a page, I can place that text into a proper HTML section which is head section rather than mixing it up in the body one. Here is the code,
class App {
function &page(){
static $p;
if (!$p){
require_once 'HTML/Page.php';
$p = new HTML_Page();
$p->setTitle('My Title');
$p->setCharset('iso-8859-1');
$p->addStyleSheet(/style/common.css');
$p->addScript('/js/common.js');
}
return $p;
}
function display(){
// some code here
}
}
and if my particular php page needs more JavaScript, we can do,
$page =& App::page();
$page->addScript('/js/cookie.js');
That’s it! However, this is just a simple implementation. If you want to delve deeper into design patterns, please click links as follow,
Data & Object Factory
http://www.dofactory.com/Patterns/Patterns.aspx
Design Patterns in Wikipedia.org
http://en.wikipedia.org/wiki/Design_pattern_(computer_science)
Design Patterns: Elements of Reusable Object Oriented Software
http://en.wikipedia.org/wiki/Design_Patterns
History of patterns:
http://www.c2.com/cgi-bin/wiki?HistoryOfPatterns
Thanks,
Channa