[fpc-pascal] Extending an enumeration
Thierry Coq
tcoq at free.fr
Wed Jul 21 08:55:58 CEST 2010
On 18/07/2010 20:56, Mark Morgan Lloyd wrote:
...
> Yes, I was thinking that. However if the basic class was say a
> round-robin scheduler with phases rrQuiescent and rrInitialised and
> the descendant was say an HP comms protocol handler with additional
> phases hpReceivingPadding, hpReceivedSync and so on it would seem to
> be questionable practice to have to define all possible enumerated
> values in the basic class.
>
...
In this case, enumeration cannot be extended. If you look at it from an
analysis point of view, you're implementing a state machine with an
enumeration. If your state machine is extendable instead of fixed, and
you want to add substates to it, then a possible implementation is to
define a state class:
TSchedulerPhase = Class(TObject);
where the basic phases are defined as variables in the base class, and
initialized in the initialization of the base module:
var
spQuiescent: TSchedulerPhase;
spInitialized: TSchedulerPhase;
Initialization
spQuiescent := TSchedulerPhase.Create;
...
The descendent class can add other states, and compute other transitions.
This is a classic implementation of the state design pattern.
I hope this helps,
Thierry.
More information about the fpc-pascal
mailing list