|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Non-Member
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jan 2003
Posts: 5,799
|
Design Patterns Catalogue
Hello, I got thinking today that with all the discussion about patterns, that what we need is a catelog of patterns that us PHP developers use day to day, so I've started this thread.
The idea is that members who use a pattern, any will do I suppose so long as the posted example works, has the revalant underlying script, etc and is PHP, will be as kind to post it to this thread? Then, whenever someone reads about a particular pattern, or even post, they can start a thread about it, to discuss their issue(s) further in regards to that pattern. The idea is that this thread will be a catelog of pattern examples, with no discussion, so to avoid bloating this thread. SweatJe is about to publish a new book later this year (summer) on PHP patterns, and I get the idea that this thread could be a supplement to that book, so what someone can't find in SweatJe's book, they'll maybe find it here, in this thread... Anyways, I'll kick things off with two examples ![]() Decorator --------- Purpose of this pattern is to add extra functionality to the decorated class at run time. So you are not altering the class(es) that are to be decorated, thus you are extending the life of the said class(es). PHP Code:
---------- Purpose of this pattern is to separate functionality away from the View. The important point of using this pattern is to remember to separate the domain from the presentation, as I have done. For example you'd use if you wanted to output tabular data with alternating rows of colour. In this case, the Template class would have the presentational HTML for this, with the ListHelper class having the logic to decide which HTML to use, from the Template class. PHP Code:
PHP Code:
Last edited by Dr Livingston; Apr 2, 2005 at 03:11. Reason: Add the purpose of each pattern |
|
|
|
|
|
#2 |
|
SitePoint Mentor
![]() ![]() Join Date: Jun 2004
Location: Copenhagen, Denmark
Posts: 5,916
|
It would probably be better to use a wiki for that ?
|
|
|
|
|
|
#3 |
|
Non-Member
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jan 2003
Posts: 5,799
|
Yep, but I have no available web space at the moment
![]() |
|
|
|
|
|
#4 |
|
SitePoint Enthusiast
![]() Join Date: Jan 2005
Location: Rome
Posts: 77
|
I can donate! I'd be happy to help you with a few Mb!
|
|
|
|
|
|
#5 |
|
SitePoint Zealot
![]() ![]() Join Date: Oct 2004
Location: naperville
Posts: 189
|
I'd be interested in this as well, webspace and all. I'll set up a wiki on philbrodeur.com if no one minds the domain name
If it actually gets going I can set it up as phpwiki.com or whatever. |
|
|
|
|
|
#6 |
|
Non-Member
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jan 2003
Posts: 5,799
|
Well, was hoping to keep it to these forums, if the idea took off, and I hope it does, it'd be a sticky thread
![]() I just thought the forums were the best options, from the point of view of topical discussions of these blighters ![]() |
|
|
|
|
|
#7 |
|
Currently Occupied; Till Sunda
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Aug 2001
Location: London
Posts: 2,511
|
What happened to phppatterns.com, it seems short lived. It would be nice to have a Wiki there (relevant domain), where people can contribute freely, however Harry seems to have disappeared lately?
|
|
|
|
|
|
#8 |
|
SitePoint Zealot
![]() ![]() Join Date: Oct 2002
Posts: 174
|
why don't you use http://www.wiki.cc/php/Main_Page ?
|
|
|
|
|
|
#9 | |
|
SitePoint Evangelist
![]() ![]() ![]() ![]() Join Date: Nov 2003
Location: Los Angeles, CA
Posts: 484
|
Quote:
I've emailed him a couple times but haven't heard anything back and haven't seen anything from him in a while. |
|
|
|
|
|
|
#10 |
|
Non-Member
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jan 2003
Posts: 5,799
|
PHP Patterns is still there as far as I know, but there has been no fresh content for a long time. Not sure what has happened to Harry but hope it's nothing serious.
As for a Wiki, I hope it doesn't go the same way as PHP Patterns. But where ever this catelog ends up, it has to be easily accessable for everyone, and allow discussions as I see it, as without the discussions, it's all pointless really. |
|
|
|
|
|
#11 |
|
SitePoint Zealot
![]() ![]() Join Date: Oct 2004
Location: naperville
Posts: 189
|
The only thing wrong with forums is orginization - each pattern really should have its own thread and then we would need a "master thread".
Edit - I'm not convinced the ratings example is so hot; you're not adding functionality but rather a different type of rating. Is this really the general purpose of the pattern? No offense; I'm still learning some of this stuff. |
|
|
|
|
|
#12 |
|
SitePoint Enthusiast
![]() Join Date: Jun 2004
Location: London
Posts: 66
|
Hi all,
A while back not to long ago i created a sourceforge project. http://sourceforge.net/projects/standardpattern/ With a simular idea in mind patterns with class examples and unit tests using "Open Source" programming languages php, python, ruby ... Never actually done anything with the project. Something along the lines of http://patternshare.org/ would be nice. This is a cool view of all the patterns at patternshare.org http://patternshare.org/default.aspx...rganizingTable Note: patternshare.org smells of Microsoft. http://toolbar.netcraft.com/site_rep...tternshare.org Last edited by davro; Mar 31, 2005 at 15:33. |
|
|
|
|
|
#13 |
|
Massimiliano Bruno Giordano
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Aug 2004
Location: Canada
Posts: 1,490
|
Obligatory spelling correction:
Catalog. |
|
|
|
|
|
#14 |
|
Non-Member
![]() ![]() ![]() ![]() Join Date: Nov 2004
Location: Australia
Posts: 455
|
Obligatory obligatory spelling correction:
Catalogue ![]() |
|
|
|
|
|
#15 | |
|
Non-Member
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jan 2003
Posts: 5,799
|
Quote:
![]() I was actually looking forwards to a lot more enthusiasm for this, and now I'm disappointed that no one else has contributed ![]() Come on gurus, don't be shy, give up your patterns... |
|
|
|
|
|
|
#16 |
|
SitePoint Zealot
![]() ![]() Join Date: Mar 2004
Location: Sweden
Posts: 180
|
Ok, here goes nothing:
Data Mapper PHP Code:
|
|
|
|
|
|
#17 | |
|
SitePoint Guru
![]() Join Date: Mar 2003
Location: London
Posts: 796
|
Quote:
Catalog is the US spelling so could probably be considered acceptable. ![]() ![]() |
|
|
|
|
|
|
#18 |
|
Non-Member
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jan 2003
Posts: 5,799
|
Chain Of Responsibility
--------------------- Using a SWITCH to delegate a decision has it's drawbacks, for example you need to change the script to add another decicion. With the CoR you do not need to change anything, you just add a new handler, provided you adhere to the interface. Btw, this is the 'classic' GoF (Group of Four) pattern I've posted. PHP Code:
![]() PHP Code:
PHP Code:
Last edited by Dr Livingston; Apr 2, 2005 at 12:06. Reason: Add the purpose of this pattern |
|
|
|
|
|
#19 |
|
SitePoint Enthusiast
![]() Join Date: Jun 2004
Location: London
Posts: 66
|
Basic Patterns:
HttpRequest FrontController Controller --------------------- PHP Code:
PHP Code:
PHP Code:
Last edited by davro; Apr 1, 2005 at 16:49. |
|
|
|
|
|
#20 |
|
sitepoint wombat
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jul 2000
Location: Perth Australia
Posts: 1,722
|
sorry dudes, but without an explanation ( goals, raison d`etre ) of each pattern ... its just gibberish to the uneducated viewer ... & if educated then they dont need the example
![]() |
|
|
|
|
|
#21 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Jan 2005
Posts: 267
|
just a suggestion, if you want to do a pattern repository, you might want to include both php4 + php5 examples if possible. alot of people still have to stay within the php4 realm because of shared hosting or other reasons. plus that might add to appeal and help people migrate over to php5 in the long run as well.
and a second suggestion, keep the thread alive, but also maybe use a wiki or repository somewhere else or even another forum attached to this one. that sums up the discussion here, but keep the discussion here cause you never know who might show up with something good to add. i love the idea Dr. L cause idiots like me have to read books like head first design patterns and professional php5 |
|
|
|
|
|
#22 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Aug 2004
Location: California
Posts: 1,672
|
I certainly like to see code so appreciate the postings. I also to agree firepages that without the simple description, explaining the problem and describing how it works the are not much help. Patterns are not implementations and showing implementations without context reduces the communication value of patterns.
I also have some questions about the patterns listed and I think it is the discussion if the ideas of the pattern that are of value. For instance, overrunner's Data Mapper look a lot like the Table Data Gateway pattern to me. Table Data Gateway is a fairly simple pattern to describe and give examples of. Data Mapper is considerably more complex and is often implemented used a combination of patterns. In fact several different combinations are possible. Dr Livingston's is a good example of Chain Of Responsibility. I would be interested in seeing other examples. There was a discussion recently that for the request the Intercepting Filter is a better choice, but for controllers Chain Of Responsibility is the way to go. So showing Chain Of Responsibility combined with the Command pattern would be of interest. Of davro's patterns only Front Controller is a pattern that I know. I do not think HTTP Request and Controller are patterns (correct me if I'm wrong). And the Front Controller as shown does not seem to dispatch which is a central function of the Front Controller pattern. Though I like the idea of having a base Controller class for Front, Page and Application Controllers. |
|
|
|
|
|
#23 | |
|
Non-Member
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jan 2003
Posts: 5,799
|
Quote:
PHP Code:
PHP Code:
![]() |
|
|
|
|
|
|
#24 |
|
SitePoint Enthusiast
![]() Join Date: Jun 2004
Location: London
Posts: 66
|
Hi all,
Sorry i can see how my last post might have added some entropy to the domain. Hopefully this post might help things a little, or maybe not. Abstract Controller Patterns: FrontController http://www.martinfowler.com/eaaCatal...ontroller.html ApplicationController http://www.martinfowler.com/eaaCatal...ontroller.html PageController http://www.martinfowler.com/eaaCatal...ontroller.html --------------------- PHP Code:
PHP Code:
PHP Code:
Last edited by davro; Apr 3, 2005 at 10:09. |
|
|
|
|
|
#25 |
|
Non-Member
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jan 2003
Posts: 5,799
|
Composite
---------- This is a useful pattern to learn where you need to construct hierarchy data structures. The example I've posted below is for an XML file, which I use to construct a web page on the fly, but the hierarchy structure could be from the database. It doesn't matter really. First, the XML file Code:
<?xml version="1.0" encoding="utf-8"?> <fragments> <fragment> <id>page</id> <parent></parent> <controller></controller> <fragment> <id>header</id> <parent>page</parent> <controller></controller> </fragment> <fragment> <id>body</id> <parent>page</parent> <controller></controller> <fragment> <id>partition</id> <parent>body</parent> <controller></controller> </fragment> </fragment> <fragment> <id>footer</id> <parent>page</parent> <controller></controller> </fragment> </fragment> </fragments> PHP Code:
PHP Code:
EDIT: 22/01/06 If you find that you want to apply a number of filters prior to the controller, via a number of decorators, then you could take a what changes I made later; Note that there is no controller node as such from earlier examples, as the controller it's self is applied as a filter as well. Code:
... <fragment> <id>header</id> <parent>page</parent> <filters> <filter name="PageController" pathname="pathname/to/file" /> <filter name="FilterOneController" pathname="pathname/to/file" /> <filter name="FilterTwoController" pathname="pathname/to/file" /> </filters> </fragment> ... ush( $fragment ); you would need to do the following before you add the composite to the composite structure, it's self;PHP Code:
Code:
... <filters> <filter name="Controller" pathname="controllers/" /> <filter name="ThirdController" pathname="controllers/" /> <filter name="SecondController" pathname="controllers/" /> <filter name="FirstController" pathname="controllers/" /> </filters> ... Code:
Array
(
[0] => FirstController Object
(
[decorated:private] => SecondController Object
(
[decorated:private] => ThirdController Object
(
[decorated:private] => Controller
)
)
)
)
PHP Code:
Yours, Dr Livingston ![]() Last edited by Dr Livingston; Jan 22, 2006 at 12:44. |
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 19:57.









If it actually gets going I can set it up as phpwiki.com or whatever.
ush( $fragment ); you would need to do the following before you add the composite to the composite structure, it's self;


Linear Mode
