Connecting Tech Pros Worldwide Forums | Help | Site Map

The Adapter Pattern in PHP5

FluffyCat
Guest
 
Posts: n/a
#1: Aug 22 '06
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.

http://www.fluffycat.com/PHP-Design-Patterns/Bridge/

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
versa.

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

Richard Levasseur
Guest
 
Posts: n/a
#2: Aug 23 '06

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.
And:
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
exist.

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.

FluffyCat
Guest
 
Posts: n/a
#3: Aug 24 '06

re: The Adapter Pattern in PHP5


On 23 Aug 2006 00:41:26 -0700, "Richard Levasseur"
<richardlev@gmail.comwrote:
Quote:
>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.
>And:
>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
>exist.
>
>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.

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.
Closed Thread


Similar PHP bytes