The Adapter Pattern in PHP5

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
In June I continued my series of design patterns examples using PHP
5 with the Adapter Pattern.  Here now is my 17th design pattern
example, the Bridge Pattern.

In the Bridge Pattern we have an abstraction and  implementation in
different class hierarchies.

Not something I use all the time, but if you have a situation where a
class varies functionality based on two completely independent needs
it can be useful.

The classis GOF* implementation has a class needing to create
different windows on different platforms.  So, you don't need to redo
anything about the platofrms when you add a new type of window or vice

*GOF - Gang of Four, referring to the four authors of "Elements of
Reusable Object-Oriented Software".

Re: The Adapter Pattern in PHP5

I really like your site, since it provides clean, simple examples of
common patterns.
A few notes:
Pass the code through highlight_string for some basic syntax
highlighting, makes code a lot easier to read.
I realized the one thing it is lacking though: an English description
of the code you're about to read.  The opening blurbs are very terse
and don't explain the process and logic behind the pattern so much as
give a text book definition of what it is.  The code itself is very
explanatory, but a providing a context can help immensely.

Ex: Singleton: In the singleton pattern a class can distribute one
instance of itself to other classes.

Might be better worded as: <what it means> <what it is suppose to do>
<purpose in software designing> <common method of implementation>
<special cases to note>

In the singleton pattern a class can control the instantiation of
itself to the user.  This is akin to having a global instance of an
object, and is used when only a single instance of a class should

The common way of implementing this is storing a static, private
instance of the class within the class and using a static class
getInstance() method to fetch the stored instance.

This is useful for:
Database connections
Configuration classes
Controlling what instance a user gets
When class instances should be shared regardless of context

Feel free to copy/paste that and modify it as you wish.

Re: The Adapter Pattern in PHP5

On 23 Aug 2006 00:41:26 -0700, "Richard Levasseur"

Quoted text here. Click to load it

Some great ideas there!  You are certainly correct, I could well add a
bit more explanation to my examples.  I generally start out with good
intentions, but I try to do an example all in one day when I can, with
the verbiage coming last.  As a result by the time I do the verbiage
I'm not as enthusiastic as I was at the start.  And - as is apparent
from my title for this thread - The Adapter Pattern instead of the
Bridge Pattern - perhaps getting a bit sloppy.  It would probably a
good idea to go over all of my examples when I'm done the set, take a
fresh look, and add a bit more text.

I'd also like to add UML, but I haven't found a UML generator for PHP
that would create jpgs.  And which is free.

Site Timeline