Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- Vincent B
December 14, 2005, 5:24 am
rate this thread
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
I want your opinion on which would be the best solution or the
Re: Render yourself ou ask someone else to render you?
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
I have an example of the visitor pattern in php 5 online at: