Post

Structural_Facade Pattern

βœ… Facade

  • provide a πŸ‘πŸ» simplified, unified interface to a πŸ‘ŽπŸ» complex subsystem
  • define a high level interface
  • that makes the subsystem easier to use

  • πŸ‘ŽπŸ» client interacts with many classes and complicated workflow
  • πŸ‘πŸ» client just interacts with single facade class
  • facade class does all the underlying difficult job, coordiate the sub components

  • πŸ‘€ Like having a remote control in a smart home
  • to control lights, speaker, caldera…

βœ… Diagram

Screenshot-2026-03-06-at-17-35-01.png

πŸ‘€

βœ”οΈ sub types of file

  • has class of file reading, writing and deleting
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class FileReader {
    public String readFile(String filePath){
        //read file function
    }
}

class FileWriter {
    public void writeFile(String filePath, String content){
      //write file function
    }
}

class FileDeleter {
    public void deleteFile(String filePath){
      //delete file function
    }
}

βœ”οΈ Fascade class

  • like a remote control that controls everything related to files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class FileSystemFacade {
    private FileReader fileReader;
    private FileWriter fileWriter;
    private FileDeleter fileDeleter;

    public FileSystemFacade() {
       //constructor
    }

    public String readFile(String filePath) {
      //manage read file
      fileReader.readFile(filePath)
    }

    public boolean writeFile(String filePath, String content) {
        //manage write file
        fileWriter.writeFile(filePath, content);
    }

    public boolean deleteFile(String filePath) {
        //maange delete file
        fileDeleter.deleteFile(filePath);
    }
}

βœ”οΈ Main class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// πŸ‘ŽπŸ» before
// client has to know
FileWriter fileWrite = new FileWriter();
fileWrite.writeFile(); //need to write each subsystem
FileReader fileRead = new FileReader();
fileRead.readFile();

// πŸ‘πŸ» after
// client just has to call FileSystemFacade class
// FileSystemFacade will do everything!

FileSystemFacade fs = new FileSystemFacade();

// Write to file
fs.writeFile();

// Read from file
fs.readFile("test.txt");

// Delete file
fs.deleteFile("test.txt");

πŸ› οΈ

  • Logger Factory
  • Logger logger = LoggerFactory.getLogger(MyClass.class);
  • LoggerFactory acts as a facade over different logging implementations

  • DriverManager
  • Connection conn = DriverManager.getConnection(url);
  • DriverManager acts as a facade for DB drivers
This post is licensed under CC BY 4.0 by the author.