Module design_pattern_examples_rust::strategy
source · Expand description
The Strategy design pattern example module
The Strategy pattern provides a way to easily assign different algorithms to an object instance that can be changed at the time the object is created.
In this exercise, the StrategyShowEntries instance sorts and displays a list of EntryInformation elements. Three different sorting strategies are provided (Name, Age, Height) and an option to reverse the normal order of the sort.
Accessed through the strategy_exercise() function.
Modules
- Contains the EntryInformation struct that holds information about each entry that can be sorted.
- Contains the SortOptions enum and the ISortEntries trait that represents different sorting strategies.
- Contains the StrategyShowEntries struct that can make use of different sorting strategies to show an ordered list of entries.
- Contains the structs representing the various sorting strategies: StrategySortByName, StrategySortByAge, and StrategySortByHeight, along with a factory, SortStrategyFactory, that can instantiate a desired sorting strategy.
Functions
- Helper function to create a list of entries that can be sorted in various ways.
- Example of using the “Strategy” design pattern.