New multicast event implementation

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

New multicast event implementation

Anthony Walter-3
Hello all. I implemented a new type of multicast events in Delphi based on generics. I thought someone could maybe consider and convert this implementation to Free Pascal. Here is a link to an article describing the implementation, its benefits and usage, and well as the full source code. Let me know what you think.

http://www.codebot.org/delphi/?doc=9568

Here are two examples where multicast events to are benefiting me .

1) I have a SystemEvents singleton class that exposes multicast events to which other classes may connect. Events include OnFontChange, OnColorsChange, OnUserSwitch ... which fire when the system fonts or colors change etc.

2) I've written an application configuration class which can be edited while the program is running. This class exposes an OnChange multicast event which many other objects listen to and take action upon if they are interested in configuration changes, such as updating layout or visible options (http://imagebot.org/snapshops/edit-dialog.jpg).

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: New multicast event implementation

michael.vancanneyt



On Tue, 1 Feb 2011, Anthony Walter wrote:

> Hello all. I implemented a new type of multicast events in Delphi based on
> generics. I thought someone could maybe consider and convert this
> implementation to Free Pascal. Here is a link to an article describing the
> implementation, its benefits and usage, and well as the full source code.
> Let me know what you think.
>
> http://www.codebot.org/delphi/?doc=9568
>
> Here are two examples where multicast events to are benefiting me .
>
> 1) I have a SystemEvents singleton class that exposes multicast events to
> which other classes may connect. Events include OnFontChange,
> OnColorsChange, OnUserSwitch ... which fire when the system fonts or colors
> change etc.
>
> 2) I've written an application configuration class which can be edited while
> the program is running. This class exposes an OnChange multicast event which
> many other objects listen to and take action upon if they are interested in
> configuration changes, such as updating layout or visible options (
> http://imagebot.org/snapshops/edit-dialog.jpg).

Why did you not use the observer pattern as used in tiOPF ?
It does not need generics, and is well tested since a long time.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Ben
Reply | Threaded
Open this post in threaded view
|

Re: New multicast event implementation

Ben
In reply to this post by Anthony Walter-3
Op 2011-02-02 03:45, Anthony Walter het geskryf:
> Hello all. I implemented a new type of multicast events in Delphi based

I've always liked the idea of supporting multiple event handlers per
event. Java, Qt, C# has had it for years.

BTW:
The print function (printer friendly version) at the bottom of your
website excludes all the source code examples - making the printout
rather useless. Just thought I would mention that.



--

            Ben.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: New multicast event implementation

Anthony Walter-3
Yeah, I just added the code highlighter, which build the highlights using javascript. I set to "display: none" while the page loads which isn't effecting the print version. The workaround is that you can print the code blocks by clicking the print icon in each code section. I'll fix it later.

Regarding the observer pattern, I'll refer you to this stack overflow question:

http://stackoverflow.com/questions/550785/c-events-or-an-observer-interface-pros-cons

My take is that event are more natural and that they are probably more well established than the pattern of creating an interface to subscribe to events. Once the plumbing is in place (adding method pointer types to a language), they are much easier to hook up. Events also allow you hook up to the only the notifications in which you are interested. If you've used Visual Basic (the old one), Delphi, or C#  hooking up mouse click, key press, or selection change events becomes more natural.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: New multicast event implementation

Anthony Walter-3
In reply to this post by Ben
Just an update ... I fixed the print feature at the bottom.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: New multicast event implementation

michael.vancanneyt
In reply to this post by Anthony Walter-3


On Wed, 2 Feb 2011, Anthony Walter wrote:

> Yeah, I just added the code highlighter, which build the highlights using
> javascript. I set to "display: none" while the page loads which isn't
> effecting the print version. The workaround is that you can print the code
> blocks by clicking the print icon in each code section. I'll fix it later.
>
> Regarding the observer pattern, I'll refer you to this stack overflow
> question:
>
> http://stackoverflow.com/questions/550785/c-events-or-an-observer-interface-pros-cons
>
> My take is that event are more natural and that they are probably more well
> established than the pattern of creating an interface to subscribe to
> events.

By contrast, generics are not established at all in Object Pascal,
so I wouldn't exactly agree with you that it is more natural :-)

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: New multicast event implementation

Anthony Walter-3
By the way, does Free Pascal generic support generic constraints yet? Also, this format seems more natural to me:

TCollection<T: TCollectionItem> = class
  property Count: Integer;
  property Items[Index: Integer]: T;
end;

TStatusPanels = class(TCollection<TStatusPanel>);

rather than ...

generic TCollection<T> = class // no constrain ability
  property Count: Integer;
  property Items[Index: Integer]: T;
end;

TStatusPanels = specialize TCollection<TStatusPanel>;

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: New multicast event implementation

michael.vancanneyt



On Wed, 2 Feb 2011, Anthony Walter wrote:

> By the way, does Free Pascal generic support generic constraints yet? Also,
> this format seems more natural to me:

No, it does not.

Michael.

>
> TCollection<T: TCollectionItem> = class
>  property Count: Integer;
>  property Items[Index: Integer]: T;
> end;
>
> TStatusPanels = class(TCollection<TStatusPanel>);
>
> rather than ...
>
> generic TCollection<T> = class // no constrain ability
>  property Count: Integer;
>  property Items[Index: Integer]: T;
> end;
>
> TStatusPanels = specialize TCollection<TStatusPanel>;
>
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Ben
Reply | Threaded
Open this post in threaded view
|

Re: New multicast event implementation

Ben
In reply to this post by Anthony Walter-3
Op 2011-02-02 15:55, Anthony Walter het geskryf:
> isn't effecting the print version. The workaround is that you can print
> the code blocks by clicking the print icon in each code section. I'll
> fix it later.

I simply used the Browser's Print menu option, instead of the "printer
friendly webpage option", and that worked fine for my needs.


> Regarding the observer pattern, I'll refer you to this stack overflow
> question:
>
> http://stackoverflow.com/questions/550785/c-events-or-an-observer-interface-pros-cons
>
> My take is that event are more natural and that they are probably more
> well established than the pattern of creating an interface to subscribe


Regarding that stackoverlflow question. That is highly dependant on how
you implemented the observer in your BOM. Years ago, I implemented a
observer via Interfaces in our Object Pascal code, where I could observe
just the fields of a class I was interested in. No generics required,
just a plain observer implementation using a InterfaceList - by creating
a base BOM class which all your other BO descend from.

I'm not saying your idea or implementation is bad - though personally I
have never had the need to use generics yet (and it seems neither has
many others - good or bad language feature, I don't know?). I'm just
pointing out that there are multiple ways of doing the same thing.
Observer could very easily be used too, and without the pitfalls listed
in the Stack Overflow article.



--

            Ben.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: New multicast event implementation

Anthony Walter-3
Have you used List<T>, Dictionary<K,V>, or IEnumerable<T> in C# much? How about EventHandler<T>? They are pretty useful.

Even in Delphi, TList<T>, TObjectList<T>, and TCompare<T> are more than handy IMO. No longer do I have to write "TMyObject(FList[I])" all over the place. When using "if FList.IndexOf(Pointer(FInstance)) > 1" not only do you need a typecast again, but you lose the ability to write or build in TCompare<T>, for example to consider objects equal by comparing a specific field.

Anyhow, I didn't mean this discussion to be about the merits of generics in programming.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: New multicast event implementation

Sven Barth-2
In reply to this post by Anthony Walter-3
Am 02.02.2011 15:40, schrieb Anthony Walter:
> By the way, does Free Pascal generic support generic constraints yet?
> Also, this format seems more natural to me:
>

They aren't implemented currently, but I hope they will at some time.

> TCollection<T: TCollectionItem> = class
>    property Count: Integer;
>    property Items[Index: Integer]: T;
> end;
>
> TStatusPanels = class(TCollection<TStatusPanel>);
>
> rather than ...
>
> generic TCollection<T> = class // no constrain ability
>    property Count: Integer;
>    property Items[Index: Integer]: T;
> end;
>
> TStatusPanels = specialize TCollection<TStatusPanel>;

When using FPC 2.5.1 you can use the Delphi compatible syntax in mode
Delphi (not supported in mode ObjFPC).

When constraints are implemented they should work in both modes.

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal