1. 程式人生 > >《精通Python設計模式》學習之抽象工廠

《精通Python設計模式》學習之抽象工廠

com environ src erro 遊戲 http exce except hello

這種工廠模式用得少,

可能在遊戲類的編程中用得比較多吧。

這個思路清晰一定要OK的。

技術分享圖片

class Frog:
    def __init__(self, name):
        self.name = name

    def __str__(self):
        return self.name

    def interact_with(self, obstacle):
        print({} the Frog encouners {} and {}!.format(self,
                                                        obstacle,
                                                        obstacle.action()))

class Bug: def __str__(self): return a bug def action(self): return eats it class FrogWorld: def __init__(self, name): print(self) self.player_name =name def __str__(self): return \n\n\t-------- Frog World ------ def
make_character(self): return Frog(self.player_name) def make_obstacle(self): return Bug() class Wizard: def __init__(self, name): self.name = name def __str__(self): return self.name def interact_with(self, obstacle): print(
{} the Wizard battles against {} and {}!.format(self, obstacle, obstacle.action())) class Ork: def __str__(self): return an evil ork def action(self): return kills it class WizardWorld: def __init__(self, name): print(self) self.player_name = name def __str__(self): return \n\n\t-------- Wizard World ------ def make_character(self): return Wizard(self.player_name) def make_obstacle(self): return Ork() class GameEnvironment: def __init__(self, factory): self.hero = factory.make_character() self.obstacle = factory.make_obstacle() def play(self): self.hero.interact_with(self.obstacle) def validate_age(name): try: age = input(Welcome {}. How old are you? .format(name)) age = int(age) except ValueError as err: print(Age {} is invalid, please try again....format(age)) return (False, age) return (True, age) def main(): name = input("Hello. What‘s your name? ") valid_input = False while not valid_input: valid_input, age = validate_age(name) game = FrogWorld if age < 18 else WizardWorld environment = GameEnvironment(game(name)) environment.play() if __name__ == __main__: main()

技術分享圖片

《精通Python設計模式》學習之抽象工廠