[Israel.pm] Delegates in Object Pascal

ik idokan at gmail.com
Sat Feb 3 02:43:10 PST 2007


On 2/3/07, Shlomi Fish <shlomif at iglu.org.il> wrote:
> Hi all!
>
> Here is ik's example of delegating methods using Object Pascal. I dare say
> it's hard for me to follow it because it's too verbose. From what I
> understand, he demonstrated one delegated method, but the question is whether
> it can be done with several delegated methods of different type signatures,
> while just listing their names (as is possible in Perl 5).

Well actually you can do it with as many methods are you wish. This
way of delegating methods are very common in object Pascal "method"
for callbacks events.

Note that on my lecture I talked about "property" and about the fact
that it can either accept methods or types. Well you can create a
callback types (of object) that will give you a chance to delegate
your methods.

Ido

>
> Regards,
>
>         Shlomi Fish
>
> ----------  Forwarded Message  ----------
>
> Date: Monday 29 January 2007 19:59
> From: ik <idokan at gmail.com>
> To: "Shlomi Fish" <shlomif at iglu.org.il>
>
> Anyway, for delegating stuff using Object Pascal you can do the following:
>
> program EventExample;
>
> uses
>  SysUtils, Classes;
>
> type
>  { define the event type }
>  TOnCalc = procedure (APercent: Integer) of object;
>
>  { class that triggers the event }
>  TCalculator = class
>  private
>    FOnCalc: TOnCalc;
>  public
>    procedure Calculate(A, B: Integer);
>    property OnCalc: TOnCalc read FOnCalc write FOnCalc;
>  end;
>
>  { class that uses the event }
>  TPresenter = class
>  public
>    procedure PresenterOnCalc(APercent: Integer);  // <--- this is the
> delegated method
>    procedure ShowCalc;
>  end;
>
> implementation
>
> { TCalculator }
> procedure TCalculator.Calculate(A, B: Integer);
> begin
>  Writeln(A + B);
>  { Execute the event if assigned from outside this class }
>  if Assigned(OnCalc) then
>    OnCalc(10); // 10 is the percent calculated at this moment
> end;
>
> { TPresenter }
> procedure TPresenter.ShowCalc;
> var
>  lCalculator: TCalculator;
> begin
>  lCalculator := TCalculator.Create;
>  lCalculator.OnCalc := PresenterOnCalc; // <-- assign the delegate
>  lCalculator.Free;
> end;
>
> procedure TPresenter.PresenterOnCalc(APercent: Integer);
> begin
>  Writeln('Calculated at this moment: ' + APercent + '%');
> end;
>
> end.
>
>
> ---------------------------------------------------------------------
> Shlomi Fish      shlomif at iglu.org.il
> Homepage:        http://www.shlomifish.org/
>
> Chuck Norris wrote a complete Perl 6 implementation in a day but then
> destroyed all evidence with his bare hands, so no one will know his secrets.
>


-- 
http://ik.homelinux.org/



More information about the Perl mailing list