1.function based
#!/usr/bin/env python

import types

def method_property(name):
    def getter(self):
        return getattr(self, name)
    def setter(self, value):
        setattr(self, name, types.MethodType(value, self))
    return getter, setter

class Duck(object):
    def __init__(self, fly, quack):
        self.fly = fly
        self.quack = quack

    quack = property(*method_property("_quack"))
    fly = property(*method_property("_fly"))

    def swim(self):
        print "All ducks float, even decoys!"

class MallardDuck(Duck):
    def __init__(self):
        Duck.__init__(self, fly, quack)
        print "I'm a real Mallard Duck"

class ModelDuck(Duck):
    def __init__(self):
        Duck.__init__(self, fly_no_way, squeak_quack)
        print "I'm a Model Duck"

class DecoyDuck(Duck):
    def __init__(self):
        Duck.__init__(self, fly_no_way, mute_quack)
        print "I'm a Decoy Duck"

def fly(self):
    print "I'm flying!!"

def fly_no_way(self):
    print "I can't fly"

def fly_rocket_powered(self):
    print "I'm flying with a rocket!"

def quack(self):
    print "Quack"

def mute_quack(self):
    print "<< Silence >>"

def squeak_quack(self):
    print "Squeak"

if __name__ == "__main__":

    print '#'*80
    print "Mallard Duck"
    print '#'*80
    mallard = MallardDuck()
    mallard.quack()
    mallard.fly()

    print '#'*80
    print "Model Duck"
    print '#'*80
    model = ModelDuck()
    model.fly()
    model.fly = fly_rocket_powered
    model.fly()

    print '#'*80
    print "Decoy Duck"
    print '#'*80
    decoy = DecoyDuck()
    decoy.fly()
    decoy.quack()
2.class based
#!/usr/bin/env python

from abc import ABCMeta, abstractmethod

def subclasshook(required):
    """Generates a subclass hook based on a simple check for attributes."""
    def __subclasshook__(cls, Check):
        rtn = True
        for r in required:
            if not any(r in vars(BaseClass) for BaseClass in Check.__mro__):
                rtn = NotImplemented
        return rtn
    return __subclasshook__

class Duck(object):
    def __init__(self, fly, quack):
        self._fly_behaviour = fly
        self._quack_behaviour = quack

    def fly(self):
        return self._fly_behaviour.fly()

    def quack(self):
        return self._quack_behaviour.quack()

    def swim(self):
        print "All ducks float, even decoys!"

class MallardDuck(Duck):
    def __init__(self):
        Duck.__init__(self, Fly(), Quack())
        print "I'm a real Mallard Duck"

class ModelDuck(Duck):
    def __init__(self):
        Duck.__init__(self, FlyNoWay(), SqueakQuack())
        print "I'm a Model Duck"

class DecoyDuck(Duck):
    def __init__(self):
        Duck.__init__(self, FlyNoWay(), MuteQuack())
        print "I'm a Decoy Duck"

class FlyBehavior:
    __metaclass__ = ABCMeta

    @abstractmethod
    def fly(self):
        pass

    __subclasshook__ = classmethod(subclasshook(["fly"]))

class Fly(FlyBehavior):
    def fly(self):
        print "I'm flying!!"

class FlyNoWay(FlyBehavior):
    def fly(self):
        print "I can't fly"

class FlyRocketPowered(FlyBehavior):
    def fly(self):
        print "I'm flying with a rocket!"

class QuackBehavior:
    __metaclass__ = ABCMeta

    @abstractmethod
    def quack(self):
        pass

    __subclasshook__ = classmethod(subclasshook(["quack"]))

class Quack(QuackBehavior):
    def quack(self):
        print "Quack"

class MuteQuack(QuackBehavior):
    def quack(self):
        print "<< Silence >>"

class SqueakQuack(QuackBehavior):
    def quack(self):
        print "Squeak"

if __name__ == "__main__":

    print '#'*80
    print "Mallard Duck"
    print '#'*80
    mallard = MallardDuck()
    mallard.quack()
    mallard.fly()

    print '#'*80
    print "Model Duck"
    print '#'*80
    model = ModelDuck()
    model.fly()
    model._fly_behaviour = FlyRocketPowered()
    model.fly()

    print '#'*80
    print "Decoy Duck"
    print '#'*80
    decoy = DecoyDuck()
    decoy.fly()
    decoy.quack()



Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐