From the course: Java Design Patterns: Behavioral Part 1

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Solution: The Mediator pattern

Solution: The Mediator pattern - Java Tutorial

From the course: Java Design Patterns: Behavioral Part 1

Start my 1-month free trial

Solution: The Mediator pattern

(bold upbeat music) - [Instructor] Let's have a look at my solution to the exercise. So, I've added a class called Mediator and I've got fields in here for the plane, planesInFlights, planesOnGround, and runway. And in the constructor, I'm creating new objects for each of these. Next, I have a takeoff method. All of the logic needed for the plane to take off is encapsulated in this method here. Now, if I take a look at the plane class, I've only got two fields: id and isInTheAir. Finally in the main class, I'm creating a Mediator object, and I'm calling the takeOff method. So if I run this up, I can see it says, "Plane 123 is taking off." So this is how the Mediator pattern can be used to create loose coupling between objects. If I wanted to add more planes, I could just add a new plane object in the Mediator class, and handle all the functionality for taking off for both planes in here.

Contents