Observer Pattern confusion
If i have a GUI builder which builds my first interface and to that interface I attach an observer which detects when a certain field in that interface is updated then do I have to do one of the following;
1) create interface1 from builder and then attach an observer so as the builder can know when a certain task has completed 2) Do i just create an observer and then the observer creates the interface?
Basically i am trying to create an interface from builder, which then, on a button click goes and gets some data. When this data has been retrieved i want to let the builder class know so it can proceed to use it to build interface 2. I am trying to user the observer design pattern to do this.
You should create the interface, then add (or register/attach) the relevant observers afterward. Something like (in pseudocode);
GUI mygui = GUIBuilder.createNewGUI(); ButtonListener OK = new OKButtonListener(); ButtonListener Cancel = new CancelButtonListener(); DataListener dataReady = new dataReadyListener(); mygui.registerButtonListener(OK); mygui.registerButtonListener(Cancel); mygui.registerDataListener(dataReady); mygui.show();
If all the GUIs you are building will have the same listeners attached, you can have the interface builder create and attach the listeners for you as well.