Guest User

Untitled

a guest
Dec 11th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. '''
  2. Created on Dec 11, 2018
  3.  
  4. @author: p636205
  5. '''
  6.  
  7. """
  8. Create algorithm classes using encapsulation and make their handles
  9. interchangeable. The Strategy Design Pattern lets the client class
  10. -- the Context -- leverage a common handle to address
  11. any of the algorithm classes
  12. """
  13.  
  14. import abc
  15.  
  16.  
  17. class Lunch_Context:
  18. """
  19. This is the interface which all the clients
  20. need to handshake with.
  21. Reference the strategy algorithm class here.
  22. """
  23.  
  24. def __init__(self, lunch_strategy):
  25. self._lunch_strategy = lunch_strategy
  26.  
  27. def lunch_context_interface(self):
  28. self._lunch_strategy.lunch_algorithm_interface()
  29.  
  30.  
  31. class Lunch_Strategy(metaclass=abc.ABCMeta):
  32. """
  33. This interface is the contract for all strategy algorithms.
  34. The context uses this interface as a model to
  35. call the concrete strategies [algorithms] defined below
  36. """
  37.  
  38. @abc.abstractmethod
  39. def lunch_algorithm_interface(self):
  40. pass
  41.  
  42.  
  43. class eat_at_desk_ConcreteStrategy(Lunch_Strategy):
  44. """
  45. Implement the eat at desk strategy using the Strategy interface.
  46. """
  47.  
  48. def lunch_algorithm_interface(self):
  49. print("zap! you heated it in the microwave and ate it at your desk")
  50.  
  51.  
  52. class join_friends_at_restaurant_ConcreteStrategy(Lunch_Strategy):
  53. """
  54. Implement the join friends at restaurant strategy using the Strategy interface.
  55. """
  56.  
  57. def lunch_algorithm_interface(self):
  58. print("you went out with friends and ate at a restaurant")
  59.  
  60. class go_to_cafeteria_ConcreteStrategy(Lunch_Strategy):
  61. """
  62. Implement the go to cafeteria strategy using the Strategy interface.
  63. """
  64.  
  65. def lunch_algorithm_interface(self):
  66. print("a quiet hour at the cafeteria")
  67.  
  68.  
  69. def main():
  70. caf_concrete_strategy = go_to_cafeteria_ConcreteStrategy()
  71. join_concrete_strategy = join_friends_at_restaurant_ConcreteStrategy()
  72. zap_concrete_strategy = eat_at_desk_ConcreteStrategy()
  73. context = Lunch_Context(caf_concrete_strategy)
  74. context.lunch_context_interface()
  75. context = Lunch_Context(join_concrete_strategy)
  76. context.lunch_context_interface()
  77. context = Lunch_Context(zap_concrete_strategy)
  78. context.lunch_context_interface()
  79.  
  80.  
  81. if __name__ == "__main__":
  82. main()
Add Comment
Please, Sign In to add comment