I recently lamented the design that I came up with for outputting HTML: I was combining a class accumulating HTML together with static methods that spit it out as text, leading to an unhappy marriage that I thought I knew how to deal with in C++ but couldn’t in Java.
As I said at the end of that post, though, I thought that I could come up with a reasonably elegant solution using functor classes. And thinking about it more before going to sleep earlier this week convinced me that that should work. So I tried it.
I got halfway through implementing it, though, and it just didn’t seem like it was going well. So I svn reverted, and am leaving things as is for the time being. (Nice to have a source control system that makes it easy to discard abortive experiments.) I did learn something from the experiment, though: I realized that the functor classes that I was building up was really an ungainly representation of the HTML tree. So the new solution was, in a weird way, actually moving away from the accumulating parameter solution: I wasn’t spitting out any HTML until I’d created a big, fat parse tree. It was, ultimately, a lot more like the static string solution: a bit more structure, perhaps, but the structure wasn’t easy to get at.
Which suggests yet another solution: I should separate the presentation of the HTML from its structure. So (given that there’s no performance reason that I need to spit out a textual representation of the HTML as we go), I should have static functions that, instead of returning a string representation of the HTML, return an abstract tree representation. And then I should have a Visitor that turns it into text, doing whatever prettifying it wants (indenting, turning relative links to absolute ones, etc.) at the same time. And there could be other Visitors for the parse structure – e.g. I generate a header and a title that look the same, except that the title can’t have HTML in it, so I could have a remove-elements Visitor that traverses the header to produce the title.
But that’s all a long ways off, and I may never end up doing it; I think I understand the situation well enough that it’s not bothering me any more, so until a story comes along that provides an external motivation for this split, I’ll leave well enough alone.
Post Revisions:
There are no revisions for this post.