Writing Code The Uses Design Patterns
Moderator: General Moderators
Writing Code The Uses Design Patterns
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?
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?
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.
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.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
I'm currently working on rewriting the article, but here's the quick spin:
2. It lets you treat compositions of objects and regular objects the same

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.
1. It's an accurate way of representing of a hierarchyWhy would you do that?
2. It lets you treat compositions of objects and regular objects the same
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.Why not, for example, make the Book object a registry containing Chapter and Page objects?
The iterator is not the data structure, but you're right: Iterator is often used to traverse all the components of a composite.Or even an iterator?
That's the hardest thing to write.Is there anything around that describes the process of deciding what pattern to use in different situations?
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.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
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?
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?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
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".arborint wrote:There is a book by Jason Sweat (sweatje here) that covers coding with pattern in PHP.
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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).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.
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
The Java respository is good as well, I agree.d11wtq wrote: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).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 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
I can second that... I still read throught it while on the pot.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.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US