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.
Aucun commentaire:
Enregistrer un commentaire