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
π
βοΈ 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);LoggerFactoryacts as a facade over different logging implementations- DriverManager
Connection conn = DriverManager.getConnection(url);DriverManageracts as a facade for DB drivers
This post is licensed under CC BY 4.0 by the author.
