HOME | ABOUT | SERVICES | PORTFOLIOS | FAQ | CONTACT | BLOG


Design Patterns

April 7th, 2008

Design 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

JavaScript - Functional Programming

April 7th, 2008

I’ve started to use JavaScript since 2001 for my www.everyday.com.kh assignment while I was working for Interquess Enterprises, The holding company of The Cambodia Yellowpages. It made me all excited about its abilities in the client side interaction such form validation, animation, and so on. However, at that time it gave me a hard time when it comes to cross-browser issue.

After a while, AJAX technique has been introduced to the web development to make a desktop-like application which is really really cool. Since its popularity in AJAX, people started to build some sorts of JavaScript libraries in order to make life a bit easier with cross-browser, object selector issues…etc. There are so many libraries out there such as Prototype, jQuery, and so on. I happened to love jQuery library www.jquery.com, which was developed by John Resig. jQuery is a light weight library, yet powerful.

Additionally, the interesting thing about JavaScript is, it has been implemented a lot of ideas of Functional Programming such as Higher Order Function, Closure, Curry with partially applied technique…etc. To learn more about this, follow the following links,

What is Functional Programming?
http://en.wikipedia.org/wiki/Functional_programming

Curried JavaScript function:
http://www.svendtofte.com/code/curried_javascript/

Partial Application in JavaScript:
http://ejohn.org/blog/partial-functions-in-javascript/

Thanks,
Channa