fork download
  1. # your code goes here#!/usr/bin/env python3
  2.  
  3. import abc
  4.  
  5. class Strategy(metaclass=abc.ABCMeta):
  6. @abc.abstractmethod
  7. def check_temperature(self, temperature):
  8. pass
  9.  
  10.  
  11. class HikeStrategy(Strategy):
  12. def check_temperature(self, temperature):
  13. if temperature >= 20 and temperature <= 40:
  14. return True
  15. else:
  16. return False
  17.  
  18.  
  19. class SkiStrategy(Strategy):
  20. def check_temperature(self, temperature):
  21. if temperature <= 0:
  22. return True
  23. else:
  24. return False
  25.  
  26. class Context:
  27. def __init__(self, temperature, strategy, action_name):
  28. self.temperature = temperature
  29. self.strategy = strategy
  30. self.action_name = action_name
  31.  
  32. def output_result(self):
  33. print('temperature: ' + str(self.temperature))
  34. print('action name: ' + str(self.action_name))
  35. print('result: ' + str(self.strategy.check_temperature(self.temperature)), end='\n\n')
  36.  
  37.  
  38. if __name__ == '__main__':
  39. context = Context(25, SkiStrategy(), 'ski')
  40. context.output_result()
  41.  
  42. context = Context(25, HikeStrategy(), 'hike')
  43. context.output_result()
  44.  
  45. context = Context(-20, SkiStrategy(), 'ski')
  46. context.output_result()
  47.  
  48. context = Context(-20, HikeStrategy(), 'hike')
  49. context.output_result()
Success #stdin #stdout 0.02s 27712KB
stdin
Standard input is empty
stdout
temperature: 25
action name: ski
result: False

temperature: 25
action name: hike
result: True

temperature: -20
action name: ski
result: True

temperature: -20
action name: hike
result: False