0

I want to implement the Observer pattern to my application.Since my code is large i will sum the problem. So, i have to make a application that manages the relations between a boss and some employees. For this, I have two separate gui for both the boss and employee.

When i lunch the application i create two loggins , one for boss and one for employee. If the loggin for boss is succesfull then it will create a new Gui Boss frame

If the employee loggin is successfull then a new Gui Employee will show up. What i want to do, is to add for the new Gui Employee created (wich is observable) an observer (Gui Boss). Here is some of my code to explain better:

//in Application.java
public static void main(String args[]){
    ........
    LoginBoss lb = new LoginBoss(contrb);  //contrb is boss controller
    lb.setVisible(true);
    LoginEmployee le = new LoginEmployee(contre); //contre is employee controller
    le.setVisible(true);
}

//In LogginBoss.java
//after i check if it's ok i create a new gui boss
guiB = new GuiBoss(contrb,boss);  //contrb is boss controller passed from loggin boss
guiB.setVisible(true);

//In LogginEmployee.java
///after i check if it's ok i create a new gui employee
guiE = new GuiEmployee(contre,employee);
guiE.setVisible(true);
//now i whant to add for guiE an Gui Boss observer, something like guiE.addObserver(guiB);

If you have any ideeas please, let me know.I'm sorry for my english.

user2271933
  • 491
  • 2
  • 10
  • 25
  • Se also this [answer](http://stackoverflow.com/a/3072979/230513). – trashgod May 15 '13 at 11:15
  • Userinterfaces (View) should not be Observable, instead the data (Model) witch they display. So if employee's data changes the boss's UI get notifyed. You might want to to reconsider your design, and probably implment some role concept (boss, employee) and use only one UI for each purpose. A simple role concept may be implmented using inheritance -> `Boss extends Employee` + For both View and Model and overriding methods where functionality should be extended. – A4L May 15 '13 at 11:37

1 Answers1

0

Take a look at this link.

If GuiEmployee extends the Observer pattern, you should make something like:

GuiEmployee watched = new GuiEmployee(var, var);

watched.addObserver(guiB);

watched.changeSomeValue(value);

In the changeSomeValue method,inside GuiEmployee (watched in my example) you must include these sentences to notify observers:

setChanged();
notifyObservers(value);

A call to notifyObservers will do nothing until setChanged is called. For more info, take a look here.

Community
  • 1
  • 1
aran
  • 10,978
  • 5
  • 39
  • 69
  • Yes, but i can't do watched.addObserver(guiB), since guiB is not in the same file where i do GuiEmployee watched = new GuiEmployee(var, var). What i want is that guiB is the one i create at the LogginBoss.java. – user2271933 May 15 '13 at 10:28
  • Why don't you simply create GuiEmployee inside GuiBoss? This way you'll have watched/guiB instance for whatever you want. The other option would be creating a method "setGuiE (GuiEmployee g)" inside GuiBoss. – aran May 15 '13 at 10:29