Design Pattern Examples
Overview of object-oriented design patterns
state_interface.py
Go to the documentation of this file.
8
9from enum import Enum
10from abc import ABC, abstractmethod
11
12
13
14class CurrentState(Enum):
15
16 Initial = 0
17
18
21 NormalText = 1
22
23
26 DoubleQuotedText = 2
27
28
31 SingleQuotedText = 3
32
33
36 EscapedDoubleQuoteText = 4
37
38
41 EscapedSingleQuoteText = 5
42
43
46 StartComment = 6
47
48
51 LineComment = 7
52
53
56 BlockComment = 8
57
58
61 EndBlockComment = 9
62
63
64 Done = 10
65
66
67
68#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
69#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
70# Interface definitions
71#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
72#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
73
74
81
82 EOF_CHAR = 0xff
83
84
85
87
88
89
94class IStateContext(ABC):
95
96
101 @abstractmethod
102 def GetNextCharacter(self) -> int:
103 pass
104
105
110 @abstractmethod
111 def OutputCharacter(self, character : int) -> None:
112 pass
113
114
115
117
118
119
127class IStateBehavior(ABC):
128
129
135 def GoNext(context : IStateContext) -> CurrentState:
136 pass
Represents the current state of the state machine.
Represents a class that implements one state of the state machine.
CurrentState GoNext(IStateContext context)
(IStateBehavior) Retrieve to the next state given the current context
Represents the context as passed to each state class.
None OutputCharacter(self, int character)
(IStateContext) Write the character to the context.
int GetNextCharacter(self)
(IStateContext) Get the next character from the input.