fork download
  1. #!/usr/bin/env python3
  2.  
  3. from abc import ABCMeta, abstractmethod
  4.  
  5. class Terminator(metaclass=ABCMeta):
  6. def __init__(self):
  7. self.props = {}
  8.  
  9. def template_method(self):
  10. self.legs_cnt()
  11. self.hands_cnt()
  12. self.liquid_body()
  13. self.steel_skeleton()
  14. return self
  15.  
  16. @abstractmethod
  17. def legs_cnt(self):
  18. pass
  19.  
  20. @abstractmethod
  21. def hands_cnt(self):
  22. pass
  23.  
  24. def liquid_body(self):
  25. pass
  26.  
  27. def steel_skeleton(self):
  28. pass
  29.  
  30.  
  31. class T_800(Terminator):
  32. def legs_cnt(self):
  33. self.props['legs_cnt'] = 2
  34.  
  35. def hands_cnt(self):
  36. self.props['hands_cnt'] = 2
  37.  
  38. def steel_skeleton(self):
  39. self.props['steel_skeleton'] = True
  40.  
  41. class T_1000(Terminator):
  42. def legs_cnt(self):
  43. self.props['legs'] = 2
  44.  
  45. def hands_cnt(self):
  46. self.props['hands_cnt'] = 2
  47.  
  48. def liquid_body(self):
  49. self.props['liquid_body'] = True
  50.  
  51.  
  52. t_800 = T_800().template_method()
  53. print(t_800.__class__, t_800.props)
  54.  
  55. t_1000 = T_1000().template_method()
  56. print(t_1000.__class__, t_1000.props)
  57.  
  58.  
Success #stdin #stdout 0.01s 27712KB
stdin
Standard input is empty
stdout
<class '__main__.T_800'> {'legs_cnt': 2, 'steel_skeleton': True, 'hands_cnt': 2}
<class '__main__.T_1000'> {'legs': 2, 'liquid_body': True, 'hands_cnt': 2}