This is a read-only archive!

Dilemmas continued

I previously prattled on about what the right abstraction should be for an icon theme. I said a hierarchy of subclasses would work.

I was very wrong. It didn't work at all. For example I tried to write a == method for my Icon class. With the class structure I was using before, the only thing I could compare between two Icons is the icon name. In which case, two icons of different sizes in different contexts would have to be considered equal. That's not right. The only other option was to make some kind of is_equal? method all the way at the top of the class hierarchy, and have it construct a complete picture of an icon on the fly, and use that in the comparison. That kind of defeats the whole purpose of setting up the class hierarchy that way to begin with.

A better abstraction is a single Icon class with size and context as properties. An icon needs to know these things about itself. I recently was reading Head First Design Patterns and one of the first "rules" it gives is:

Favor composition over inheritance.

It's right in this case. I was tripped up by the fact that on disk a size does contain a context, and a context does contain an icon. But there's no reason my abstraction needs to match that structure.

On a side note, it's a good book, even though it mostly deals with Java. Many of the "design patterns" it gives are workarounds for the inflexibility of Java itself, but it does give some interesting ways of looking at certain problems.

March 18, 2007 @ 6:13 AM PDT
Cateogory: Programming

5 Comments

Hussam
Quoth Hussam on March 20, 2007 @ 7:09 AM PDT

I have a burning question that no search string on google could answer. Is it correct that a static website (like one generated by WordPress) requires you to re-create the navigation bar for webpage linked to in it?

I know one way would be to use frames but apparently that's obsolete.

Another way is to turn your webpage into an application (AJAX, ASP, JSP) etc.

Brian
Quoth Brian on March 20, 2007 @ 11:38 AM PDT

Not sure what you mean by "navigation bar", or by "webpage linked to it".

Hussam
Quoth Hussam on March 21, 2007 @ 1:28 AM PDT

The navigation menu maybe.. "Home, Code, Themes, Origami". Home, Code.. etc being the webpages linked to in the navigation menu. What else do you call it?

Brian
Quoth Brian on March 21, 2007 @ 4:23 AM PDT

Oh, OK. Wordpress calls it the "sidebar". My Wordpress theme has a header, sidebar, index, and footer template, and each page on this site is built mostly of those. Every time you load a page, those templates are re-read and the HTML is re-generated by PHP.

So when you click Code, it loads a PHP script which happens to re-read all the templates above and generate the HTML for the Code page. When you click Themes, it does the same thing all over again. When you click Origami, you're actually no longer in Wordpress, you're in my Gallery2, which reads its OWN templates, but I wrote those templates to look similar to my Wordpress templates.

So it's not like AJAX or frames, no. But it's simple. And the overhead isn't that much to send a new blob of HTML for every new page; it's a few kilobytes worth of HTML each time. And it's all done server-side, so I don't have to care whether your browser supports some screwy Javascript; your browser only has to support plain old HTML. And this is how the internet was designed to work anyways.

Hussam
Quoth Hussam on March 21, 2007 @ 6:19 AM PDT

Thank you. I just didn't know where to look for an answer. I thought there had to be a better way than what I was doing for the sidebar.

When a person first visits the website, the sidebar contains log-in information. The login info and the links are generated by php functions I wrote. If a person tries to visit subsequent websites (beyond index.php like reservation.php, editinfo.php etc) without being logged in the function checks for that and prints an error message.

I have about 6 pages that a user can access. All of them begin with :

if (check_user_logged_in) { generate_links(); } else { generate_login(); ... // error message }

All this becomes redundant after a while with 10-20 pages, hence my question about your sidebar.