Design Pattern Examples
Overview of object-oriented design patterns
Strategy_ShowEntries_Class Class Reference

Represents a way of displaying a list of EntryInformation objects in a particular order. The order of sorting is a strategy that can be specified when the class is instantiated. The sorting strategy can be modified with a flag indicating whether the sort is reversed from normal (in this case, descending instead of ascending). More...

Collaboration diagram for Strategy_ShowEntries_Class:
Collaboration graph

Public Types

enum  SortOptions { ByName , ByAge , ByHeight }
 Identifies the different sorting strategies available. More...
 

Public Member Functions

 Strategy_ShowEntries_Class (SortOptions sortOption, bool reversedSort)
 Constructor.
 
void ShowEntries (EntryInformation[] entries)
 Display the specified entries in sorted order. The order of the sort was established when the Strategy_ShowEntries_Class was instantiated.
 

Private Attributes

ISortEntries _sortEntries
 The sorting strategy to use.
 
bool _reversedSort
 Whether to reverse the normal order of the sort.
 

Detailed Description

Represents a way of displaying a list of EntryInformation objects in a particular order. The order of sorting is a strategy that can be specified when the class is instantiated. The sorting strategy can be modified with a flag indicating whether the sort is reversed from normal (in this case, descending instead of ascending).

In this particular approach, a class with a specific sorting strategy is created. It can be applied to any number of lists to achieve the specified sorting behavior as the sorting behavior cannot be changed once specified.

An alternative implementation would be to pass the choice of sorting strategy to the ShowEntries() method and instantiate the sorting class there. In this way, the list is sorted and displayed using the specified sorting strategy. The advantage of this approach is only one instance of the class is needed. The disadvantage is the need for two additional parameters that must be passed in all the time with the entries to be sorted (there might be places in the program where the sorting strategy is not known or is unavailable from the user).

Definition at line 84 of file Strategy_ShowEntries_Class.cs.

Member Enumeration Documentation

◆ SortOptions

Identifies the different sorting strategies available.

Enumerator
ByName 

Sort alphabetically name in ascending order.

ByAge 

Sort numerically by age in ascending order.

ByHeight 

Sort numerically by height in ascending order.

Definition at line 89 of file Strategy_ShowEntries_Class.cs.

Constructor & Destructor Documentation

◆ Strategy_ShowEntries_Class()

Strategy_ShowEntries_Class ( SortOptions  sortOption,
bool  reversedSort 
)
inline

Constructor.

Parameters
sortOptionA value from the SortOptions enumeration indicating the sorting strategy to use.
reversedSorttrue if the sort is to be reversed (descending); otherwise, sort in ascending order.

Definition at line 128 of file Strategy_ShowEntries_Class.cs.

References Strategy_ShowEntries_Class._reversedSort, Strategy_ShowEntries_Class._sortEntries, and Strategy_SortEntries_ClassFactory.Create().

Member Function Documentation

◆ ShowEntries()

void ShowEntries ( EntryInformation[]  entries)
inline

Display the specified entries in sorted order. The order of the sort was established when the Strategy_ShowEntries_Class was instantiated.

Parameters
entriesA list of EntryInformation objects to sort and display

Definition at line 141 of file Strategy_ShowEntries_Class.cs.

References Strategy_ShowEntries_Class._reversedSort, Strategy_ShowEntries_Class._sortEntries, entries, and ISortEntries.Sort().

Referenced by Strategy_Exercise.Run().

Member Data Documentation

◆ _reversedSort

bool _reversedSort
private

Whether to reverse the normal order of the sort.

This is remembered here so we can use it when outputting the list.

Definition at line 119 of file Strategy_ShowEntries_Class.cs.

Referenced by Strategy_ShowEntries_Class.ShowEntries(), Strategy_SortEntries_ByName.Sort(), Strategy_SortEntries_ByAge.Sort(), Strategy_SortEntries_ByHeight.Sort(), and Strategy_ShowEntries_Class.Strategy_ShowEntries_Class().

◆ _sortEntries

ISortEntries _sortEntries
private

The documentation for this class was generated from the following file: