Tree views - Best design practices

Jonathan Gardner jgardner at jonathangardner.net
Thu Jan 8 16:33:17 EST 2009


On Jan 8, 1:00 pm, "Filip Gruszczyński" <grusz... at gmail.com> wrote:
>
> Is it really any better than asking for class? I mean, if I need to
> add another class to a hierarchy which behaves differently, will it be
> more flexible (actually you have to add another method to every class
> and check for in the gui). I believe it's just the same as asking for
> the class, but we hide it under static methods. It's no different
> though.
>

One additional note:

Given that the interface and class of an object are two, orthogonal
and independent things, how do you tell what interfaces an object
supports?

There are a variety of methods. I can break them down into 3.

(1) The user of the object keeps track of which classes support which
interfaces. This is bad because you can't anticipate new classes
properly. Sometimes it is necessary when the other two options aren't
feasible.

(2) The implementor of the object provides information on what
interfaces it supports through a method or attribute of some sort.
This is bad because there may be new interfaces that come into
existence that the object does support but the implementor doesn't
know about it and so the object says it doesn't support the interface.

(3) Some 3rd Party registration of interfaces and classes keeps track
of which classes support which interfaces, and vice-versa. When you
add a new interface, you have to list all the existing classes that
also support that interface. When you add a new class, you list all
the existing interfaces that it supports. This is just plain hard to
do, of course.

None of these solutions are perfect, of course.

Duck typing tries to solve this problem with option (4): Nobody really
keeps track of interfaces at all, and you just kind of wing it hoping
for the best. This solution is also far from perfect, but it suggests
that you never look at the class of an object, or really, even its
interface. You just start using it.

So my solution is the "just use it" bit. All the user really needs to
know is "Are you a leaf or a branch?" And the objects simply have to
answer that question.



More information about the Python-list mailing list