Design Pattern Examples
Overview of object-oriented design patterns
Strategy_Exercise.c
Go to the documentation of this file.
1
5
6#include <stdbool.h>
7#include <stdio.h>
8#include <stdlib.h>
9
11
12#include "Strategy_Exercise.h"
13
14
15//-----------------------------------------------------------------------------
16
31static void Sort_Entries(const EntryInformation* entries, size_t numEntries, int* sortIndices, SortStrategy* sortStrategy)
32{
33 if (entries != NULL && sortIndices != NULL && sortStrategy != NULL)
34 {
35 for (size_t leftIndex = 0; leftIndex < numEntries - 1; leftIndex++)
36 {
37 for (size_t rightIndex = leftIndex + 1; rightIndex < numEntries; rightIndex++)
38 {
39 bool swapEntries = sortStrategy->compareFunction(&entries[sortIndices[leftIndex]], &entries[sortIndices[rightIndex]]);
40 if (sortStrategy->reversedSort)
41 {
42 swapEntries = !swapEntries;
43 }
44 if (swapEntries)
45 {
46 int tempIndex = sortIndices[leftIndex];
47 sortIndices[leftIndex] = sortIndices[rightIndex];
48 sortIndices[rightIndex] = tempIndex;
49 }
50 }
51 }
52 }
53}
54
68static void Display_Entries(const EntryInformation* entries, size_t numEntries, int* sortIndices, SortStrategy* sortStrategy)
69{
70 if (entries != NULL && sortIndices != NULL)
71 {
72 // This is a tabular display, making it easier to follow the sorted data.
73 printf(" Sort strategy: %s (order = %s)\n", sortStrategy->name, sortStrategy->reversedSort ? "Descending" : "Ascending");
74 printf(" %-6s %3s %3s\n", "Name", "Age", "Height");
75 printf(" %6s %3s %3s\n", "------", "---", "------");
76 for (size_t index = 0; index < numEntries; index++)
77 {
78 const EntryInformation* entry = &entries[sortIndices[index]];
79 printf(" %-6s %3d %3d\"\n", entry->Name, entry->Age, entry->Height);
80 }
81 }
82}
83
84//=============================================================================
85//=============================================================================
90{
91 // Name, age, height (in inches)
92 { "Ronnie", 19, 84 },
93 { "Elaine", 29, 71 },
94 { "Jack", 20, 81 },
95 { "Myra", 35, 78 },
96 { "Fred", 18, 88 },
97};
98
99
100//=============================================================================
101//=============================================================================
102
120// ! [Using Strategy in C]
122{
123 printf("\nStrategy Exercise\n");
124
125 size_t numEntries = sizeof(entries) / sizeof(entries[0]);
126 int* sortIndices = malloc(numEntries * sizeof(int)); // These are sortable indices into entries
127 if (sortIndices != NULL)
128 {
129 for (int index = 0; index < (int)numEntries; index++)
130 {
131 sortIndices[index] = index;
132 }
133
134 SortStrategy sortStrategy = { 0 };
135
136 SortStrategy_Initialize(&sortStrategy, Sort_ByName, false);
137 Sort_Entries(entries, numEntries, sortIndices, &sortStrategy);
138 Display_Entries(entries, numEntries, sortIndices, &sortStrategy);
139
140 SortStrategy_Initialize(&sortStrategy, Sort_ByAge, false);
141 Sort_Entries(entries, numEntries, sortIndices, &sortStrategy);
142 Display_Entries(entries, numEntries, sortIndices, &sortStrategy);
143
144 SortStrategy_Initialize(&sortStrategy, Sort_ByHeight, true);
145 Sort_Entries(entries, numEntries, sortIndices, &sortStrategy);
146 Display_Entries(entries, numEntries, sortIndices, &sortStrategy);
147
148 free(sortIndices);
149 }
150 else
151 {
152 printf(" Error! Out of memory creating array of indices to sort on!\n");
153 }
154
155 printf(" Done.\n");
156}
157// ! [Using Strategy in C]
static EntryInformation entries[]
List of individuals to play around with in the Strategy exercise.
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 SortSt...
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...
void Strategy_Exercise(void)
Example of using the Strategy Pattern.
void SortStrategy_Initialize(SortStrategy *strategy, SortOptions sortOption, bool reversedSort)
Initialize a SortStrategy object with the desired strategy.
Declaration of the SortStrategy structure and the SortStrategy_Initialize() function as used in the S...
@ Sort_ByAge
Sort numerically by age in ascending order.
@ Sort_ByHeight
Sort numerically by height in ascending order.
@ Sort_ByName
Sort alphabetically name in ascending order.
Declaration of the Strategy_Exercise() function as used in the Strategy Pattern.
Represents an individual with a Name, Age, and Height.
int Age
Age of this individual, in years.
const char * Name
Name of this individual.
int Height
Height of this individual, in inches.
Represents the strategy to use for sorting EntryInformation objects.
bool reversedSort
True if to reverse the order of the sort.
const char * name
Name of the strategy (for display purposes)
CompareFunction compareFunction
Compare function that determines the order of two entries.