nithir revised this gist . Go to revision
No changes
nithir revised this gist . Go to revision
3 files changed, 86 insertions
factory.py(file created)
| @@ -0,0 +1,24 @@ | |||
| 1 | + | #!/usr/bin/env python3.7 | |
| 2 | + | ||
| 3 | + | from npc_factory import npc_factory | |
| 4 | + | from weapon_factory import weapon_factory | |
| 5 | + | ||
| 6 | + | class abstract_factory(): | |
| 7 | + | def get(object_type): | |
| 8 | + | try: | |
| 9 | + | if object_type in ['hacker', 'ganger']: | |
| 10 | + | return npc_factory.get_npc(object_type) | |
| 11 | + | if object_type in ['pistolet', 'mitraillette']: | |
| 12 | + | return weapon_factory.get_weapon(object_type) | |
| 13 | + | raise AssertionError(f"Le type '{object_type}' n'existe pas.") | |
| 14 | + | except AssertionError as _e: | |
| 15 | + | print(_e) | |
| 16 | + | exit(1) | |
| 17 | + | ||
| 18 | + | if __name__ == '__main__': | |
| 19 | + | obj = abstract_factory.get('hacker') | |
| 20 | + | obj.affichage() | |
| 21 | + | obj = abstract_factory.get('pistolet') | |
| 22 | + | obj.affichage() | |
| 23 | + | obj = abstract_factory.get('hackerNPC') | |
| 24 | + | obj.affichage() | |
npc_factory.py(file created)
| @@ -0,0 +1,31 @@ | |||
| 1 | + | #!/usr/bin/env python3.7 | |
| 2 | + | ||
| 3 | + | class NPC (): | |
| 4 | + | def affichage(self): | |
| 5 | + | print(f"Nom : {self.nom}") | |
| 6 | + | ||
| 7 | + | class gangerNPC(NPC): | |
| 8 | + | def __init__(self): | |
| 9 | + | self.nom = """Morales""" | |
| 10 | + | ||
| 11 | + | class hackerNPC(NPC): | |
| 12 | + | def __init__(self): | |
| 13 | + | self.nom = """Templar""" | |
| 14 | + | ||
| 15 | + | class npc_factory(): | |
| 16 | + | def get_npc(npc_type): | |
| 17 | + | try: | |
| 18 | + | if npc_type == "hacker": | |
| 19 | + | return hackerNPC() | |
| 20 | + | if npc_type == "ganger": | |
| 21 | + | return gangerNPC() | |
| 22 | + | raise AssertionError(f"Le type '{npc_type}' est inconnue.") | |
| 23 | + | except AssertionError as _e: | |
| 24 | + | print(_e) | |
| 25 | + | exit(1) | |
| 26 | + | ||
| 27 | + | if __name__ == '__main__': | |
| 28 | + | npc = npc_factory.get_npc('hacker') | |
| 29 | + | npc.affichage() | |
| 30 | + | npc = npc_factory.get_npc('mage') | |
| 31 | + | npc.affichage() | |
weapon_factory.py(file created)
| @@ -0,0 +1,31 @@ | |||
| 1 | + | #!/usr/bin/env python3.7 | |
| 2 | + | ||
| 3 | + | class weapon (): | |
| 4 | + | def affichage(self): | |
| 5 | + | print(f"Nom : {self.nom}") | |
| 6 | + | ||
| 7 | + | class pistolet(weapon): | |
| 8 | + | def __init__(self): | |
| 9 | + | self.nom = """Ares predator""" | |
| 10 | + | ||
| 11 | + | class mitraillette(weapon): | |
| 12 | + | def __init__(self): | |
| 13 | + | self.nom = """Ingram smartgun""" | |
| 14 | + | ||
| 15 | + | class weapon_factory(): | |
| 16 | + | def get_weapon(weapon_type): | |
| 17 | + | try: | |
| 18 | + | if weapon_type == "pistolet": | |
| 19 | + | return pistolet() | |
| 20 | + | if weapon_type == "mitraillette": | |
| 21 | + | return mitraillette() | |
| 22 | + | raise AssertionError(f"Le type '{weapon_type}' est inconnue.") | |
| 23 | + | except AssertionError as _e: | |
| 24 | + | print(_e) | |
| 25 | + | exit(1) | |
| 26 | + | ||
| 27 | + | if __name__ == '__main__': | |
| 28 | + | weapon = weapon_factory.get_weapon('pistolet') | |
| 29 | + | weapon.affichage() | |
| 30 | + | weapon = weapon_factory.get_weapon('machette') | |
| 31 | + | weapon.affichage() | |