|
Re: Render yourself ou ask someone else to render you?
<vincent.bouret@gmail.com> wrote in message
news:1134538032.486439.26380@f14g2000cwb.googlegroups.com...
> 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? (BTW, the language is PHP)
>
> 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?
I believe web applications are generally written using the Model View
Controller pattern. It sounds like "Article" is your model, and so you need
to create a View; e.g. "HTMLView" or "PDFView" which can receive an article
and generate and HTML or PDF representation of it.
That's closer to your (1) than your (2), I think.
- Oliver
|