Code design problem

Marshall T. Vandegrift llasram at gmail.com
Wed Aug 29 09:03:54 EDT 2007


Marco Nawijn <nawijn at gmail.com> writes:

> The problem I face is that the implementation of the application class
> is completely different for the local and remote case. The local case
> is a straightforward implemenation using the subprocess module, the
> remote case is a CORBA implementation. Somehow I would like to switch
> from implementation class at runtime depending on whether or not the
> host parameter is specified or not.
>
> The Application, local implementation and remote implementation all
> have the same interface, so a possibility might be something like the
> following:

<snipped example>

> To me forwarding each call in the Application class looks a little bit
> redundant and I would like to get rid of it. Does anyone have any
> comments or suggestions? Can metaclass programming come to rescue?

It sounds like you could probably get away with just a factory function:

    def Application(program, host=None):
        if host is None:
            return LocalApplication(program)
        else:
            return RemoteApplication(program, host)

Then just implement the same interface and/or derive from a common base
class for LocalApplication and RemoteApplication.

HTH!,

-Marshall




More information about the Python-list mailing list