java - how to call object A's method from object B which itself is a property of the A's object? -
i have login form dialog box displayed in beginning step , user must login continue application. problem don't know how application should know when login process completed , continue. think should register windowlistener , implement windowclosing event. , inside should call method application continue process. don't know how solution correct. because implemented login form class extends jdialog , application instantiated local variable. think not correct call application's methods inside login class. i'm not sure. it's feeling. suggest?
public class application { private login login = null; public application() { login = new login(); } public continue() { //... } } public class login extends jdialog { public login() { //... } public void processlogin() { } private class windoweventhandler implements windowlistener { public void windowclosing(windowevent e) { if(#loginprocesssuccessful?) { // call application's continue method } } } }
use , observer pattern (aka listener).
basically, login provide kind registration process allow interested parties register instance of class.
when required event occurs, login notify each of these parties event has occurred...
the basic premises limit amount of information expose between classes. login shouldn't care else other telling listener event has occured. means not tightly coupling code together, giving greater flexibility.
public interface loginlistener { public void loginsucceeded(); // want return information caller... public void loginfailed(); } public class application implements loginlistener { private login login = null; public application() { login = new login(this); } public void loginsucceeded() { // yea me } public void loginfailed() { // sucks } } public class login extends jdialog { private loginlistener listener; public login(loginlistener listener) { //... } public void processlogin() { if (loginsuccessful()) { listener.loginsucceeded(); } else { listener.loginfailed(); } } } note: example uses single listener, there's no reason why should limit in way , provide mechanism registering multiple callbacks
Comments
Post a Comment