0

I am reading observer pattern design and could not understand the logic behind how one class calls the method of another class's method which one is not parent/child of another.

for instance i have the following Company and Employee classes.

public class Company {
    String  name;

    public void hire() {
        System.out.println("company hires employees");
    }
}

class Employee {
    int age;
    String  name;

    public void gotHired() {
        System.out.println("employee got hired by a company");
    }
}

Is there anyway possible ways these classes could take the method of one another so that they communicate. You could add any methods that would be helpful for the explanation. Thank you

Ascalonian
  • 14,409
  • 18
  • 71
  • 103
Tesfa Zelalem
  • 619
  • 1
  • 9
  • 18

3 Answers3

2

one class should contain reference of other class Object to be able to do so

public class Company {
    String  name;
    Employee employee=new Employee();

    public void hire() {
        System.out.println("company hires employees");
    }
}

In this way Company class object can use Employee class methods

good luck!

Vihar
  • 3,626
  • 2
  • 24
  • 47
  • This is true, but it has nothing to do with the *Observer pattern*. – Giulio Biagini Jan 15 '15 at 14:17
  • Yaa I pretty much know what observer pattern is which includes using methods of super class/interfaces on sub class objects , but spoon feeding will take the question asker know where, we are here to guide them, diving into their own problems will make them learn and progress, hope it makes some sense, so I just pasted the pointers which the asker can take forward – Vihar Jan 15 '15 at 15:55
1

Yes. They can communicate. A company can have several employees.So,

public class Company {
    String  name;
    List<Employee> empList = new ArrayList<Employee>();  // list of employees belonging to the company. "Composition" 

    public void hire() {
        System.out.println("company hires employees");
    }
    public void addEmployee(Employee e){ // add employee to company. I just got Hired!!!
    empList.add(e);
    }

    String getFirstEmployee(){ // get first employee
     if(!empList.isEmpty){
     Employee e = empList.get(0);
      return e
    }
   }




}

class Employee {
    int age;
    String  name;

    public void gotHired() {
        System.out.println("employee got hired by a company");
    }
}
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

This is an Observer implementation. First of all the interface:

public interface Observer
{
    public void notifyMe();
}

Then who want to be notified:

public class Player implements Observer
{
    @Override public void notifyMe() {
        /* do something */
    }
}

public class Judge implements Observer
{
    @Override public void notifyMe() {
        /* do something */
    }
}

Then who is responsible of notifying:

public class Game
{
    private ArrayList<Observer> listOfObserver;


    public Game() {
        listOfObserver = new ArrayList<>();
    }


    public void registerObserver(Observer observer) {
        listOfObserver.add(observer);
    }

    public void startGame() {
        ...
        ...
        ...

        /* when something important happens */
        for (int i = 0; i < listOfObserver.size(); i++)
            listOfObserver.notify();

        ...
        ...
        ...
    }
}

And then, the main() function:

public class Main
{
    public static void main(String args[]) {
        Player player1 = new Player();
        Player player2 = new Player();
        Player player3 = new Player();
        Player player4 = new Player();

        Judge judge = new Judge();

        Game game = new Game();
        game.registerObserver(player1);
        game.registerObserver(player2);
        game.registerObserver(player3);
        game.registerObserver(player4);
        game.registerObserver(judge);

        game.startGame();
    }
}
Giulio Biagini
  • 935
  • 5
  • 8