Refactoring IState - ISignal

This commit is contained in:
Michael Schimmel
2025-06-24 09:24:37 +02:00
parent a3da63ad6a
commit 6ea0f94e36
11 changed files with 120 additions and 118 deletions
+27 -13
View File
@@ -8,30 +8,29 @@ uses
Myc.Signals;
type
// TMycNullSignal implements a "null object" pattern for IMycState.
// This state is always considered "set".
// TMycNullSignal implements a "null object" pattern for ISignal.
TMycNullSignal = class(TInterfacedObject, TSignal.ISignal)
public
function Subscribe(Subscriber: TSignal.ISubscriber): Pointer;
procedure Unsubscribe(Tag: Pointer);
end;
// TMycNullState implements a "null object" pattern for IMycState.
// TMycNullState implements a "null object" pattern for IState.
// This state is always considered "set".
TMycNullState = class(TInterfacedObject, TSignal.ISignal, TState.IState)
protected
// TState.IState
private
function GetSignal: TSignal;
function GetIsSet: Boolean;
public
function Subscribe(Subscriber: TSignal.ISubscriber): Pointer;
procedure Unsubscribe(Tag: Pointer);
property Signal: TSignal read GetSignal;
end;
// A state that acts as an event. It's state ist always set and it will always Trigger it's subscribers, if it gets notified by itself.
TMycEvent = class(TInterfacedObject, TSignal.ISubscriber, TSignal.ISignal, TEvent.IEvent)
strict private
FSubscribers: TMycNotifyList<TSignal.ISubscriber>;
protected
function GetSignal: TSignal;
function GetIsSet: Boolean;
public
@@ -69,7 +68,7 @@ type
[volatile]
FCount: Integer; // The internal countdown value for the latch.
function GetState: TState; // Implementation for TLatch.ILatch.GetState and IFlag.GetState.
protected
function GetSignal: TSignal;
function GetIsSet: Boolean;
public
// Creates the latch with an initial count.
@@ -103,11 +102,11 @@ type
[volatile]
FFlag: Boolean; // Internal state: true if dirty/set, false if clean/reset.
function GetState: TState;
protected
function GetSignal: TSignal;
function GetIsSet: Boolean;
public
// Creates a new dirty flag, initially set to dirty (true).
constructor Create;
constructor Create(AInit: Boolean);
destructor Destroy; override;
// Factory method to create a new TMycFlag instance.
@@ -166,6 +165,11 @@ begin
Result := true; // Null state is always set.
end;
function TMycNullState.GetSignal: TSignal;
begin
Result := Self;
end;
function TMycNullState.Subscribe(Subscriber: TSignal.ISubscriber): Pointer;
begin
// Since the state is always set, notify the subscriber immediately.
@@ -327,6 +331,11 @@ begin
Result := FCount <= 0;
end;
function TMycLatch.GetSignal: TSignal;
begin
Result := Self;
end;
function TMycLatch.GetState: TState;
begin
// TMycLatch itself implements TState.IState.
@@ -392,17 +401,17 @@ end;
{ TMycFlag }
constructor TMycFlag.Create;
constructor TMycFlag.Create(AInit: Boolean);
begin
inherited Create;
FFlag := false;
FFlag := AInit;
FSubscribers.Create;
end;
class function TMycFlag.CreateDirty: TFlag.IFlag;
begin
// Factory method to create a new TMycFlag instance.
Result := TMycFlag.Create;
Result := TMycFlag.Create( true );
end;
destructor TMycFlag.Destroy;
@@ -423,6 +432,11 @@ begin
Result := FFlag;
end;
function TMycFlag.GetSignal: TSignal;
begin
Result := Self;
end;
function TMycFlag.GetState: TState;
begin
// TMycFlag itself implements TState.IState.
@@ -497,7 +511,7 @@ end;
constructor TMycObserverFlag.Create(const ASignal: TSignal.ISignal);
begin
inherited Create;
inherited Create( false );
FSignal := ASignal;
end;