Question

It seems to me that update a property is always an 'event' and that all you can do with an event handler can be done in the property let/set routine. And, in my limited experience, I never needed to raise an event without change a property.

So, I see them as a duplicate for a functional point of view and maybe useful for the code's structure. I always worked with small jobs maybe I miss the big picture.

When and why should I prefer one or the other?

Était-ce utile?

La solution

The benefit of events is that you (or more precisely, consumers of your component) can subscribe to them. The one implementing the event handler is typically not the same person as the one that created the object that publishes the event.

Events make an object more versatile. They allow the object to be used in a context unforeseen by its creator without the need to change the object itself. The object can be created by one person at one moment, its events may be implemented by someone else at a later time.

Autres conseils

There are two levels of abstraction in your question:

1) What is happening programmatically in my code

2) What is happening under a system's view in my system modeled by my code

Setting a property (programmatically) is simply a statement of your code which gets executed. So from a POV of your system everytime a statement is executed, something is happening in your system. This could be called an "event". Typically you are interested in data and it's changes. In turn everytime something happens to your data that might be an event of interest.

Question: When should I use events?

Answer: Everytime your system itself or other systems have to take notice that data was somehow changed.

If you use the pattern of Events you have one part of the system which is being changed enabled to notify other parts of the same system or other systems to take notice that this change-event has happened.

The part which takes notice of an event happened could in turn change the state of other parts of the system it is part of. In consequence e.g. there might be a property set.

Unless you do not have the need for such a communication pattern events are of no real use.

It seems to me that update a property is always an 'event' ...

Linguistically, yes.
You set a property at some point in time and under some condition and that can be described as an "event". But we're Developers, so let's go beyond "casual" language and get into technicalities ...

A Property allows you, as its Developer, to control the data entering your component. Sure, you could just expose a field (i.e. a variable) and let the consumers of your component set that field to whatever [insane] value they feel like ... but that would be Madness. So we write Properties to allow us to filter out the dross that we might get sent.

We use an Event to tell the "Outside World" that something in which they're interested has "happened" within our component.

... all you can do with an event handler can be done in the property let/set routine.

Everything that the component Developer can do has to be done in the Property, because that's the only part that they have control over. By raising an Event, they are telling the Whole, Wild World (or, at least, any other code that has subscribed to the Event) that "something" has happened. They have no control over whether that event will be handled at all, or what the other code might do as a result of it.

... I never needed to raise an event without change a property.

This is especially true when designing User Interface controls, where the two do seem to go hand-in-hand. Press a value into a Property and an Event pops out to let you now you've done it.

However, there are cases when Events can be raised without an [external] agent making a change through one of the component's properties.

When and why should I prefer one or the other?

Personally, I'd stick with both.

Licencié sous: CC-BY-SA avec attribution
scroll top