Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating and Implementing an interface using Python?

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.

like image 463
Sparky Avatar asked Apr 30 '15 01:04

Sparky


People also ask

What is interface How do you create interface in Python explain with an example?

In object-oriented languages like Python, the interface is a collection of method signatures that should be provided by the implementing class. Implementing an interface is a way of writing an organized code and achieve abstraction. The package zope.

What is meant by interface in Python?

Introduction to Interface in Python. An interface acts as a template for designing classes. Interfaces also define methods the same as classes, but abstract methods, whereas class contains nonabstract methods. Abstract methods are those methods without implementation or which are without the body.

How do you implement an interface?

The implements keyword is used to implement an interface . The interface keyword is used to declare a special type of class that only contains abstract methods. To access the interface methods, the interface must be "implemented" (kinda like inherited) by another class with the implements keyword (instead of extends ).


2 Answers

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"
like image 169
user253751 Avatar answered Sep 27 '22 19:09

user253751


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.

like image 25
Alex Taylor Avatar answered Sep 27 '22 19:09

Alex Taylor