5

I have two (2) questions: Firstly, how do I create the FlyBehavior interface using Python? Secondly, how do I implement the FlyBehavior interface in the FlyWithWings class, using Python (see below)? I'm learning Design Patterns by Head First and I want to rewrite the following Java classes using Python

public abstract class Duck {

    // Reference variables for the 
    // behavior interface types
    FlyBehavior flyBehavior;
    QuackBehavior quackBehavior;

    public Duck() {
    }

    // Delegate to the behavior class
    public void performFly(){
        flyBehavior.fly();
    }

    // Delegate to the behavior class
    public void performQuack(){
        quackBehavior.quack();
    }
}

Here is the interface that all flying behavior classes implement

public interface FlyBehavior {
    public void fly();
}

Here is the flying behavior implementation for ducks that do fly

public class FlyWithWings implements FlyBehavior {
    public void fly(){
    System.out.println("I'm flying");
    }
}

Here is what I have so far using Python. Below is my Python abstract Duck class

import abc

class Duck:
    __metaclass__=abc.ABCMeta


    FlyBehavior FlyBehavior;
    QuackBehavior QuackBehavior;

    @abc.abstractmethod
    def __init__():
        return

    @abc.abstractmethod
    def performFly():
        return

    @abc.abstractmethod
    def performQuack():
        return

Here is where I'm stuck trying to create the interface, and trying to implement the interface.

1

2 Answers 2

6

As Alex Taylor pointed out, Python is a duck-typed language - you don't need to specify the types of things, you just use them.

However, I think his translation of the Java code is wrong. You do not need to use abc here - just use a normal class.

class Duck(object):
    # Like in Java, you don't need to write a __init__ if it's empty

    # You don't need to declare fields either - just use them.

    def performFly(self):
        self.flyBehaviour.fly()

    def performQuack(self):
        self.quackBehaviour.quack()

class FlyWithWings(object):
    def fly(self):
        print "I'm flying"

# Example:
d = Duck()
d.flyBehaviour = FlyWithWings()
d.performFly() # prints "I'm flying"
Sign up to request clarification or add additional context in comments.

1 Comment

Shouldn't the last line be: 'd.performFly()' ?
6

Python is a duck typed language. You don't need interfaces - you pass in an object and if it supports the method you want it works. If it doesn't have the method it blows up. It doesn't have the compile-time checking that Java has. If you need to check, you do it yourself at run-time. So it should just be:

import abc

class Duck:
    __metaclass__=abc.ABCMeta


    FlyBehavior FlyBehavior;
    QuackBehavior QuackBehavior;

    @abc.abstractmethod
    def __init__():
        return

    @abc.abstractmethod
    def performFly():
        flyBehavior.fly()

    @abc.abstractmethod
    def performQuack():
        quackBehavior.quack()

As a broader point, not all design patterns are applicable to all languages. See Are Design Patterns Missing Language Features.

1 Comment

Good point about design patterns not applying to all languages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.