Behavioral Patterns  «Prev  Next»
Lesson 13 Course project, part 6
ObjectiveWrite a Mediator Class for the Course Project

Write a Mediator Class for the Course Project

Java Mediator Class for Traffic Flow System

Write a Mediator Class for a "traffic flow system", which will coordinate the intersection of four traffic lights using the Java Programming Language. The class will be responsible for queuing requests and making sure that each light is green for a minimum amount of time.
import java.util.Timer;
import java.util.TimerTask;

public class TrafficFlowMediator {

    // Constants
    private static final long MIN_GREEN_TIME = 10000; // 10 seconds for demonstration; adjust as needed
    
    // Enum to represent traffic light directions
    public enum TrafficDirection {
        NORTH,
        EAST,
        SOUTH,
        WEST;
    }
    
    private TrafficLight northLight;
    private TrafficLight eastLight;
    private TrafficLight southLight;
    private TrafficLight westLight;
    private TrafficDirection currentGreenDirection;
    private Timer timer;

    public TrafficFlowMediator() {
        northLight = new TrafficLight(TrafficDirection.NORTH);
        eastLight = new TrafficLight(TrafficDirection.EAST);
        southLight = new TrafficLight(TrafficDirection.SOUTH);
        westLight = new TrafficLight(TrafficDirection.WEST);
        currentGreenDirection = TrafficDirection.NORTH; // Start with North for simplicity
        timer = new Timer();
        timer.schedule(new TrafficScheduler(), 0, MIN_GREEN_TIME);
    }
    
    private class TrafficScheduler extends TimerTask {
        @Override
        public void run() {
            switchGreen();
        }
    }

    // Utility method to switch off all lights
    private void switchOffAll() {
        northLight.setRed();
        eastLight.setRed();
        southLight.setRed();
        westLight.setRed();
    }

    // Utility method to switch to the next green light
    private synchronized void switchGreen() {
        switchOffAll();

        switch (currentGreenDirection) {
            case NORTH:
                eastLight.setGreen();
                currentGreenDirection = TrafficDirection.EAST;
                break;
            case EAST:
                southLight.setGreen();
                currentGreenDirection = TrafficDirection.SOUTH;
                break;
            case SOUTH:
                westLight.setGreen();
                currentGreenDirection = TrafficDirection.WEST;
                break;
            case WEST:
                northLight.setGreen();
                currentGreenDirection = TrafficDirection.NORTH;
                break;
        }
    }

    // Nested class representing each Traffic Light
    private class TrafficLight {
        private TrafficDirection direction;
        private boolean isGreen;

        public TrafficLight(TrafficDirection direction) {
            this.direction = direction;
            this.isGreen = false;
        }

        public void setGreen() {
            isGreen = true;
            System.out.println(direction + " light is GREEN");
        }

        public void setRed() {
            isGreen = false;
            System.out.println(direction + " light is RED");
        }

        public boolean isGreen() {
            return isGreen;
        }
    }

    public static void main(String[] args) {
        new TrafficFlowMediator(); // Instantiate the traffic system
    }
}

Explanation of Java Mediator Pattern

This TrafficFlowMediator class mediates the operation of four traffic lights positioned North, East, South, and West respectively.
  1. We utilize an internal Timer to ensure each light remains green for at least the specified MIN_GREEN_TIME.
  2. The TrafficLight nested class represents each traffic light, allowing it to be switched between red and green.
  3. The synchronized switchGreen method manages the transition from one green light to the next, ensuring only one light is green at a time.

This structure ensures a coordinated and orderly operation of the intersection, meeting the stipulated requirement.
For this part of the course project, you will be incorporating the Mediator pattern into your traffic flow system. The class that you develop will coordinate the intersection and the four traffic lights. This class will be responsible for queuing requests and making sure that each light is green for a minimum amount of time.

This diagram illustrates how the traffic lights will interfact with the Mediator.
This diagram illustrates how the traffic lights will interfact with the Mediator.

The Mediator design pattern enables decoupling of objects by introducing a layer in between the various objects so that the interaction between objects can occur by means of the layer. In an enterprise application there are a large number of classes, the complexity also increases as the size of the project increases. The business logic might be distributed across several classes and there will be interaction between classes. If we draw a line of interaction between these classes where we have interaction, it will become difficult to decipher what is going on. Before the SW Developer applies the Mediator pattern, there might exist complex interaction between objects, and this creates dependency and tighter coupling. Our objective is to achieve loose coupling and we want to reduce dependency as much as possible while increasing reusability.

Signal Mediator Class - Exercise

In this exercise, you will write a SignalMediator class for the intersection that manages the four lights.
Signal Mediator Class - Exercise