Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Created on Dec 11, 2018
- @author: p636205
- '''
- """
- Create algorithm classes using encapsulation and make their handles
- interchangeable. The Strategy Design Pattern lets the client class
- -- the Context -- leverage a common handle to address
- any of the algorithm classes
- """
- import abc
- class Lunch_Context:
- """
- This is the interface which all the clients
- need to handshake with.
- Reference the strategy algorithm class here.
- """
- def __init__(self, lunch_strategy):
- self._lunch_strategy = lunch_strategy
- def lunch_context_interface(self):
- self._lunch_strategy.lunch_algorithm_interface()
- class Lunch_Strategy(metaclass=abc.ABCMeta):
- """
- This interface is the contract for all strategy algorithms.
- The context uses this interface as a model to
- call the concrete strategies [algorithms] defined below
- """
- @abc.abstractmethod
- def lunch_algorithm_interface(self):
- pass
- class eat_at_desk_ConcreteStrategy(Lunch_Strategy):
- """
- Implement the eat at desk strategy using the Strategy interface.
- """
- def lunch_algorithm_interface(self):
- print("zap! you heated it in the microwave and ate it at your desk")
- class join_friends_at_restaurant_ConcreteStrategy(Lunch_Strategy):
- """
- Implement the join friends at restaurant strategy using the Strategy interface.
- """
- def lunch_algorithm_interface(self):
- print("you went out with friends and ate at a restaurant")
- class go_to_cafeteria_ConcreteStrategy(Lunch_Strategy):
- """
- Implement the go to cafeteria strategy using the Strategy interface.
- """
- def lunch_algorithm_interface(self):
- print("a quiet hour at the cafeteria")
- def main():
- caf_concrete_strategy = go_to_cafeteria_ConcreteStrategy()
- join_concrete_strategy = join_friends_at_restaurant_ConcreteStrategy()
- zap_concrete_strategy = eat_at_desk_ConcreteStrategy()
- context = Lunch_Context(caf_concrete_strategy)
- context.lunch_context_interface()
- context = Lunch_Context(join_concrete_strategy)
- context.lunch_context_interface()
- context = Lunch_Context(zap_concrete_strategy)
- context.lunch_context_interface()
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment