Design pattern question

Changjune Kim juneaftn at REMOVETHIShanmail.net
Wed Aug 21 04:07:34 EDT 2002


"Karl Schmid" <schmid at ice.mpg.de> wrote in message
news:ajvevb$6el$1 at gwdu67.gwdg.de...
> 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
>


class Assembler:
    def run():
        raise NotImplementedError #abstract method
    def save_sequences():
         blah blah such that all the assemblers should do

class Cap3Assembler(Assembler):
    def run():
         ....

class PhrapAssembler(Assembler):
    def run():
         ....

#assemblers as singletons

_typeToAssembler={'cap3':Cap3Assembler(),'phrap':PhrapAssembler()}

def AssemblerFactory(aType):
    return _typeToAssembler[aType]

#or as non-singletons

_typeToAssembler={'cap3':Cap3Assembler,'phrap':PhrapAssembler}

def AssemblerFactory(aType):
    return _typeToAssembler[aType]()




More information about the Python-list mailing list