1

I'm having a hard time understanding the DEM (Delegation Event Model) in Java. After reading this question with answers I didn't get the explanation I needed.

What are the motivations for using DEM?

My observations in the examples I've seen are:

  • There is a model with a list of listeners.
  • That model also has a methods like fireThisOrThat() that are called from other methods like addThisOrThat.

I will get this on an upcoming exam, that's why I need to understand it and how to use it.

Community
  • 1
  • 1
whirlwin
  • 16,044
  • 17
  • 67
  • 98

2 Answers2

3

Delegation means a source generates an event and sends it to one or more listeners.

Java Swing is an excellent example of delegation. A Swing object, like a JButton, will generate events. In the example of a JButton, an event would be that the button image receives a left click (the button is pressed).

Any number of other objects can listen to the JButton. This allows one or more event actions to take place as a result of the JButton event.

Suppose you have a JTextArea. You can have a listener on the JButton such that when the JButton fires a left mouse click event, the JTextArea listener edits the text and writes the text to a database. In other words, you push (left click) a button on the screen, and the text in a JTextArea is written to a database.

By using delegation, the JButton and the JTextArea aren't coupled together. You could replace the JTextArea with another Swing component, and the Swing components would still work together.

Andrew Rasmussen
  • 14,912
  • 10
  • 45
  • 81
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
0

In Delegation Event Model, a source generates an event and sends it to one or more listeners. Here, listener waits until it receives an event. Once the listener receives an event, it processes the event and then returns. The advantage of this design is that the application logic that processes events is clearly separated from the user interface that generates those events.

In the delegation event model, listener must register with a source in order to receive an event notification. The advantage of this is that the notifications are sent only to those listeners that want to receive them.

Madan Poudel
  • 61
  • 1
  • 4