Forum > General


(1/3) > >>


So i wanted to ask if there is a Simple Example Somewhere on how to use Observers. I know how it works in theory but not how in practice.

Problem Simplified (its more komplex with much more Components, thats the reason why i wanted to decapsulate my GUI from my Data/Calculations and Notify both classes about Changes via Observers):
I have 2 classes: A GUI form with 3 TEdit's, and a DataClass which stores the values of these 3 Edit's, in e.g. Val1, Val2 and Val3.
If I change Edit1 or Edit2, I want to notify my data class. This class should then update the changed fields. After that the value of Edit1 and Edit2 are added together and the result is written to Val3. Since Val3 has now changed and therefore no longer has the same value as Edit3 from the GUI, this field should now also be updated by the observer.

I hope you understood what I am trying to achieve.

My Programm just gets bigger and bigger so i wanted to implement an MVC pattern, buts that a bit too complex jet for me so i just wanted to start with encapsulate the GUI from Calculations/Data and Communicate with Observers.
For Example an Observer for every TEdit i have. Currently using onChange events but my code gets unreadable. I reuse those Events like this: Combo1Change(Nil), and have like everything mixed in this whole Class.

I would be very happy if someone can help me here. :)

Michael wrote several articles.

You can find them at the address below.
The article about the cdcover implements the observer pattern.
source-code is available

I don't remember where exactly I got this example (I think it was in the FPC mailing list), but maybe it can help you to understand the observer pattern.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{ 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.  

Thanks :) if there are more Examples or Explanations please let me know. i realy want to know what i am doing !

Here is another very simple example using TStringList (which already implements IFPObserved) and TEdit (which does not implement IFPObserved, so you have to add it).
The example is based on your skeleton of two edits whose Texts the observing control combines.


[0] Message Index

[#] Next page

Go to full version