public class Thermostat {
public void setTemperature(int temperature) {
System.out.println("Setting thermostat to " + temperature + " degrees.");
}
}
public class Lights {
public void on() {
System.out.println("Lights are on.");
}
public void off() {
System.out.println("Lights are off.");
}
}
public class CoffeeMaker {
public void brewCoffee() {
System.out.println("Brewing coffee.");
}
}
public class SmartHomeFacade {
private Thermostat thermostat;
private Lights lights;
private CoffeeMaker coffeeMaker;
public SmartHomeFacade(
Thermostat thermostat, Lights lights, CoffeeMaker coffeeMaker
) {
this.thermostat = thermostat;
this.lights = lights;
this.coffeeMaker = coffeeMaker;
}
public void wakeUp() {
System.out.println("Waking up...");
thermostat.setTemperature(22);
lights.on();
coffeeMaker.brewCoffee();
}
public void leaveHome() {
System.out.println("Leaving home...");
thermostat.setTemperature(18);
lights.off();
}
}
public class Main {
public static void main(String[] args) {
Thermostat thermostat = new Thermostat();
Lights lights = new Lights();
CoffeeMaker coffeeMaker = new CoffeeMaker();
SmartHomeFacade smartHome
= new SmartHomeFacade(thermostat, lights, coffeeMaker);
smartHome.wakeUp();
smartHome.leaveHome();
}
}
Waking up...
Setting thermostat to 22 degrees.
Lights are on.
Brewing coffee.
Leaving home...
Setting thermostat to 18 degrees.
Lights are off.
class FileReader {
public String readFile(String filePath)
throws IOException {
return new String(Files.readAllBytes(Paths.get(filePath)));
}
}
class FileWriter {
public void writeFile(String filePath, String content)
throws IOException {
Files.write(Paths.get(filePath), content.getBytes());
}
}
class FileDeleter {
public void deleteFile(String filePath)
throws IOException {
Files.delete(Paths.get(filePath));
}
}
class FileSystemFacade {
private FileReader fileReader;
private FileWriter fileWriter;
private FileDeleter fileDeleter;
public FileSystemFacade() {
this.fileReader = new FileReader();
this.fileWriter = new FileWriter();
this.fileDeleter = new FileDeleter();
}
public String readFile(String filePath) {
try {
return fileReader.readFile(filePath);
} catch (IOException e) {
System.err.println("Error reading file: " + e.getMessage());
return null;
}
}
public boolean writeFile(String filePath, String content) {
try {
fileWriter.writeFile(filePath, content);
return true;
} catch (IOException e) {
System.err.println("Error writing file: " + e.getMessage());
return false;
}
}
public boolean deleteFile(String filePath) {
try {
fileDeleter.deleteFile(filePath);
return true;
} catch (IOException e) {
System.err.println("Error deleting file: " + e.getMessage());
return false;
}
}
}
// Client code demonstrating the use of FileSystemFacade
public class Main {
public static void main(String[] args) {
FileSystemFacade fs = new FileSystemFacade();
// Write to file
boolean writeSuccess = fs.writeFile(
"test.txt", "Hello, Facade Pattern!"
);
System.out.println("File write success: " + writeSuccess);
// Read from file
String content = fs.readFile("test.txt");
System.out.println("File content: " + content);
// Delete file
boolean deleteSuccess = fs.deleteFile("test.txt");
System.out.println(
"File delete success: " + deleteSuccess
);
}
}
File write success: true
File content: Hello, Facade Pattern!
File delete success: true