Thursday, June 9, 2022

Design Pattern: Facade Pattern

Chapters

Facade Pattern

facade pattern (also spelled façade) is a software-design pattern commonly used in object-oriented programming. Analogous to a facade in architecture, a facade is an object that serves as a front-facing interface masking more complex underlying or structural code in order to simplify the complexity of the system behind the facade.

To implement this pattern, we need a class that implements an interface and the implementations of the methods of the interface are delegated to the system behind the facade. Although, the interface may perform additional functionality before/after forwarding a request. Take a look at this example.

/*
Client
*/

public class ClientCode{

  public static void main(String[] args){
    RoomInterface ri = 
    new RoomController(new RoomControl());
    
    ri.openThenMark();
    System.out.println();
    ri.unmarkThenClose();
  }
}

/*
Facade
*/

interface RoomInterface{

  void openMarkThenClose();
  void openUnmarkThenClose();
  void openThenMark();
  void openThenUnmark();
  void markThenClose();
  void unmarkThenClose();
}

class RoomController implements RoomInterface{
  RoomControlInterface rci;
  
  RoomController(RoomControlInterface rci){
    this.rci = rci;
  }
  
  @Override
  public void openMarkThenClose(){
    rci.openRoom();
    rci.markRoom();
    rci.closeRoom();
  }
  
  @Override
  public void openUnmarkThenClose(){
    rci.openRoom();
    rci.unmarkRoom();
    rci.closeRoom();
  }
  
  @Override
  public void openThenMark(){
    rci.openRoom();
    rci.markRoom();
  }
  
  @Override
  public void openThenUnmark(){
    rci.openRoom();
    rci.unmarkRoom();
  }
  
  @Override
  public void markThenClose(){
    if(!rci.openRoom()){
      System.out.println("Room is not open.");
      System.out.println
      ("Therefore, it can't be closed.");
      return;
    }
    rci.markRoom();
    rci.closeRoom();
  }
  
  @Override
  public void unmarkThenClose(){
    if(!rci.openRoom()){
      System.out.println("Room is not open.");
      System.out.println
      ("Therefore, it can't be closed.");
      return;
    }
    rci.unmarkRoom();
    rci.closeRoom();
  }
  
}

/*
Complex System
*/

interface RoomControlInterface{
  
  boolean openRoom();
  void markRoom();
  void unmarkRoom();
  void closeRoom();
}

class RoomControl implements RoomControlInterface{
  private boolean isMarked = false;
  private boolean isOpen = false;
  
  @Override
  public boolean openRoom(){

    if(isOpen){
      System.out.println
      ("Room is already open!");
    }
    else{
      System.out.println
      ("Room has been opened!");
      isOpen = true;
    }
    return isOpen;
  }
  
  @Override
  public void markRoom(){
     if(isMarked){
      System.out.println
      ("Room is already marked. "+
       "no need to mark again.");
    }
    else{
      isMarked = true;
      System.out.println
      ("Room has been marked!");
    }
  }
  
  @Override
  public void unmarkRoom(){
    if(!isMarked){
      System.out.println
      ("Room is already unmarked. "+
       "no need to unmark again.");
    }
    else{
      isMarked = false;
      System.out.println
      ("Room has been unmarked!");
    }
  }
  
  @Override
  public void closeRoom(){
    
    if(isOpen){
      System.out.println
      ("Room has been closed!");
      isOpen = false;
    }
  }
}

Result
Room has been opened!
Room has been marked!

Room is already open!
Room has been unmarked!
Room has been closed!

No comments:

Post a Comment