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.
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.
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.
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 ).
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"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With