Archive for November, 2011

Design Pattern: Template in PHP

Next in the Head First Design Patterns series is the Template Pattern. The template pattern defines the skeleton of an algorithm in a method and defers some of the implementation of the steps to the subclasses. The subclasses can redefine certain steps of the algorithm without changing the algorithm’s structure.

Design Pattern: State in PHP

Next on from the Head First Design Patterns is the “State Pattern”. The State Pattern allows an object to alter its behavior when its internal state changes. By altering its behavior, the object can perform a different set of operations. To truly understand this pattern, let’s take a look at an example. A sales order can have many different states such as “New Order”, “Invoicing”, “Processing”, “Shipped”. Before the sales order can be moved forward to a different state, there are strict rules that need to be obeyed.

Design Pattern: Decorator in PHP

Now, on the next series from the Head First Design Patterns book – the decorator pattern. The decorator pattern is used to attach additional responsibilities to an object dynamically. This provides a more flexible alternative to subclassing in order to add on more functionality.

Design Pattern: Facade in PHP

Continuing with the Head First Design Patterns book is the facade pattern. Facade is another design pattern in PHP. The facade pattern is used to provide a simplified interface and to hide the complexity of the actual work going on behind. When does the facade get used? The most basic example is your computer. Say you have Windows 7. Windows 7 provides an interface for you to find your files, to change your background and so forth. But beneath Windows 7, there are multiple independent applications that actually do the work, but Windows 7 provides a unified theme for all these interactions.

Design Pattern: Adapter in PHP

The next part in the Head First Design Pattern series covers the “Adapter Pattern”. Say for example you worked on an API. The API is out in the wild and is being used widely. Then the changes for the API comes rolling in and you have to redesign the implementation. But, since you already have users using the API, you need to resort to a less disruptive solution. What do you do? Rewrite the old functions? An easier way would be to create an interface that would convert the old public interface of your API to the new interface.

Categories