Mediator PatternA Mediator Pattern says that "to define an object that encapsulates how a set of objects interact". I will explain the Mediator pattern by considering a problem. When we begin with development, we have a few classes and these classes interact with each other producing results. Now, consider slowly, the logic becomes more complex when functionality increases. Then what happens? We add more classes and they still interact with each other but it gets really difficult to maintain this code now. So, Mediator pattern takes care of this problem. Mediator pattern is used to reduce communication complexity between multiple objects or classes. This pattern provides a mediator class which normally handles all the communications between different classes and supports easy maintainability of the code by loose coupling. Benefits:
Usage:
UML for Mediator Pattern:Participants:
Implementation of Mediator Pattern:Step 1: Create a ApnaChatRoom interface. Step 2: Create a ApnaChatRoomIml class that will implement ApnaChatRoom interface and will also use the number of participants involved in chatting through Participant interface. Step 3: Create a Participant abstract class. Step 4: Create a User1 class that will extend Participant abstract class and will use the ApnaChatRoom interface. Step 5: Create a User2 class that will extend Participant abstract class and will use the ApnaChatRoom interface. Step 6: Create a MediatorPatternDemo class that will use participants involved in chatting. Output: Next TopicMemento Design Pattern |