Thursday, June 16, 2022

Design Pattern: Mediator Pattern

Chapters

Mediator Pattern

Mediator Pattern is a design pattern that encapsulates interactions between objects. This pattern promotes loose coupling between objects and their interactions. Thus, making our code more flexible and maintainable.

This example demonstrates mediator pattern.
public class ClientCode{

  public static void main(String[] args){
    BookShelf bookShelf1 = 
    new RoomBookShelf(new String[]{"1", "2", "3"});
    BookShelf bookShelf2 = 
    new RoomBookShelf(new String[]{"A", "B", "C"});
    
    Mediator mediator = 
    new BookShelfInteractions(bookShelf1, bookShelf2);
    
    System.out.println("Shelf1: " + bookShelf1.getBook(0));
    System.out.println("Shelf2: " + bookShelf2.getBook(2));
    mediator.swapBooks("1", "C");
    System.out.println("After Swap...");
    System.out.println("Shelf1: " + bookShelf1.getBook(0));
    System.out.println("Shelf2: " + bookShelf2.getBook(2));
  }
}

/*
Assume classes below are in different package
*/

interface Mediator{
  void swapBooks(String bookInShelf, 
                 String bookInAnotherShelf);
}

class BookShelfInteractions implements Mediator{
  private BookShelf bookShelf1, bookShelf2;
  
  BookShelfInteractions(BookShelf bookShelf1,
                        BookShelf bookShelf2){
    this.bookShelf1 = bookShelf1;
    this.bookShelf2 = bookShelf2;
  }
  
  @Override
  public void swapBooks(String bookInShelf1, 
                        String bookInShelf2){
    boolean bookIsInShelf1 = false;
    boolean bookIsInShelf2 = false;
    
    String[] shelf1 = 
    bookShelf1.getBookShelf();
    String[] shelf2 = 
    bookShelf2.getBookShelf();
    
    int bookShelf1Index = 0;
    int bookShelf2Index = 0;
    for(int i = 0; i < shelf1.length; i++)
      if(shelf1[i].equals(bookInShelf1)){
        bookIsInShelf1 = true;
        bookShelf1Index = i;
        break;
      }
    if(!bookIsInShelf1){
      System.out.println
      ("Book " + bookInShelf1 + 
       "Doesn't exist!");
       return;
    }
    
    for(int i = 0; i < shelf2.length; i++)
      if(shelf2[i].equals(bookInShelf2)){
        bookIsInShelf2 = true;
        bookShelf2Index = i;
        break;
      }
    if(!bookIsInShelf2){
      System.out.println
      ("Book " + bookInShelf2 + 
       "Doesn't exist!");
       return;
    }
    
    String tempShelf = shelf2[bookShelf2Index];
    shelf2[bookShelf2Index] = 
    shelf1[bookShelf1Index];
    shelf1[bookShelf1Index] = tempShelf;
    System.out.println("Books have been swapped!");
  }
  
}

abstract class BookShelf{
  private String[] books;
  
  BookShelf(String[] books){
    this.books = books;
  }
  
  public String getBook(int index){
    if(index < 0 || index >= books.length)
      throw new ArrayIndexOutOfBoundsException();
    
    return books[index];
  }
  
  String[] getBookShelf(){
    return books;
  }
}

class RoomBookShelf extends BookShelf{
  
  RoomBookShelf(String[] books){
    super(books);
  }
  
}

Result
Shelf1: 1
Shelf2: C
Books have been swapped!
After Swap...
Shelf1: C
Shelf2: 1

No comments:

Post a Comment