[Tutor] Re: Factory classes (etc)
Scot W. Stevenson
scot@possum.in-berlin.de
Thu, 5 Sep 2002 10:20:25 +0200
Hello Magnus,
Thank you for the long answer!
> Your spray can example is neither an "Abstract Factory" nor a "Factory
> Method" I'm afraid. Both the "Abstract Factory" and "Factory Method"
> patterns return instances of _different_classes_ depending on the
> situation.
I knew it was too good to be true =8).
> # Factory function for symmetrical shapes
> def getSymmetricalShape(x, y, size, corners=0, angle=0):
> assert type(corners) == type(1) and (corners == 0 or corners > 1)
> if corners == 0:
> return Circle(x, y, size)
> elif corners == 2:
> return Line(x, y, size, angle)
> elif corners == 3:
> return Triangle(x, y, size, angle)
> elif corners == 4:
> return Square(x, y, size, angle)
> else:
> return RegularPolygon(x, y, size, sides, angle)
Ah, now I understand. Just as an aside, if I were to really programm
something like this (which is certainly the best way to understand the
principle), would I want to use a if-elif-else construct like above, or
rather a dictionary such as:
shapes = {0: Circle, 2: Line, 3: Triangle, [...]}
and then do a
shapes[corners](x, y, size, angle)
which would assume that Circle doesn't go balistic if it is suddenly handed
an angle. Again, I'm glad you did it this way here, because it is easier
to understand.
> This is a bit strange, since the Pattern
> movement (in software design) started among SmallTalkers, and SmallTalk
> is dynamically typed--I don't know enough SmallTalk to explain this.
Does anybody actually _use_ SmallTalk for production code, or is it one of
those teaching languages that revolutionized various concepts but never
made it in the market (sort of like Multics [sp] vs. Unix, or the League
of Nations vs. the United Nations, or for that matter, "Buffy" the Film
vs. "Buffy" the TV series)?
> I'm not saying that the ideas in GoF are overly complex, but I think
> it would have been a much thinner book if the Gang of Four had used
> Python...
Are there any common patterns that are missing by today's standards? I'm
thinking about the Borg "non-pattern" described in the "Python Cookbook" -
I can't believe that seven years later, the GoF is still state of the
art...
Thanks again,
Y, Scot
--
Scot W. Stevenson wrote me on Thursday, 5. Sep 2002 in Zepernick, Germany
on his happy little Linux system that has been up for 1903 hours
and has a CPU that is falling asleep at a system load of 0.08.