theateist
theateist

Reputation: 14399

What is the correct way to interact between views in MVC in Java?

I have 2 JPanel: Panel_S, Panel_P. The 1st listens to Station events and the 2nd listens to Passenger events. When new passenger is created it is added to JList in Panel_P. When passenger finished "going" to station by goToStation and getting in line by station.addPassenger(this); the station's event fireAddPassengerEvent is fired and the passenger is added to JList in Panel_S. Now, I need some how to remove the passenger from JList in Panel_P because it finished "going" and entered the queue in station. What is the correct way to implement this? Does Panel_S should "tell" Panel_P to remove the passenger from its list once Panel_S adds passenger to its list?

If you didn't understood my question please tell me and I'll try to explain myself better.

public class Passenger extends Thread {
    private Station station;
    private Vector<PassengerEventsViewListener> viewListeners;

    public Passenger(Station station)
    {
        this.station = station;
    }
    private void getInLineInStation()
    {
        station.addPassenger(this);
    }
    private void fireArrivedToStationEvent(EventArgs<Passenger> args)
    {       
       for (PassengerEventsViewListener l : viewListeners) {
          l.arrivedToStationEvent(args);
       }
    }
    private void goToStation()
    {
        //going
        fireArrivedToStationEvent(this);
    }

    @Override
    public void run()
    {
       goToStation();
       getInLineInStation();
    }
}

public class Station {
     private Vector<StationEventsViewListener> viewListeners;

     private void fireAddPassengerEvent(Passenger passenger)
     {      
         for (StationEventsViewListener l : viewListeners) {
             l.addPassengerEvent(passenger);
         } 
     }
     public synchronized boolean addPassenger(Passenger p)
     {
          passengersInQueue.addLast(passenger);     
          fireAddPassengerEvent(passenger);
     }
}

Upvotes: 1

Views: 123

Answers (2)

GETah
GETah

Reputation: 21419

No, your views should not communicate and should not be coupled. Only the models talk and see each other. Each model's view changes accordingly.

Back to your problem. Panel_P should be linked to a model that contains information about passengers. In the other hand, Panel_S should be linked to a model that contains information about stations. Now when a new passenger arrives to a station, the passenger model should notify the station model of the new arrival. Panel_S can then be updated from station model without caring about passenger model nor its view (Panel_P)

Upvotes: 2

Guillaume Polet
Guillaume Polet

Reputation: 47608

Does Panel_S should ''tell'' Panel_P to remove the passenger from its list once Panel_S adds passenger to its list?

In MVC: no. This is typically what MVC tries to avoid: coupling views together. Like always in MVC, the solution is to make the model notify this information. Upon notification, the Panel_P will automatically remove the passenger from its list.

Upvotes: 2

Related Questions