Design Pattern Examples
Overview of object-oriented design patterns
Strategy_Exercise.c File Reference

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"
Include dependency graph for Strategy_Exercise.c:

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.
 

Detailed Description

Implementation of the Strategy_Exercise() function as used in the Strategy Pattern.

Definition in file Strategy_Exercise.c.

Function Documentation

◆ Display_Entries()

static void Display_Entries ( const EntryInformation entries,
size_t  numEntries,
int *  sortIndices,
SortStrategy sortStrategy 
)
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.

Parameters
entriesA list of EntryInformation objects that were sorted.
numEntriesThe number of entries in the entries and sortIndices lists.
sortIndicesA list of indices into the entries list giving the order of the sorted entries.
sortStrategyA 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().

◆ Sort_Entries()

static void Sort_Entries ( const EntryInformation entries,
size_t  numEntries,
int *  sortIndices,
SortStrategy sortStrategy 
)
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.

Parameters
entriesA list of EntryInformation objects to sort.
numEntriesThe number of entries in the entries and sortIndices lists.
sortIndicesA list of indices into the entries list to be sorted. This list is altered as a result of the sorting process.
sortStrategyA 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().

◆ 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().

Variable Documentation

◆ entries

EntryInformation entries[]
static
Initial value:
=
{
{ "Ronnie", 19, 84 },
{ "Elaine", 29, 71 },
{ "Jack", 20, 81 },
{ "Myra", 35, 78 },
{ "Fred", 18, 88 },
}

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().