zoukankan      html  css  js  c++  java
  • 抽象工厂模式

    class Frog:
    
        def __init__(self, name):
            self.name = name
    
        def __str__(self):
            return self.name
    
        def interact_with(self, obstacle):
            print('{} the Frog encounters {} 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 '
    
    	------ 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 '
    
    	------ 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()
    

      

  • 相关阅读:
    弹跳加载中自定义简单控件实现
    属性动画的监听
    属性动画入门学习
    动画示例
    帧动画代码实现示例
    利用代码定义动画
    利用XMl标签定义动画
    自定义控件的使用方法
    自定义控件之canvas变换和裁剪
    自定义控件之Region区域
  • 原文地址:https://www.cnblogs.com/andy9468/p/8414331.html
Copyright © 2011-2022 走看看