On 2012-11-15 01:15, bsquared wrote:
> Hello,
> 
> Does anyone know of any example code for the FPObserver interface? 
> Preferable with Collections.

Sure, see attached. It's very basic, but shows the functionality.

NOTE:
  I used latest 2.7.1 compiler, where some base classes in the RTL
  support the IFPObserved interface.


And the demo output is as follows:

------------------------------------
$ ./fpobserver_demo
TStringList has changed [ooChange]
TStringList has changed [ooChange]
TStringList has changed [ooChange]
[ooFree] detected so we should detach ourselves
------------------------------------



This does raise some points of interest though.

 1) I would probably surface the IFPObserver methods to Public. Though
    there is good arguments to not do it either - thus you are forced
    to use correct interface usage... via Supports(), getting a
    interface pointer back, and using that interface pointer to make
    method calls.

 2) Even though I did two Add() calls and one Delete() call, TStringList
    reported the operation as simply ooChanged. Correct, but not very
    precise. After all, we also have ooAdd, ooDelete operation states
    we could work with.

 3) My personal preference is that the Observed automatically detaches
    Observers in the destructor. But this is just a personal preference
    in my own code.


Regards,
  - Graeme -

{ This demo is very basic, but shows the Observer support in the RTL }
program fpobserver_demo;

{$mode objfpc}{$h+}
{$ifdef mswindows}{$apptype console}{$endif}

uses
 Classes, SysUtils, typinfo;

type
  TMyObserver = class(TObject, IFPObserver)
  private
    procedure FPOObservedChanged(ASender : TObject; Operation : TFPObservedOperation; Data : Pointer);
  end; 
  
{ TMyObserver }

procedure TMyObserver.FPOObservedChanged(ASender: TObject;
               Operation: TFPObservedOperation; Data: Pointer);

  function OperationToString(AOperation: TFPObservedOperation): string;
  begin
    result := GetEnumName(TypeInfo(TFPObservedOperation),
                         Ord(AOperation));
  end;
var
  intf: IFPObserved;
begin
  if Operation = ooFree then
  begin
    writeln('[ooFree] detected so we should detach ourselves');
    if Supports(ASender, IFPObserved, intf) then
      intf.FPODetachObserver(self);
  end
  else
  begin
    writeln(ASender.ClassName + ' has changed ['+
      OperationToString(Operation) + ']');
  end;
end;
  
var
  sl: TStringList;
  observer: TMyObserver;
  intf: IFPObserved;
begin
  { This stringlist will be the subject (observed) }
  sl := TStringList.Create;
  { this instance will be the observer - notified when StringList changes }
  observer := TMyObserver.Create;

  { attach observer }  
  if Supports(sl, IFPObserved, intf) then
  begin
    intf.FPOAttachObserver(observer);
  end;
  
  { Do something to the stringlist }
  sl.Add('Item one');
  sl.Add('Item two');
  sl.Delete(0);
  
  { Clean-up code - also shows ooFree operation }
  sl.Free;
  observer.Free;
end.

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to