Implementation of the Strategy_Exercise() function as used in the Strategy Pattern. More...
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "Strategy_SortStrategy.h"
#include "Strategy_Exercise.h"
Go to the source code of this file.
Functions | |
static void | Sort_Entries (const EntryInformation *entries, size_t numEntries, int *sortIndices, SortStrategy *sortStrategy) |
Sort the list of EntryInformation objects using the sorting strategy given in the SortStrategy object. | |
static void | Display_Entries (const EntryInformation *entries, size_t numEntries, int *sortIndices, SortStrategy *sortStrategy) |
Display the list of EntryInformation objects that have (presumably) been sorted with the given SortStrategy object. | |
void | Strategy_Exercise (void) |
Example of using the Strategy Pattern. | |
Variables | |
static EntryInformation | entries [] |
List of individuals to play around with in the Strategy exercise. | |
Implementation of the Strategy_Exercise() function as used in the Strategy Pattern.
Definition in file Strategy_Exercise.c.
|
static |
Display the list of EntryInformation objects that have (presumably) been sorted with the given SortStrategy object.
Note: Must use the contents of the given indices to access the entries in the correct (sorted) order.
entries | A list of EntryInformation objects that were sorted. |
numEntries | The number of entries in the entries and sortIndices lists. |
sortIndices | A list of indices into the entries list giving the order of the sorted entries. |
sortStrategy | A SortStrategy object describing the sorting strategy that was used. |
Definition at line 68 of file Strategy_Exercise.c.
References EntryInformation::Age, entries, EntryInformation::Height, EntryInformation::Name, SortStrategy::name, and SortStrategy::reversedSort.
Referenced by Strategy_Exercise().
|
static |
Sort the list of EntryInformation objects using the sorting strategy given in the SortStrategy object.
Note: This function actually sorts the given indices and not the entries themselves. The entries are treated as immutable.
entries | A list of EntryInformation objects to sort. |
numEntries | The number of entries in the entries and sortIndices lists. |
sortIndices | A list of indices into the entries list to be sorted. This list is altered as a result of the sorting process. |
sortStrategy | A SortStrategy object describing the sorting strategy to be used. The key part of this object is the comparison function used to compare two entries to determine if they need to be swapped. |
Definition at line 31 of file Strategy_Exercise.c.
References SortStrategy::compareFunction, entries, and SortStrategy::reversedSort.
Referenced by Strategy_Exercise().
void Strategy_Exercise | ( | void | ) |
Example of using the Strategy Pattern.
The Strategy pattern provides a way to easily assign different algorithms to a function that can be changed at the time the function is called.
In this exercise, the Sort_Entries() is given a sorting strategy via the SortStrategy structure, which is initialized with the appropriate options based on the value from the SortOptions enumeration.
The Display_Entries() function is given the same strategy object to display the results of the sort.
Three different sorting strategies are provided (Name, Age, Height) and an option to reverse the normal order of the sort.
Definition at line 121 of file Strategy_Exercise.c.
References Display_Entries(), entries, Sort_ByAge, Sort_ByHeight, Sort_ByName, Sort_Entries(), and SortStrategy_Initialize().
|
static |
List of individuals to play around with in the Strategy exercise.
Definition at line 89 of file Strategy_Exercise.c.
Referenced by Display_Entries(), Strategy_ShowEntries_Class::ShowEntries(), Strategy_SortEntries_ByName::Sort(), Strategy_SortEntries_ByAge::Sort(), Strategy_SortEntries_ByHeight::Sort(), Sort_Entries(), DesignPatternExamples_cpp::Strategy_Exercise(), and Strategy_Exercise().