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

Implementation of the SortStrategy structure and the SortStrategy_Initialize() function, along with the individual strategies, as used in the Strategy Pattern. More...

#include <stdlib.h>
#include <string.h>
#include "Strategy_SortStrategy.h"
Include dependency graph for Strategy_SortStrategy.c:

Go to the source code of this file.

Classes

struct  SortStrategyItem
 Represents a sorting strategy. More...
 

Functions

static bool _Compare_Name (const EntryInformation *leftEntry, const EntryInformation *rightEntry)
 Compare the name fields of two EntryInformation objects to determine if the two entries need to be swapped.
 
static bool _Compare_Age (const EntryInformation *leftEntry, const EntryInformation *rightEntry)
 Compare the age fields of two EntryInformation objects to determine if the two entries need to be swapped.
 
static bool _Compare_Height (const EntryInformation *leftEntry, const EntryInformation *rightEntry)
 Compare the height fields of two EntryInformation objects to determine if the two entries need to be swapped.
 
void SortStrategy_Initialize (SortStrategy *strategy, SortOptions sortOption, bool reversedSort)
 Initialize a SortStrategy object with the desired strategy.
 

Variables

static SortStrategyItem sortingStrategies []
 List of sorting strategies as indexed by a value from the SortOptions enumeration. Provides the name of the sorting strategy and the compare function to use.
 

Detailed Description

Implementation of the SortStrategy structure and the SortStrategy_Initialize() function, along with the individual strategies, as used in the Strategy Pattern.

Definition in file Strategy_SortStrategy.c.

Function Documentation

◆ _Compare_Age()

static bool _Compare_Age ( const EntryInformation leftEntry,
const EntryInformation rightEntry 
)
static

Compare the age fields of two EntryInformation objects to determine if the two entries need to be swapped.

Parameters
leftEntryThe left EntryInformation object to compare.
rightEntryThe right EntryInformation object to compare.
Returns
Returns true if the left object comes after the right object in an ascending order and this would need to be swapped; otherwise, returns false.

Definition at line 43 of file Strategy_SortStrategy.c.

References EntryInformation::Age.

◆ _Compare_Height()

static bool _Compare_Height ( const EntryInformation leftEntry,
const EntryInformation rightEntry 
)
static

Compare the height fields of two EntryInformation objects to determine if the two entries need to be swapped.

Parameters
leftEntryThe left EntryInformation object to compare.
rightEntryThe right EntryInformation object to compare.
Returns
Returns true if the left object comes after the right object in an ascending order and this would need to be swapped; otherwise, returns false.

Definition at line 65 of file Strategy_SortStrategy.c.

References EntryInformation::Height.

◆ _Compare_Name()

static bool _Compare_Name ( const EntryInformation leftEntry,
const EntryInformation rightEntry 
)
static

Compare the name fields of two EntryInformation objects to determine if the two entries need to be swapped.

Parameters
leftEntryThe left EntryInformation object to compare.
rightEntryThe right EntryInformation object to compare.
Returns
Returns true if the left object comes after the right object in an ascending order and this would need to be swapped; otherwise, returns false.

Definition at line 21 of file Strategy_SortStrategy.c.

References EntryInformation::Name.

◆ SortStrategy_Initialize()

void SortStrategy_Initialize ( SortStrategy strategy,
SortOptions  sortOption,
bool  reversedSort 
)

Initialize a SortStrategy object with the desired strategy.

Parameters
strategyThe SortStrategy object to initialize.
sortOptionA value from the SortOptions enumeration specifying the sorting strategy to use.
reversedSortTrue if to reverse the sense of the compare function.

Definition at line 112 of file Strategy_SortStrategy.c.

References SortStrategyItem::compareFunction, SortStrategy::compareFunction, SortStrategyItem::name, SortStrategy::name, SortStrategy::reversedSort, and sortingStrategies.

Referenced by Strategy_Exercise().

Variable Documentation

◆ sortingStrategies

SortStrategyItem sortingStrategies[]
static
Initial value:
=
{
{ "ByName" , _Compare_Name },
{ "ByAge" , _Compare_Age },
{ "ByHeight", _Compare_Height }
}
static bool _Compare_Height(const EntryInformation *leftEntry, const EntryInformation *rightEntry)
Compare the height fields of two EntryInformation objects to determine if the two entries need to be ...
static bool _Compare_Age(const EntryInformation *leftEntry, const EntryInformation *rightEntry)
Compare the age fields of two EntryInformation objects to determine if the two entries need to be swa...
static bool _Compare_Name(const EntryInformation *leftEntry, const EntryInformation *rightEntry)
Compare the name fields of two EntryInformation objects to determine if the two entries need to be sw...

List of sorting strategies as indexed by a value from the SortOptions enumeration. Provides the name of the sorting strategy and the compare function to use.

Definition at line 95 of file Strategy_SortStrategy.c.

Referenced by SortStrategy_Initialize().