[ZODB-Dev] SecureServerStorage and SecureClientStorage
Guido van Rossum
guido@python.org
Sat, 05 Oct 2002 04:43:59 -0400
> > > > Hm, maybe we should adopt as general principle that whenever a class
> > > > instantiates another class (and we think it might be useful to
> > > > subclass that class to provide additional functionality), it doesn't
> > > > use that other class directly, but uses a reference to that class
> > > > stored on the current class.
> > >
> > > Over the last few months I have been refactoring one of our internal
> > > projects that used to use this idiom. One problem is that you often
> > > have to create more derived classes than you might think is
> > > necessary; the purpose of the extra classes is solely to change the
> > > type of the classes it creates.
> >
> > That's true. Does this mean you no longer like this idiom? Do you
> > have an alternative?
>
> Suppose instances of class X have instances of class Y as an attribute. X
> creates an instance of class Z to do some work on Y. Your suggested idiom
> (and the one we have recently abandonned) is that X holds the concrete type
> of Z because X creates Z. The right answer might be as simple as choosing the
> concrete type in Y rather than X
That depends on the relationship between classes X, Y and Z, of
course. The cases that I changed in ZEO only involve two classes (per
case) though, and I think there it works well: class X creates a Y
instance each time a certain event happens. Old code:
class X:
def on_event(self):
"do a lot of work"
y = Y()
"do a lot of work on y"
New code:
class X:
Y_factory = Y
def on_event(self):
"do a lot of work"
self.y = self.Y_factory()
"do a lot of work on y"
The use case is that someone else writes:
class Y1(Y):
...override some Y method...
they can get this new functionality by writing this:
class X1(X):
Y_factory = Y1
How would you improve upon this?
--Guido van Rossum (home page: http://www.python.org/~guido/)