You are currently browsing the category archive for the ‘Design Patterns’ category.

The other day I was thinking wayyyy too hard about nothing in particular, and I thought about the simple ambiguity of naming ourselves.

From the movie Orgasmo:

Maxxx Orbison:
What’s your name, again?
Sancho:
I am Sancho.
Maxxx Orbison:
Look, I get a lot of people auditioning all the time. What makes you think that you’d be good enough for porno?
Sancho:
I am Sancho.
Maxxx Orbison:
Great… but what do you do?
Sancho:
What do I do? I am Sancho.
Maxxx Orbison:
And…?
Sancho:
And there are many Jeffs in the world, and many Toms as well. But I… am Sancho.
Maxxx Orbison:
And…?
Sancho:
Are you Sancho? No you are not. Neither is Scott Baio Sancho. Frank Gifford is not Sancho. But I…
Maxxx Orbison:
You… are Sancho!
Sancho:
That’s right.
Maxxx Orbison:
Okay, you’re hired.

In a nutshell, Sancho is stating something more abstract than just his name. I can say “I am Tom Barta”, but typically I just mean “My name is Tom Barta.” This guy isn’t just named Sancho, he is Sancho, and there is no other.

Think about it: what do you really mean if you go up to a stranger and ask, “Who are you?” The answer could be “John”, “a businessman”, “your neighbor”, or “a pround Republican”. Similarly, if you approach someone and say “Hi, I am John,” I can imagine it would take a tiny bit more processing than saying “Hi, my name is John.” If your name is rare (like Moon Unit or Apple), it’s even more important to remove any ambiguity. “I am Apple” sounds like nonsense or pidgin.

There’s a Nerd-Tangent Hidden in Every Real-Life Thought

There is a parallel between this and programming. The advent of object-oriented programming has popularized the notion of “object identity” versus “object equality”. I can have two objects sitting in memory with identical data. Sometimes, that’s just a programming error, and the same object has been copied unnecessarily (this happens frequently with caching or persistent systems). Sometimes, the two objects genuinely are different. How can I tell if one is just an alias, or if they are logically distinct?

It depends, of course. If I am looking at Value Objects, there’s generally not a reason to distinguish them by identity. The color Red is always Red, even if I have two Reds. However, with Entities, identity is of utmost importance. Two John Smiths in a customer database represent different people. Another way to think about it is in the context of the Flyweight pattern. The two Reds could be replaced with a flyweight without affecting the program. However, the John Smiths couldn’t.

Enter Programming Languages

Of course, programming languages that use objects must have some way of distinguishing object identity from object equality.

Language Identity Equality
C, C++ &a == &b a == b
Java Anyone care to fill this one in for me? I’m unaware of the semantics of == and equals().
PHP nothing! a == b (coerce types to match) or a === b (check types)
Python a is b a == b

Whoops! Looks like PHP doesn’t even have object identity! I’d like for someone to be able to refute this, but I haven’t been able to figure it out. PHP documentation claims === means identical and == means equal, but that certainly doesn’t match the notion of object identity I just explained. Sadly, this essentially means that object identity will never truly work in PHP. Instead, we are left with “equal” and “more equal”.

Does it Matter?

In the big picture, I don’t think it hurts PHP programmers to lose object identity. Most PHP applications are business-logic interfaces sitting on top of relational databases. What’s special about the RDBMS in this context? Well, object identity doesn’t exist. I know Postgres has oid and there are probably others, but using them for general applications seems to be unfavorable. In a database table, objects (tuples/records/rows) are identified by a primary key that disallows duplicates and frequently uses auto-incrementing integers. It’s a trivial solution, really, to just assign a number to everyone who walks in the door (until you run out of numbers, of course).

Since the database enforces this uniqueness, I know that two customers both named John Smith will at least have different customer IDs. Social Security, credit cards, university student IDs, and phone numbers all revolve around this notion of unique numeric identity. Consequently, almost any PHP application using a RDBMS can simply piggyback upon the database’s IDs and trivially state that === is now identical to ==.

Advertisements

I just stumbled upon Martijn van Welie’s Patterns in Interaction Design, which contains quite a few useful design patterns for web designers and developers. It’s mostly on the designer side, but most of the patterns could be implemented in anyone’s web development framework. Most of the web dev frameworks that I’ve seen are based around setting up MVC and automating CRUD (and perhaps I am generalizing too much), and not enough around the littler things, like consistent <title> or navigation or URIs.

Edit: A little more searching and I found Nathan Wallace’s Design Patterns in Web Programming. It’s mostly just a skeleton for pattern ideas, but looks to be a good start, and it has a number of similarities with issues I’ve run into at work (particularly data entry, authentication, and error handling).

These are my rough criticisms (hopefully constructive!) of patternshare.org:

Ok first of all, what is with all of the patterns named ImplementingSomePatternInNET? While I understand that examples of patterns can be very useful, I think it’s clutter to label them separately. ImplementingModelViewControllerInASPNET and ImplementingMVCWithUIP both belong as “Examples” links in the ModelViewController page. I haven’t checked yet, but if there isn’t an Examples section for other patterns, there ought to be, specifically for this purpose. I’m not trying to downplay the usefulness of the MDSN library – I just think it’s been placed at the wrong location.

I see several methods of mapping class hierarchies into a relational model. ClassTableInheritance, ConcreteTableInheritance, and SingleTableInheritance are all solutions to the exact same problem. However, none of them list each other under the “Related Patterns” section. They only link to the book’s page. It would be useful for patterns that are clearly so closely related to be indicated as being such. Maybe this is just an issue of “many eyes” fixing the situation. At the very least, an anonymous user should be able to suggest a relation between two patterns.

Read the rest of this entry »