Developers often use the facade design pattern when a system is very complex or difficult to understand because the system has many interdependent classes or because its source code is unavailable. This pattern hides the complexities of the larger system and provides a simpler interface to the client.
Facade is a part of Gang of Four design pattern and it is categorized under Structural design patterns.
Facade is a structural design pattern that provides a simplified (but limited) interface to a complex system of classes, library or framework. While Facade decreases the overall complexity of the application, it also helps to move unwanted dependencies to one place.
Facade Method is a Structural Design pattern that provides a simpler unified interface to a more complex system. The word Facade means the face of a building or particularly an outer lying interface of a complex system, consists of several sub-systems.
A design pattern is a common way of solving a recurring problem. Classes in all design patterns are just normal classes. What is important is how they are structured and how they work together to solve a given problem in the best possible way.
The Facade design pattern simplifies the interface to a complex system; because it is usually composed of all the classes which make up the subsystems of the complex system.
A Facade shields the user from the complex details of the system and provides them with a simplified view
of it which is easy to use
. It also decouples
the code that uses the system from the details of the subsystems, making it easier to modify the system later.
http://www.dofactory.com/Patterns/PatternFacade.aspx
http://www.blackwasp.co.uk/Facade.aspx
Also, what is important while learning design patterns is to be able to recognize which pattern fits your given problem and then using it appropriately. It is a very common thing to misuse a pattern or trying to fit it to some problem just because you know it. Be aware of those pitfalls while learning\using design patterns.
Wikipedia has a great example of Facade pattern.
/* Complex parts */
class CPU {
public void freeze() { ... }
public void jump(long position) { ... }
public void execute() { ... }
}
class Memory {
public void load(long position, byte[] data) { ... }
}
class HardDrive {
public byte[] read(long lba, int size) { ... }
}
/* Facade */
class ComputerFacade {
private CPU processor;
private Memory ram;
private HardDrive hd;
public ComputerFacade() {
this.processor = new CPU();
this.ram = new Memory();
this.hd = new HardDrive();
}
public void start() {
processor.freeze();
ram.load(BOOT_ADDRESS, hd.read(BOOT_SECTOR, SECTOR_SIZE));
processor.jump(BOOT_ADDRESS);
processor.execute();
}
}
/* Client */
class You {
public static void main(String[] args) {
ComputerFacade computer = new ComputerFacade();
computer.start();
}
}
As explained in the previous answer it provides a simple interface to the consuming client. For example: "watch ESPN" is the intended function. But it involves several steps like:
But the facade will simplify this and just provide "watch ESPN" function to the client.
Facade hides the complexities of the system and provides an interface to the client from where the client can access the system.
public class Inventory {
public String checkInventory(String OrderId) {
return "Inventory checked";
}
}
public class Payment {
public String deductPayment(String orderID) {
return "Payment deducted successfully";
}
}
public class OrderFacade {
private Payment pymt = new Payment();
private Inventory inventry = new Inventory();
public void placeOrder(String orderId) {
String step1 = inventry.checkInventory(orderId);
String step2 = pymt.deductPayment(orderId);
System.out
.println("Following steps completed:" + step1
+ " & " + step2);
}
}
public class Client {
public static void main(String args[]){
OrderFacade orderFacade = new OrderFacade();
orderFacade.placeOrder("OR123456");
System.out.println("Order processing completed");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With