Design pattern question
Karl Schmid
schmid at ice.mpg.de
Wed Aug 21 03:59:56 EDT 2002
Hi Markus,
I know of the biopython project and use their tools extensively ;-)
I am trying to learn design patterns and they may be useful in this project.
The goal is to separate the abstraction of a sequence assembler from its
partiular implementation. The abstraction is expressed by the abstract
class SequenceAssembly() and when it is instantiated, it is given a
parameter (which, for example, is set in the configuration file for a
particular project) that determines which implementation (the actual
assembly algorithm) is used. The switch between implementations is then in
the abstract class and not in the client program that calls the
SequenceAssembly() instance. The client then does not need to know anything
about the particular implementation (this is called loose coupling, I
believe) which makes it very easy to add additional assembly algorithms.
In python, the return statement is not allowed in the __init__ function,
which could be used to return instances of particular implementations
(e.g., the Cap3Assembler), so a different solution has to be found, which
is what I am looking for.
Karl
Markus von Ehr wrote:
> Hi Karl,
>
> I don't understand what you wanna do exactly, but do you know the
> BioPython project? Maybe there's already something inside matching
> your problem.
>
> http://biopython.org
>
> Markus
>
>
> Karl Schmid schrieb:
>>
>> Hi,
>>
>> I would like to separate an abstraction from its implementation. I think
>> this is called the Bridge pattern by Gamma et al. (or at least it is a
>> part of a bridge pattern).
>>
>> In particular, I would like to assemble a set of DNA sequences and have
>> the option to chose among different assembly algorithms. I am not sure
>> what the best way to do this would be:
>>
>> class SequenceAssembler:
>>
>> def __init__(self,assembler_type):
>>
>> if assembler_type == 'cap3':
>> # What should I do here?
>> elif assembler_type == 'phrap':
>> # What should I do here?
>>
>> def save_sequences():
>> pass
>>
>> class Cap3Assembler(SequenceAssembler):
>>
>> def run_assembler():
>> # Cap3 specific instructions
>> pass
>>
>> class PhrapAssembler(SequenceAssembler):
>>
>> def run_assembler():
>> # Phrap specific instructions
>> pass
>>
>> Now I would like to instantiate the abstract class, but to obtain an
>> instance of either the Cap3Assembler or PhrapAssembler class depending on
>> the assembler_type variable.
>>
>> >>> assembler = SequenceAssembler(assembler_type)
>>
>> Is this possible?
>>
>> Thank you in advance.
>>
>> -- Karl Schmid
More information about the Python-list
mailing list