|
Posted by FluffyCat on 12/15/05 07:54
On 13 Dec 2005 21:24:30 -0800, "Vincent B" <vincent.bouret@gmail.com>
wrote:
>Hi,
>
>Here is a quick OO design question. Suppose I have a few objects that I
>would want rendered in HTML (Article, Category). What would be the best
>OO way to render them?
>
>If I am not mistaken,
>
>1) the Strategy pattern would instruct me to pass the rendering to
>another object. So I would have HTMLArticleRenderer that is constructed
>using an Article object and that will render using
>HTMLArticleRenderer->render(). If I wanted to add a PDF output, I could
>just add a PDFArticleRenderer and switch renderer at runtime.
>
>2) the Decorator pattern would instruct me to add a render() method to
>every object (Article, Category) so they could be called in a tree-like
>fashion.
>
>I want your opinion on which would be the best solution or the
>cleanest?
>
>Regards,
>
>Vincent
Not to add to your delema, but the visitor pattern could be another
way for you to go with this. In the visitor pattern you could have a
htmlVisitor and a pdfVisitor. Your article and category classes have
an accept() function to accept whichever visitor you will be using and
then call that visitor to do the formatting.
The advantage of the visitor pattern is that you can add a new visitor
- such as xmlVisitor - without changing your article or category
classes.
I have an example of the visitor pattern in php 5 online at:
http://www.fluffycat.com/PHP-Design-Patterns/Visitor/
-Larry Truett
[Back to original message]
|