Design Pattern Examples
Overview of object-oriented design patterns
strategy_exercise.py
Go to the documentation of this file.
6
7from .strategy_entryinformation import EntryInformation
8from .strategy_showentries_class import Strategy_ShowEntries_Class
9from .strategy_isortentries import SortOptions
10
11
12
13entries = [
14 # Name, age, height (in inches)
15 EntryInformation("Ronnie", 19, 84),
16 EntryInformation("Elaine", 29, 71),
17 EntryInformation("Jack", 20, 81),
18 EntryInformation("Myra", 35, 78),
19 EntryInformation("Fred", 18, 88),
20]
21
22
32
33# ! [Using Strategy in Python]
35 print()
36 print("Strategy Exercise")
37
38 displaySortedByNameAscending = Strategy_ShowEntries_Class(SortOptions.ByName, False)
39 displaySortedByNameAscending.ShowEntries(entries)
40
41 displaySortedByAgeAscending = Strategy_ShowEntries_Class(SortOptions.ByAge, False)
42 displaySortedByAgeAscending.ShowEntries(entries)
43
44 displaySortedByHeightDescending = Strategy_ShowEntries_Class(SortOptions.ByHeight, True)
45 displaySortedByHeightDescending.ShowEntries(entries)
46
47 print(" Done.")
48# ! [Using Strategy in Python]
Represents an individual with a Name, Age, and Height.
Represents a way of displaying a list of EntryInformation objects in a particular order.
def Strategy_Exercise()
Example of using the Strategy Pattern.