Computer Blog

Thursday, June 4, 2009 - 12:21

Download the source for the Command Pattern.

The Command Pattern is a design pattern implemented for a multitude of purposes: progress bars, wizards, thread pools, multi-level undo, and networking to name a few. By encapsulating the request as an object, the Command Pattern allows us to parameterize other objects with different requests, queue or log those requests, and even support undo functionality.

To get a...

Wednesday, June 3, 2009 - 23:31

Download the source for the Singleton Pattern.

Sometimes you may find yourself in need of an object that can exist once and only once, for example: registry settings, device drivers, or game objects. To do this we use a pattern aptly named the Singleton Pattern. The Singleton utilizes a private constructor and so can only be instantiated through a call to another method. Here is a basic Singleton.class.php:

...
Wednesday, June 3, 2009 - 21:03

Download the source for the Simple Factory Pattern.

The Factory Pattern defines an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses providing a way to decouple the implementation of an object from it’s use allowing for a way to change or add new objects without affecting the Creator. Or in practical...

Tuesday, June 2, 2009 - 18:35

Download the source for the Decorator Pattern.

Ever need to add new or additional functionality to a class dynamically? Need to make a class that is open for extension but closed for modification? Then the Decorator pattern is for you. How? Simple, we will add a new class to wrap, or decorate, the original class.

For our example let’s imagine we have an ice cream stand. We sell a number of different flavors of ice cream: vanilla, chocolate...

Monday, June 1, 2009 - 00:27

Download the source for the Oberver Pattern.

The Observer design pattern is used to define a one-to-many dependency between a set of objects, a subject that contains and controls the state and an observer that uses that state. If the state of the parent object, the Subject, changes it will automatically notify all its dependent objects, the Observers, and depending on the style of notification, the parent object may then have values updated with...

Wednesday, April 15, 2009 - 23:12

One of the biggest complaints I hear about PHP is how ugly it is as a language: function names are not case-sensitive but variables are, there is a haphazard implementation of many features, and having no real structure enforced on PHP developers makes it easy to write messy code. And the critics may be right. I think Rasmus Lerdorf summarizes PHP best when he says:

What...