Facade Design Pattern

A Design Pattern is a documented and prooven design solution, which can be used to solve commonly occurring Design Problems.

A Facade Design Pattern is providing Simple, Single Unified interface to a set of classes. Below is an example of the same.

As shown in below diagram, an Application is directly dependent on Display, FileSystem, Networking etc….we are assuming each of these services are provided by corresponding class. Without using Facade Pattern, Application is directly dependent on the classes.

But by re designing above solution by using facade Pattern, Application is not directly dependent on each of individual classes, Hence any change in the public methods exposed by the individual class or change of class, may not directly impact Application. If any changes in the individual classes, changes need to be performed in the UnifiedAPI, and hence Application need not be changed.

Facade Pattern
Facade Pattern



class FileSystem{

   public void readFile() {
      System.out.println("readFile() in FileSystem");
   }
}

class Network {

   public void sendData() {
      System.out.println("sendData() in Network");
   }
}


class Display {

   public void draw() {
      System.out.println("draw() in Display");
   }
}

class AppLifeCycle {

   public void startApp() {
      System.out.println("startApp() in AppLifeCycle");
   }
}

interface OSAPI{
    public void readFileOS();
    public void sendDataOS();
    public void startAppOS();
    public void drawOS(); 
}

class FacadeClass implements OSAPI{
      public void readFileOS(){
          new FileSystem().readFile();
      }
      
    public void sendDataOS(){
            new Network().sendData();
    }
    
    public void startAppOS(){
        new AppLifeCycle().startApp();
    }
    
    public void drawOS(){
        new Display().draw();
    }
}

public class FacadePattern1 {
   public static void main(String[] args) {
      FacadeClass obj = new FacadeClass();

      obj.drawOS();
      obj.readFileOS();
      obj.sendDataOS();
      obj.startAppOS();
   }
}