Áö±Ý »ç¿ëÁßÀÎ À͸í¸Þ¼Òµå¸¦ »ç¿ëÇÑ GuardÇÔ¼ö(try..finally ´ë¿ë)À» Àû½À´Ï´Ù. Á¦°¡ °¡Áø ¼Ò½º´Â ´Ù¸¥ °Í°ú ¿¬°èµÈ °ÍÀÌ ¸¹¾Æ ¾Æ·¡ ´Ù¸¥ ºÐÀÇ ¼Ò½º¸¦ °£´ÜÇÏ°Ô ¼öÁ¤ÇÏ¿© Àû½À´Ï´Ù.
±âÁ¸ÀÇ Guard´Â °íÁ¤µÈ ÇüÅÂÀÇ ¸Þ¼Òµå ÇüŸ¸ °¡´ÉÇÏÁö¸¸ À͸íÇÔ¼ö¸¦ »ç¿ëÇϸé Á¦ÇÑÀÌ »ç¶óÁý´Ï´Ù.
=== »ç¿ë ¿¹ -====
var stream: TStream;
ds: TxxConnect;
begin
stream := TMemoryStream.Create;
Screen.Cursor := crHourglass;
ds := TxxConnect.Create;
//Finallyºí·°¿¡¼ »ç¿ëÇÏ´Â ±¸¹® Àüü¸¦ ÀûÀ¸¸é µË´Ï´Ù.
Guard( procedure begin
stream.Free;
Screen.Cursor := crDefault;
if (ds.Connected) then ds.Disconnect;
ds.Free;
end;);
ds.Connect;
.....
===== ÇÔ¼ö ¼Ò½º ====
TProc = reference to procedure ; //À͸íÇÔ¼ö
function Guard(event : TProc): IUnknown; overload;
implementation
type
TFinalObject = class(TInterfacedObject)
private
FProc : TProc;
public
constructor Create(Instance: TProc); overload;
destructor Destroy; override;
end;
constructor TFinalObject.Create(Instance: TProc);
begin
Fproc := Instance;
end;
destructor TFinalObject.Destroy;
begin
if Assigned(FProc) then FProc;
inherited;
end;
function Guard(event : TProc): IUnknown; overload;
begin
Result := TFinalObject.Create(event);
end;