0

In a context of a multiple window application using MVC:

  • In the Model layer, in order to implement the Observer pattern, should I use one Subject class and have every other Model class use it in order to notify observers? Or should I create multiple Subjects?
  • In the same way, should I have one controller taking care of every possible action on the Views or maybe having multiple controllers? (In the case of multiple controllers, for example, when the application opens a new window should a Controller be instantiated?)

1 Answers1

0

"Head first design patterns" has an excellent chapter on the observer pattern.

should I use one Subject class and have every other Model class use it in order to notify observers?

Depends on whether the observers all require the same data. If observers require different data, use multiple subject classes

should I have one controller taking care of every possible action on the Views or maybe having multiple controllers

This might not be the answer you expected, but maybe, rather than creating an instance for every controller, pass an interface in the view's constructor. This interface should be implemented by the controller. Ex:

class controller implements foos
{
public static void run();
}


interface foos{
     public static void run();
}


public class view
{
    foos controllerInstance;
    //constructor
    view(foos paramController)
    {
    this.controllerInstance = paramController;
    }
    //later in the code
    controllerInstance.run();
}

If you'd like the run method can pass data using the paramater. Hope this helps :)

(first answer here on SO)

Gabriel Stellini
  • 426
  • 4
  • 13