From the course: Complete Guide to Java Design Patterns: Creational, Behavioral, and Structural

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Challenge: The Mediator pattern

Challenge: The Mediator pattern

In this challenge, you'll be updating a traffic light control system to use the mediator pattern. At the moment, there are a couple of classes to look at. The first is this PedestrianCrossingLights class. So this can be two colors, either red or green. Initially, the color field is set to red, and then there are two methods one to set the color to red and the other to set it to green. Then there's the TrafficLight class. A traffic light can be either red, amber, or green. And there are methods to set each of these colors, too. But when the light changes to red or green, it also needs to change the pedestrian light color. For example, when the traffic light is set to red, it sets the color of this light to reds and then it prints out a message saying traffic light is red and then it says pedestrianCrossingLights.changeToGreen. And in the changeToGreen method, for example, it first sets the pedestrian crossing light to red and then it changes the color of this light to green. This does…

Contents