Design mode, description with Delphi-> LOCK PATTERN

zhaozj2021-02-08  262

Lock Pattern

origin

Lock Pattern is the basis for TStrings data update control in Delphi. Some tstrings, tstringlist code will be exemplified later.

purpose

Provide a temporary access lock control mechanism for classes

motivation

We often find an object to regularly dispatch a notification that returns changes. The scheduled object must control its internal state to synchronize object data. If multiple changes to an object simultaneously, multiple notifications will be generated and a concurrent operation is generated. At this point, an Lock Pattern mode is added to your class, providing a temporary lock control from the face. Thus avoid unnecessary notifications.

We learn from the TBAG class:

Class TBAG provides the behavior of Collection. Each time you change, TBAG will schedule an onchange event. If we add multiple objects to TBAG, you will get multiple changes notifications. A graphics component may perform multiple refresh operations. After introducing Lock Pattern, turn on the TBAG lock before adding the object, turn off the lock after operation. In this way, I will get a single change notification. A better way is to join a sign, such as fupdating

application

The following is a TBAG class part code: (only some related code)

Type

TBAG = Class (TOBJECT)

Private

FLOCKCNT: Integer;

protected

Function Locked: Boolean;

Procedure setlocking (Updating: boolean);

public

Procedure lock;

Procedure unlock;

END;

IMPLEMENTATION

Procedure tbag.lock;

Begin

Inc (FLOCKCNT);

IF flockcnt = 1 THEN setLocking (false);

END;

Function TBag.locked: boolean;

Begin

Result: = (FLOCKCNT <> 0);

END;

Procedure tbag.seetlocking (Updating: boolean);

Begin

END;

Procedure tbag.unlock;

Begin

Dec (FLOCKCNT);

IF flockcnt = 0 THEN setLocking (true);

END;

TBAG description:

· FLOCKCNT Save the internal state of the lock mechanism. FLOCKCNT = 0 indicates that it is in a non-lock state, and other values ​​indicate that it is in a locked state. The class TBAG sets its value through the Lock and Unlock.

· Lock and UNLOCK provide lock interface, each call will cause lock of the lock and call the setLocking method.

· There is a Updating method in the setLocking method, the parameter of the Lock call is false, and the parameter called the Unlock call is True. By the way, you can insert an additional code in the method, assign it to it more operations indicating that its lock state changes. .

· Locked Returns the class lock status.

Note: Lock, UNLOCK must be used at the same time, thereby avoiding classes to remain in the locked state. Wise method is to use the try..finally block to keep the Lock, unlock simultaneously.

A typical application example:

Procedure TBag.Add (Item: Pointer);

Begin

{Add item to internal structure}

Change;

end

Procedure TBag.Additems (items: tlist);

Begin

LOCK;

{Add Multiple Items}

Try

For i: = 0 to items.count - 1 doadd (items [i]);

Finally

{Use try..finally to make sunelock is caled}

UNLOCK;

END;

END;

Procedure tbag.change;

Begin

IF not locked then

IF assigned (fonchange) THEN FONCHANGE (Self);

END;

Procedure tbag.seetlocking (Updating: boolean);

Begin

If Updating the {Bag Has Become Unlocked}

Change;

END;

Delphi Example: Tstring, TstringList

Tstrings = Class (TPERSIStent)

Private

FupdateCount: integer;

Protected

Procedure Exchange (INDEX1, INDEX2: Integer; Virtual;

Procedure setupdatestate (Updating: boolean); virtual;

public

Procedure beginupdate;

PROCEDURE ENDUPDATE;

PROCEDURE CHANGED;

VirtuR;

转载请注明原文地址:https://www.9cbs.com/read-3039.html

New Post(0)