Code design problem
Sion Arrowsmith
siona at chiark.greenend.org.uk
Wed Aug 29 08:59:00 EDT 2007
Marco Nawijn <nawijn at gmail.com> wrote:
>I have a hard time figuring out an elegant and efficient design for
>the following problem.
What you want is known as the factory pattern.
> [ ... ] I would
>like the following (pseudo)-code to work:
>
>app = Application('patran') # Run on local machine
>app.start(args)
>
>app = Application('patran', host='myhost') # Run on remote machine
>app.start(args)
>
>The Application, local implementation and remote implementation all
>have the same interface, so a possibility might be something like the
>following:
>
>class Interface(object):
> .....
> def start(self): pass
> def stop(self): pass
You don't need an abstract interface like this in Python. Duck-
typing will take care of it for you. On the other hand, if you
do have common code between the local and global implementations,
it makes sense to share it in a base class. You might then want
to consider defining the methods which must be overridden as
def start(self): raise NotImplementedError
etc.
Given:
>class LocalImplementation(Interface):
> .....
>
>class GlobalImplementation(CorbaGlobalImplementation, Interface):
> .....
with suitable start and stop methods defined, all you need is
an Application factory function:
def Application(program, host=None):
if host is None:
return LocalImplementation(program)
return GlobalImplementation(program, host)
--
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
More information about the Python-list
mailing list