Python 抽象工厂方法



import abc

class AbstractEnemyFactory( object ):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def createNinja( self, name ):
            pass

    @abc.abstractmethod
    def createSwordsman( self, name ):
            pass


class LowLevelEnemyFactory( AbstractEnemyFactory ):

    def createNinja( self, name ):
            return LowLevelEnemyNinja( name = name )

    def createSwordsman( self, name ):
            return LowLevelEnemySwordsman( name = name )


class HighLevelEnemyFactory( AbstractEnemyFactory ):

    def createNinja( self, name ):
            return HighLevelEnemyNinja( name )

    def createSwordsman( self, name ):
            return HighLevelEnemySwordsman( name )


class EnemyNinja( object ):

    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def ninjutsu_attack( self ):
            pass


class LowLevelEnemyNinja( EnemyNinja ):

    def __init__( self, name = 'LowLevelEnemyNinja' ):
            self._name = name
            self._chakra = 100
            self._attack_power = 5
            self._speed = 10

    def ninjutsu_attack( self ):
            print 'Ninja use ninjutsu.'
            print '[%s]: my chakra is %s and the power of attack is %s'\
                    %( self._name, self._chakra, self._attack_power )


class HighLevelEnemyNinja( EnemyNinja ):

    def __init__( self, name = 'HighLevelEnemyNinja' ):
            self._name = name
            self._chakra = 300
            self._attack_power = 15
            self._speed = 20

    def ninjutsu_attack( self ):
            print 'Ninja use ninjutsu.'
            print '[%s]: my chakra is %s and the power of attack is %s'\
                    %( self._name, self._chakra, self._attack_power )

    def special_effect( self ):
            print '[%s]: special effect!'%( self._name )


class EnemySwordsman( object ):
    
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def swords_attack( self ):
            pass


class LowLevelEnemySwordsman( EnemySwordsman ):

    def __init__( self, name = 'LowLevelEnemySwordsman' ):
            self._name = name
            self._blood = 100
            self._attack_power = 5
            self._speed = 10

    def swords_attack( self ):
            print 'Swordsman use swords.'
            print '[%s]: my blood is %s and the power of attack is %s'\
                    %( self._name, self._blood, self._attack_power )
    


class HighLevelEnemySwordsman( EnemySwordsman ):

    def __init__( self, name = 'HighLevelEnemySwordsman' ):
            self._name = name
            self._blood = 300
            self._attack_power = 30
            self._speed = 30

    def swords_attack( self ):
            print 'Swordsman use swords.'
            print '[%s]: my blood is %s and the power of attack is %s'\
                    %( self._name, self._blood, self._attack_power )

    def special_effect( self ):
            print '[%s]: special effect!'%( self._name )


if __name__ == '__main__':

    h = HighLevelEnemyFactory()
    ninja = h.createNinja( name = 'Scheme' )
    ninja.ninjutsu_attack()
    ninja.special_effect()
    swordsman = h.createSwordsman( name = 'Lambda' )
    swordsman.swords_attack()
    swordsman.special_effect()


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值