Geoff wrote:

>But someone (I can't even remember who started this thread :-)) is
>using a Delegation technique to write his servlets and could only
>get it to work by removing the check for Servlet as a base class.

That someone was me... :-)

Here's an example of delegation:

In the file that I want Webware to treat as a servlet:

class PseudoServlet:
    def __init__(self):
        # Create and store instance of a real servlet
        self.__dict__['realservlet'] = Servlet()

    def myMethod1(self):
        do stuff

    def __getattr__(self, name):
        return getattr(self.realservlet, name)

    def __setattr__(self, name, value):
        return setattr(self.realservlet, name, value)

PseudoServlet can be served up by Webware as normal so long as the
issubclass() assertion in the ServletFactory is relaxed. What happens
is that any attribute or method that isn't defined in PseudoServlet
is automatically delegated to the real servlet.

Originally, I did this to get around a multiple inheritance problem that
I was having with Cheetah, where I could not override the base template's
.awake() method due to the way Python < 2.2 was resolving multiple
inheritance. Cheetah has since been modified to only support single
inheritance.

However, I'm still using delegation to get around weak encapsulation
and other issues that I ran into when writing servlets. The issues
I'm referring to are discussed in the following links:

1. Inheritance results in rigid interfaces and weak encapsulation of
attributes, methods, etc. of the superclasses, hence the need to be
aware of *all* the attributes and methods in superclasses when writing
a Cheetah template, new servlet, or mix-ins. There are other pros and
cons of the approaches, revolving around fragility of superclass
interfaces, etc. This is a good, short summary of some of these issues:
http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html

2. The second problem I ran into was the generic multiple inheritance
problem that is recognized and addressed in Python 2.2. Kuchling
discusses this in
http://www.amk.ca/python/2.2/index.html#SECTION000330000000000000000"; but
there are lots of references to this in the literature going back to
the early days of OO. Here's a good reference:
http://www.elj.com/eiffel/feature/inheritance/mi/review/

Problem 2 was interesting since the specific issue I had when Cheetah
still supported multiple inheritance was solved in Python 2.2. What this
means is that when you use multiple inheritance, the methods in base
classes
you think you are overriding can differ between Python < 2.2 and
Python >= 2.2. This is nasty! In the situation I came across, the code
would
have worked in Python 2.2, but would have broken in Python < 2.2.

...Edmund.



_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to