Facade pattern is one type of structural pattern, which hides the complexity of the system and provides an interface for clients to access the system. Every abstract factory is a facade pattern by default. Client can easily interact with the system applications.
Advantages
Complexity is hidden.
Sub systems can be easily accessed.
Allows loose coupling.
Conceptual
figure
Facade: Facade is used to hide the implementation details of code.
Subsystem: Subsystems contain code.
The clients can interact with facade but cannot interact with the subsystem.
Examples
Creating an interface Design.
[java]
public interface Design
{
void draw();
}[/java]
Creating a class Triangle which implements the interface Design.
[java]
public class Triangle implements Design
{
public void draw()
{
System.out.println("Triangle::draw()");
}
}[/java]
Creating a class Rhombus and implementing the interface Design.
[java]
public class Rhombus implements Design
{
public void draw()
{
System.out.println("Rhombus::draw()");
}
}[/java]
Creating a class Circle which implements the interface Design.
[java]
public class Circle implements Design
{
public void draw()
{
System.out.println("Circle::draw()");
}
}[/java]
Creating a Facade class DesignMaker.
[java]
public class DesignMaker
{
private Design circle;
private Design triangle;
private Design Rhombus;
public DesignMaker()
{
circle = new Circle();
triangle = new Triangle();
rhombus = new Rhombus();
}
public void drawCircle()
{
circle.draw();
}
public void drawTriangle()
{
rectangle.draw();
}
public void drawRhombus()
{
square.draw();
}
}[/java]
Creating a main class FacadePattern.
[java]
public class FacadePattern
{
public static void main(String[] args)
{
DesignMaker designMaker = new DesignMaker();
designMaker.drawCircle();
designMaker.drawTriangle();
designMaker.drawRhombus();
}
}[/java]
Output
Result will be as follows.
[java]
Circle::draw()
Triangle::draw()
Rhombus::draw()[/java]
Summary
Key Points
Façade Pattern - Complexity is reduced.
Façade Pattern - Interface is designed to provide access to the client.