Refactoring
This commit is contained in:
+20
-20
@@ -12,7 +12,7 @@ type
|
||||
protected
|
||||
function GetIsSet: Boolean; virtual; abstract;
|
||||
public
|
||||
function Subscribe(Subscriber: IMycSubscriber): TMycSubscription; virtual; abstract;
|
||||
function Subscribe(Subscriber: IMycSubscriber): Pointer; virtual; abstract;
|
||||
procedure Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag); virtual; abstract;
|
||||
property IsSet: Boolean read GetIsSet;
|
||||
end;
|
||||
@@ -23,7 +23,7 @@ type
|
||||
protected
|
||||
// IMycState
|
||||
function GetIsSet: Boolean;
|
||||
function Subscribe(Subscriber: IMycSubscriber): TMycSubscription;
|
||||
function Subscribe(Subscriber: IMycSubscriber): Pointer;
|
||||
procedure Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag);
|
||||
public
|
||||
constructor Create;
|
||||
@@ -38,7 +38,7 @@ type
|
||||
FSubscribers: TMycNotifyList<IMycSubscriber>; // List of subscribers waiting for this latch to be set.
|
||||
private
|
||||
[volatile] FCount: Integer; // The internal countdown value for the latch.
|
||||
function GetState: IMycState; // Implementation for IMycLatch.GetState and IMycFlag.GetState.
|
||||
function GetState: TState; // Implementation for IMycLatch.GetState and IMycFlag.GetState.
|
||||
protected
|
||||
function GetIsSet: Boolean; override; final; // Implementation for IMycState.GetIsSet.
|
||||
public
|
||||
@@ -48,7 +48,7 @@ type
|
||||
destructor Destroy; override;
|
||||
|
||||
// IMycSignal implementation
|
||||
function Subscribe(Subscriber: IMycSubscriber): TMycSubscription; override;
|
||||
function Subscribe(Subscriber: IMycSubscriber): Pointer; override;
|
||||
procedure Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag); override;
|
||||
|
||||
// IMycSubscriber implementation for TMycLatch itself.
|
||||
@@ -59,7 +59,7 @@ type
|
||||
|
||||
TMycNullLatch = class( TInterfacedObject, IMycLatch )
|
||||
private
|
||||
function GetState: IMycState;
|
||||
function GetState: TState;
|
||||
function Notify: Boolean;
|
||||
end;
|
||||
|
||||
@@ -71,7 +71,7 @@ type
|
||||
FSubscribers: TMycNotifyList<IMycSubscriber>; // List of subscribers interested in state changes of this dirty flag.
|
||||
private
|
||||
[volatile] FFlag: Boolean; // Internal state: true if dirty/set, false if clean/reset.
|
||||
function GetState: IMycState; // Implementation for IMycFlag.GetState.
|
||||
function GetState: TState;
|
||||
protected
|
||||
function GetIsSet: Boolean; override; final; // Implementation for IMycState.GetIsSet (true if dirty).
|
||||
public
|
||||
@@ -82,7 +82,7 @@ type
|
||||
// Factory method to create a new TMycDirty instance.
|
||||
class function CreateDirty: IMycDirty; static;
|
||||
|
||||
function Subscribe(Subscriber: IMycSubscriber): TMycSubscription; override;
|
||||
function Subscribe(Subscriber: IMycSubscriber): Pointer; override;
|
||||
procedure Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag); override;
|
||||
|
||||
// IMycSubscriber implementation for TMycDirty.
|
||||
@@ -95,7 +95,7 @@ type
|
||||
|
||||
TMycNullDirty = class( TInterfacedObject, IMycDirty )
|
||||
private
|
||||
function GetState: IMycState;
|
||||
function GetState: TState;
|
||||
function Notify: Boolean;
|
||||
function Reset: Boolean;
|
||||
end;
|
||||
@@ -117,7 +117,7 @@ begin
|
||||
Result := true; // Null state is always set.
|
||||
end;
|
||||
|
||||
function TMycNullState.Subscribe(Subscriber: IMycSubscriber): TMycSubscription;
|
||||
function TMycNullState.Subscribe(Subscriber: IMycSubscriber): Pointer;
|
||||
begin
|
||||
// Since the state is always set, notify the subscriber immediately.
|
||||
if Assigned(Subscriber) then
|
||||
@@ -125,7 +125,7 @@ begin
|
||||
Subscriber.Notify;
|
||||
end;
|
||||
// No ongoing subscription is necessary or meaningful for a null state.
|
||||
Result.Create(Self, nil); // Return an empty subscription.
|
||||
Result := nil;
|
||||
end;
|
||||
|
||||
procedure TMycNullState.Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag);
|
||||
@@ -135,7 +135,7 @@ end;
|
||||
|
||||
{ TMycNullLatch }
|
||||
|
||||
function TMycNullLatch.GetState: IMycState;
|
||||
function TMycNullLatch.GetState: TState;
|
||||
begin
|
||||
Result := TState.Null;
|
||||
end;
|
||||
@@ -202,13 +202,13 @@ begin
|
||||
Result := FCount <= 0;
|
||||
end;
|
||||
|
||||
function TMycLatch.GetState: IMycState;
|
||||
function TMycLatch.GetState: TState;
|
||||
begin
|
||||
// TMycLatch itself implements IMycState.
|
||||
exit(Self);
|
||||
Result := Self;
|
||||
end;
|
||||
|
||||
function TMycLatch.Subscribe(Subscriber: IMycSubscriber): TMycSubscription;
|
||||
function TMycLatch.Subscribe(Subscriber: IMycSubscriber): Pointer;
|
||||
var
|
||||
alreadySet: Boolean;
|
||||
begin
|
||||
@@ -227,7 +227,7 @@ begin
|
||||
else
|
||||
begin
|
||||
// If not yet set, advise the subscriber.
|
||||
Result.Create(Self, FSubscribers.Advise(Subscriber));
|
||||
Result := FSubscribers.Advise(Subscriber);
|
||||
end;
|
||||
finally
|
||||
FSubscribers.Release;
|
||||
@@ -249,7 +249,7 @@ end;
|
||||
|
||||
{ TMycNullDirty }
|
||||
|
||||
function TMycNullDirty.GetState: IMycState;
|
||||
function TMycNullDirty.GetState: TState;
|
||||
begin
|
||||
Result := TState.Null;
|
||||
end;
|
||||
@@ -297,10 +297,10 @@ begin
|
||||
Result := FFlag;
|
||||
end;
|
||||
|
||||
function TMycDirty.GetState: IMycState;
|
||||
function TMycDirty.GetState: TState;
|
||||
begin
|
||||
// TMycDirty itself implements IMycState.
|
||||
exit(Self);
|
||||
Result := Self;
|
||||
end;
|
||||
|
||||
function TMycDirty.Notify: Boolean;
|
||||
@@ -329,7 +329,7 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
function TMycDirty.Subscribe(Subscriber: IMycSubscriber): TMycSubscription;
|
||||
function TMycDirty.Subscribe(Subscriber: IMycSubscriber): Pointer;
|
||||
begin
|
||||
if not Assigned(Subscriber) then
|
||||
exit;
|
||||
@@ -341,7 +341,7 @@ begin
|
||||
FSubscribers.Lock;
|
||||
try
|
||||
// Add subscriber regardless of current state.
|
||||
Result.Create(Self, FSubscribers.Advise(Subscriber));
|
||||
Result := FSubscribers.Advise(Subscriber);
|
||||
if FFlag then // If currently dirty, notify the new subscriber immediately.
|
||||
begin
|
||||
Subscriber.Notify;
|
||||
|
||||
Reference in New Issue
Block a user