The mediator design pattern defines an object that encapsulates how a set of objects interact.
The Mediator is a behavioral pattern (like the Observer or the Visitor pattern) because it can change the program’s running behavior.
We are used to see programs that are made made up of a large number of classes. However, as more classes are added to a program, the problem of communication between these classes may become more complex.
Because of this, the maintenance becomes a big problem that we need to solve in this way or another.
Like in many other Design Patterns, The mediator pattern comes to solve the problem. It makes the communication between objects encapsulated with a mediator object.
Objects don’t communicate directly with each other, but instead, they communicate through the mediator.
Mediator pattern implementation in Java
This program illustrate an auction. The Auction Mediator is responsible for adding the buyers, and after each buyer bid a certain amount for the item, the mediator know who won the auction.
Tal Baum was added to the buyers list. Elad Shamailov was added to the buyers list. John Smith was added to the buyers list. Welcome to the auction. Tonight we are selling a vacation to Vegas. please Bid your offers. ----------------------------------------------- Waiting for the buyer's offers... ----------------------------------------------- The auction winner is Elad Shamailov. He paid 2000$ for the item. Elad Shamailov Has canceled his bid!, In that case The auction winner is Tal Baum. He paid 1800$ for the item.
- You can replace one object in the structure with a different one without affecting the classes and the interfaces.
- The Mediator often needs to be very intimate with all the different classes, And it makes it really complex.
- Can make it difficult to maintain.
Author : http://designpattern.co.il/
- Design Patterns | Set 1 (Introduction)
- Design Patterns | Set 2 (Factory Method)
- Command Pattern
- Observer Pattern | Set 1 (Introduction)
- Observer Pattern | Set 2 (Implementation)
- Singleton Design Pattern | Implementation
- Decorator Pattern | Set 1 (Background)
- The Decorator Pattern | Set 2 (Introduction and Design)
- Decorator Pattern | Set 3 (Coding the Design)
- Strategy Pattern | Set 1 (Introduction)
- Strategy Pattern | Set 2 (Implementation)
- Adapter Pattern
- Iterator Pattern
- Curiously recurring template pattern (CRTP)
- Flyweight Design Pattern
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.