Recent

Author Topic: Observer but more simple  (Read 3023 times)

Weitentaaal

  • Full Member
  • ***
  • Posts: 185
Observer but more simple
« on: May 18, 2021, 02:01:48 pm »
Hello Guys :)

Just a Newbie Question i guess

I did implement a Drag and Drop with 2 listBoxes (every Item Has a Record as Object)... After that i had Some Conditions wich needed to be checked. I had to check if certains items are Checked or not checked (the Rocord has a Property "IsChecked"). The Problem now is that i have 2 listboxes  so i would have to check in First and second Listbox, wich would take many resources bc to check if a item is checked i have to Search it First so i would always have to iterate through both List's to find it and there is not just 1 item wich needs to be checked on so i would iterate so many times through my Lists. Any Good Solution ? i know my english is not so good so i wrote a Summary of my problem:

-2 ListBoxes
-Drag and Drop
-Every Item has a Record Stored wich referes to Data.
-Data is Record with a property "isChecked"

What i have to do:

-Every Time i drag an item in other List i have to check for Some items
-Are they Checked ? (the Property)
-lot of Computing needed if i iterate always through listboxes to search

Is there something like an observer wich checks if certain item is dragged to there and if so then store something ?
Lazarus: 2.0.6 x86_64-win64-win32/win64
FPC-Version: 3.0.4
Compiler Version: 3.2.0

lucamar

  • Hero Member
  • *****
  • Posts: 4009
Re: Observer but more simple
« Reply #1 on: May 18, 2021, 02:21:21 pm »
You could trigger an event from the record when isChecked changes. Then, in the handler, store the information in e.g. an array and, when you need  to, just check if the item in question is in the array.

Of course, it depends quite a lot on what exactly you're doing and why but that or a similar solution should work well, IMHO.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Thaddy

  • Hero Member
  • *****
  • Posts: 10784
Re: Observer but more simple
« Reply #2 on: May 18, 2021, 03:06:02 pm »
That is a way to solve it, but FPC has already a built-in observer pattern for classes:
See https://www.freepascal.org/docs-html/rtl/classes/ifpobserved.html

And that is about the simplest you can get.

winni

  • Hero Member
  • *****
  • Posts: 2406
Re: Observer but more simple
« Reply #3 on: May 18, 2021, 05:36:31 pm »

Hi!

There is a simple example in

Code: Text  [Select][+][-]
  1. fpcsrc/packages/fcl-base/examples/dobserver.pp

Winni

cdbc

  • Sr. Member
  • ****
  • Posts: 361
    • http://www.cdbc.dk
Re: Observer but more simple
« Reply #4 on: May 18, 2021, 06:57:38 pm »
Hi
Every class that descends from TPersistent already have the I/Tobserved implemented.
All you have to do, is implement the IObserver/TObserver
class, Ie: one method or one class with one method.
EasyPeasy... "Use the source Luke"  :D
Ex: Form1 = TForm1(IObserver)
      .....
      Procedure FPOObservedChanged(ASender : TObject; Operation : TFPObservedOperation; Data : Pointer);
      End;
or...
You can roll your own, like this: https://github.com/cdbc-dk/bc_rtl/blob/master/bc_observer_2.pas
Regards Benny
« Last Edit: May 18, 2021, 07:05:19 pm by cdbc »
If it ain't broke, don't fix it ;)

Weitentaaal

  • Full Member
  • ***
  • Posts: 185
Re: Observer but more simple
« Reply #5 on: May 19, 2021, 11:09:05 am »
Thank u all Guys for ur Replys :)

I Have all info i need now but still didn't get it how to create my own Event. For Example a Property ISChecked has its own event, How do i assign it to the Property ? and how should an Event Look Like to work ?
Lazarus: 2.0.6 x86_64-win64-win32/win64
FPC-Version: 3.0.4
Compiler Version: 3.2.0

lucamar

  • Hero Member
  • *****
  • Posts: 4009
Re: Observer but more simple
« Reply #6 on: May 19, 2021, 01:27:34 pm »
I Have all info i need now but still didn't get it how to create my own Event. For Example a Property ISChecked has its own event, How do i assign it to the Property ? and how should an Event Look Like to work ?

It's a little more complex for records than for objects or classes but it migt work like, say, first declaring:
Code: Pascal  [Select][+][-]
  1. {needs:
  2. $modeswitch advancedrecords}
  3.  
  4.   PImaginary = ^TImaginary;
  5.  
  6.   TChangeEvent = procedure(Sender: PImaginary; var ChangeState: Boolean);
  7.  
  8.   TImaginary = record
  9.   private
  10.     FIsChecked: Boolean;
  11.     FOnCheckedChange: TChangeEvent;
  12.     procedure SetIsChecked(AValue: Boolean);
  13.     procedure SetOnCheckedChange(AValue: TChangeEvent);
  14.   public
  15.     property OnCheckedChange: TChangeEvent read FOnCheckedChange write SetOnCheckedChange;
  16.     property IsChecked: Boolean read FIsChecked write SetIsChecked;
  17.   end;

Then you define the property setters as:

Code: Pascal  [Select][+][-]
  1. { Set the event handler }
  2. procedure TImaginary.SetOnCheckedChange(AValue: TChangeEvent);
  3. begin
  4.   if FOnCheckedChange <> AValue then
  5.     FOnCheckedChange := AValue;
  6. end;
  7.  
  8. { Set IsChecked and act in consequence }
  9. procedure TImaginary.SetIsChecked(AValue: Boolean);
  10. begin
  11.   if FIsChecked <> AValue then begin
  12.     if Assigned(FOnCheckedChange) then
  13.       FOnCheckedChange(Self, AValue);
  14.       {Re-test, because the event handler might have changed it}
  15.       if FIsChecked <> AValue then begin
  16.         FIsChecked := AValue;
  17.         {... whatever else ...}
  18.       end;
  19.   end;
  20. end;

Note, though, that records (even extended ones) are not really mean for this kind of manipulations (hence e.g. the need for the pointer type); you'd be much better off using an old-style object or a class, even simple ones.
« Last Edit: May 19, 2021, 01:29:20 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Weitentaaal

  • Full Member
  • ***
  • Posts: 185
Re: Observer but more simple
« Reply #7 on: May 19, 2021, 03:25:00 pm »
Thank you Lucamar ... i have to much Code to rewrite if i use a Class/Object instead but i will definitely try this out in an new Project.

your example was very helpful :)

LG
Lazarus: 2.0.6 x86_64-win64-win32/win64
FPC-Version: 3.0.4
Compiler Version: 3.2.0

 

TinyPortal © 2005-2018