Page 1 of 2
Writing Code The Uses Design Patterns
Posted: Sat Sep 23, 2006 8:25 am
by onion2k
I'm looking for some information (a tutorial, article, book, whatever) about actually writing code using design patterns. So far all the things I've read consist of little more than a list of different patterns and what they do. That's helpful in it's own way, but it'd not really helping me write code using various patterns. For example..
http://www.patternsforphp.com/wiki/Composite
The first example given is of a set of Book, Chapter, and Page classes that all extend a composite object (I think, the exampe code isn't finished yet) to get isComposite() and output() methods. Why would you do that? Why not, for example, make the Book object a registry containing Chapter and Page objects? Or even an iterator?
Is there anything around that describes the process of deciding what pattern to use in different situations?
Posted: Sat Sep 23, 2006 9:32 am
by neophyte
Hmmm, I be learning this too. I keep hearing that such things come with experience.
I came across a book the other day that I think is what you are looking for:
http://www.linuxworld.com/news/2006/082806-php5.html
This one was written by the codemonkey behind Digg --Elliot White. I haven't read this one but it sounds interesting.
I also recently got this book. It was worth the price.
PHP5 Objects Patterns and Practice
http://www.phpkitchen.com/index.php?/ar ... ctice.html
I think TheNinjaSpaceGoat liked this one too.
Posted: Sat Sep 23, 2006 10:00 am
by Ambush Commander
I'm currently working on rewriting the article, but here's the quick spin:
Why would you do that?
1. It's an accurate way of representing of a hierarchy
2. It lets you treat compositions of objects and regular objects the same
Why not, for example, make the Book object a registry containing Chapter and Page objects?
This would mean you'd flatten it out: a chapter would be on the same level of page, which, intuitively speaking, doesn't make much sense.
Or even an iterator?
The iterator is not the data structure, but you're right: Iterator is often used to traverse all the components of a composite.
Is there anything around that describes the process of deciding what pattern to use in different situations?
That's the hardest thing to write.
By the way, the DOM PHP extension is actually the perfect example of a composite: all the component elements extend from DOMNode, so you can treat them all the same if you need to recursively descend into the structure.
Posted: Sat Sep 23, 2006 11:38 am
by Ambush Commander
Article rewritten. Maybe that will clear things up.
Posted: Sat Sep 23, 2006 11:42 am
by Maugrim_The_Reaper
I suggest reading about the principles which underpin Patterns - there's nothing on PFP yet, but Zend Devzone has a good article written by Matt Zandstra. It's very hard to talk about writing code around patterns. It's a bit of a vague area picked up through experience but knowing why a pattern is ultimately useful is the first step.
It's like you added above about Books, Chapter and Pages. You viewed them as same-level items which suggests Registry, but if you view Book and Chapters as "composites" of lower level Page objects which all have a common element (Text), then to treat them as similar entities the Composite Pattern makes sense. Of course, the example is a bit contrived and there are far more informative examples. But the point is I can output any Book, Chapter or Page by calling one method - output(). How would that be handled by your Registry example in contrast?
Posted: Sat Sep 23, 2006 12:47 pm
by Christopher
There is a book by Jason Sweat (sweatje here) that covers coding with pattern in PHP.
Posted: Sat Sep 23, 2006 1:26 pm
by onion2k
arborint wrote:There is a book by Jason Sweat (sweatje here) that covers coding with pattern in PHP.
Yes, I've got it. The further into the book I go the more I find the descriptive elements about various patterns quite useful, but I've also found it suffers the all too typical problem of software development texts in so far as it jumps around far too much. For example, Jason introduces the Strategy pattern by mentioning a typical implentation of a menu object which is a clear real-world example, and then completely changes his mind and goes on to explain a variable cache instead. That sort of thing really bugs me. He also intertwines the pattern implementation examples with lots of unit testing code. For someone interested in learning TDD and patterns I'm sure that's grand, but if you're just interested in getting to grips with patterns is makes life a lot more difficult. The book really should have been titled "PHP Design Patterns, Oh And Unit Testing Too".
Posted: Sat Sep 23, 2006 1:32 pm
by alex.barylski
Maybe you could browse the MSDN patterns repository...the examples usually revolve around ASP.NET but their clear and in typicaly MSDN fashion, well done.

Posted: Sat Sep 23, 2006 1:51 pm
by Chris Corbyn
Hockey wrote:Maybe you could browse the MSDN patterns repository...the examples usually revolve around ASP.NET but their clear and in typicaly MSDN fashion, well done.

I haven't looked there but many pattern examples are demonstrated in Java but if you don't know Java and they have a little UML to explain what's happening you can usually implement the same thing in PHP(5).
Posted: Sat Sep 23, 2006 3:46 pm
by alex.barylski
d11wtq wrote:Hockey wrote:Maybe you could browse the MSDN patterns repository...the examples usually revolve around ASP.NET but their clear and in typicaly MSDN fashion, well done.

I haven't looked there but many pattern examples are demonstrated in Java but if you don't know Java and they have a little UML to explain what's happening you can usually implement the same thing in PHP(5).
The Java respository is good as well, I agree.
I don't find the language really gets in the way, as anyone (once they understand basic programming concepts/constructs) can usually follow along.
What I found interesting/confusing was that both Sun and M$ have their own definitions for systems, paradigms, etc...
For instance Java defined the model 1 and model 2 architectures...and promote the front controller more than MSDN. Anyones understanding will only be improved after reading something from two entirely different resources.
Java + MSDN = Golden
Cheers

Posted: Sun Sep 24, 2006 4:07 pm
by neophyte
Just throwing PHP5 objects patterns and practice back out there. It is perfect for someone trying to get their head around PHP5 and OO coding. Just touches on TDD and so on. Most of the book is an explanation of patterns. But it also explains PHP5 objects as well. Good book.
Posted: Sun Sep 24, 2006 4:11 pm
by Luke
neophyte wrote:Just throwing PHP5 objects patterns and practice back out there. It is perfect for someone trying to get their head around PHP5 and OO coding. Just touches on TDD and so on. Most of the book is an explanation of patterns. But it also explains PHP5 objects as well. Good book.
I can second that... I still read throught it while on the pot.
Posted: Mon Sep 25, 2006 7:59 am
by ibbo
Posted: Mon Sep 25, 2006 8:28 pm
by Ambush Commander
As far as I can tell, that website is dead... it has lots of good content though. :-/
Posted: Tue Sep 26, 2006 5:03 am
by ibbo
Yes there has been no updates for a while but it does have some good examples to get you started.
ibbo