Design pattern question

Geert Pante greyfairer at my-deja.com
Wed Aug 21 04:19:48 EDT 2002


Hi Karl,

I don't think you can change the class of an object in the constructor.  You could use a factory method with a
parameter, and call it as a static method.

class SequenceAssembler:
    def create(int type):
        if type=='cap3'
            return Cap3Assembler()
        elif type=='phrap'
            return PhrapAssembler()

assembler = SequenceAssembler.create('cap3')




"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
>





More information about the Python-list mailing list