class IWeapon(object):
def info(self):
raise NotImplementedError
class Sword(IWeapon):
def info(self):
print("This cold weapon is Sword")
class Knigfe(IWeapon):
def info(self):
print("This cold weapon is Knigfe")
class Gun(IWeapon):
def info(self):
print("This modern weapon is Gun")
class Cannon(IWeapon):
def info(self):
print("This modern weapon is Cannon")
#------------------------------------------------------
class IFactory(object):
def create_weapon(self, weapon_name):
raise NotImplementedError
class WeaponFactory(IFactory):
def create_weapon(self, weapon_name):
if weapon_name == 'Sword':
return Sword()
elif weapon_name == 'Knigfe':
return Knigfe()
if weapon_name == 'Gun':
return Gun()
elif weapon_name == 'Cannon':
return Cannon()
else:
raise NameError
#------------------------------------------------------
def main():
weapon_name = ('Sword', "Knigfe", 'Gun', "Cannon")
weaponFactory = WeaponFactory()
sword = weaponFactory.create_weapon(weapon_name[0])
knigfe = weaponFactory.create_weapon(weapon_name[1])
gun = weaponFactory.create_weapon(weapon_name[2])
cannon = weaponFactory.create_weapon(weapon_name[3])
sword.info()
knigfe.info()
gun.info()
cannon.info()
if __name__ == "__main__":
main()