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

#include <Strategy_ShowEntries_Class.h>

Collaboration diagram for Strategy_ShowEntries_Class:
Collaboration graph

Public Member Functions

 Strategy_ShowEntries_Class (SortOptions sortOption, bool reversedSort)
 Constructor.
 
void ShowEntries (const std::vector< 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::shared_ptr_t _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 44 of file Strategy_ShowEntries_Class.h.

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 68 of file Strategy_ShowEntries_Class.h.

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

Member Function Documentation

◆ ShowEntries()

void ShowEntries ( const std::vector< 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 81 of file Strategy_ShowEntries_Class.h.

References Strategy_ShowEntries_Class::_reversedSort, Strategy_ShowEntries_Class::_sortEntries, entries, and Helpers::formatstring().

Referenced by DesignPatternExamples_cpp::Strategy_Exercise().

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 58 of file Strategy_ShowEntries_Class.h.

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::shared_ptr_t _sortEntries
private

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