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

Represents the state machine. This maintains the context in which the state machine runs. More...

Inheritance diagram for StateContext_Class:
Inheritance graph
Collaboration diagram for StateContext_Class:
Collaboration graph

Public Member Functions

string RemoveComments (string text)
 Entry point for callers to filter text. Removes C++-style line and block comments from the text.
 
char GetNextCharacter ()
 Get the next character from the input.
 
void OutputCharacter (char character)
 Write the character to the context. This is how the parser accumulates the filtered text.
 

Private Member Functions

void _SetNextState (CurrentState newState)
 Helper method to transition the state machine to the specified state. Does nothing if the new state is the same as the old state. Instantiates the state class the first time the state class is needed.
 
char IStateContext. GetNextCharacter ()
 Retrieve the next character from the input.
 
void IStateContext. OutputCharacter (char character)
 Save the character to the accumulation of the filtered text.
 

Private Attributes

string _inputText = string.Empty
 The text to be filtered.
 
int _textIndex = 0
 Index into the text to be filtered.
 
StringBuilder _outputText = new StringBuilder()
 The results of the filtering.
 
Dictionary< CurrentState, IStateBehavior_stateBehaviors = new Dictionary<CurrentState, IStateBehavior>()
 Maps values from the CurrentState enumeration to instances of the IStateBehavior representing the behavior for that state.
 
CurrentState _currentState
 The current state of the machine.
 
IStateBehavior _currentStateBehavior = new State_NormalText()
 The current behavior (that is, a reference to the state behavior class) for the current state.
 

Detailed Description

Represents the state machine. This maintains the context in which the state machine runs.

To use:

StateContext_Class filterContext = new StateContext_Class();
string filteredText = filterContext.RemoveComments(textToFilter);
static const char * textToFilter
Represents the state machine. This maintains the context in which the state machine runs.
Definition: State_Class.cs:696
string RemoveComments(string text)
Entry point for callers to filter text. Removes C++-style line and block comments from the text.
Definition: State_Class.cs:769

Definition at line 695 of file State_Class.cs.

Member Function Documentation

◆ _SetNextState()

void _SetNextState ( CurrentState  newState)
inlineprivate

Helper method to transition the state machine to the specified state. Does nothing if the new state is the same as the old state. Instantiates the state class the first time the state class is needed.

Parameters
newStateA value from the CurrentState enumeration indicating the state to which to transition.

Definition at line 742 of file State_Class.cs.

References StateContext_Class._currentState, StateContext_Class._currentStateBehavior, StateContext_Class._stateBehaviors, and State_Factory.CreateState().

Referenced by StateContext_ClassImpl.RemoveComments(), and StateContext_Class.RemoveComments().

◆ GetNextCharacter()

char IStateContext. GetNextCharacter ( )
inlineprivate

Retrieve the next character from the input.

Returns
Returns the next character from the input. Returns State_Constants.EOF if there is no more input.

Implements IStateContext.

Definition at line 799 of file State_Class.cs.

References StateContext_Class._inputText, StateContext_Class._textIndex, and State_Constants.EOF.

◆ OutputCharacter()

void IStateContext. OutputCharacter ( char  character)
inlineprivate

Save the character to the accumulation of the filtered text.

Parameters
characterThe character to save.

Implements IStateContext.

Definition at line 815 of file State_Class.cs.

References StateContext_Class._outputText, and State_Constants.EOF.

◆ RemoveComments()

string RemoveComments ( string  text)
inline

Entry point for callers to filter text. Removes C++-style line and block comments from the text.

Parameters
textThe text to filter.
Returns
Returns the filtered text.

Definition at line 769 of file State_Class.cs.

References StateContext_Class._currentState, StateContext_Class._currentStateBehavior, StateContext_Class._inputText, StateContext_Class._outputText, StateContext_Class._SetNextState(), StateContext_Class._textIndex, and IStateBehavior.GoNext().

Referenced by State_Exercise.Run().

Member Data Documentation

◆ _currentState

◆ _currentStateBehavior

IStateBehavior _currentStateBehavior = new State_NormalText()
private

The current behavior (that is, a reference to the state behavior class) for the current state.

Definition at line 727 of file State_Class.cs.

Referenced by StateContext_Class._SetNextState(), StateContext_ClassImpl.RemoveComments(), and StateContext_Class.RemoveComments().

◆ _inputText

string _inputText = string.Empty
private

◆ _outputText

StringBuilder _outputText = new StringBuilder()
private

◆ _stateBehaviors

Dictionary<CurrentState, IStateBehavior> _stateBehaviors = new Dictionary<CurrentState, IStateBehavior>()
private

Maps values from the CurrentState enumeration to instances of the IStateBehavior representing the behavior for that state.

Definition at line 716 of file State_Class.cs.

Referenced by StateContext_Class._SetNextState(), and StateContext_ClassImpl._SetNextState().

◆ _textIndex

int _textIndex = 0
private

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