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.
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.
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.
ActionListener
is an example of the observer pattern. You register observers (or listeners) on a component that get called when a specific event occurs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With