Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is ActionListener an example of Delegation pattern?

In my college days, I never realized what patterns were there in the Java API. Now at work I came across Delegation pattern in Objective C n Cocoa on iOS where one screen sets itself as a delegate on coming screen so that that screen can pass some message to that delegate and it can take some action when it comes back to the previous screen.

I realize that I use to do something similar with when I used to pass "this" as as ActionListener [by implementing the interface] to a JButton and it would automatically call actionPerformed implemented by me in this class and thus I could change any instance data in my JFrame class.

So Is ActionListener an example of Delegate If I am correct ?

EDIT: As correctly mentioned below, It is Observer pattern. We dont set ActionListener we add one. Thus there can be many Listeners to that action.

like image 814
Amogh Talpallikar Avatar asked Jan 12 '12 06:01

Amogh Talpallikar


People also ask

Which design pattern is used when we attach Actionlisteners to Jbuttons?

This is the essence of the observer design pattern. The ActionListener objects added to JButton are notified of the action in that JButton , and they can take the proper action.

How does action listener work?

Action listeners register for Events using the Observer pattern and they are notified, by the main event loop, of any events they are registered for. So no, it's not a polling (pull) mechanism, but the opposite - a (push) callback. This is an example of 'don't call us, we'll call you' programming.


1 Answers

ActionListener is an example of the observer pattern. You register observers (or listeners) on a component that get called when a specific event occurs.

like image 157
casablanca Avatar answered Oct 20 '22 00:10

casablanca