Design Patterns - SPLessons

Façade Pattern

Chapter 17

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Façade Pattern

Façade Pattern

shape Description

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.

shape Advantages

  • Complexity is hidden.
  • Sub systems can be easily accessed.
  • Allows loose coupling.

shape Conceptual
figure

shape Examples

Creating an interface Design.

public interface Design
{
void draw();
}

Creating a class Triangle which implements the interface Design.

public class Triangle implements Design
{
public void draw()
{
System.out.println("Triangle::draw()");
}
}

Creating a class Rhombus and implementing the interface Design.

public class Rhombus implements Design
{
public void draw()
{
System.out.println("Rhombus::draw()");
}
}

Creating a class Circle which implements the interface Design.

public class Circle implements Design
{
public void draw()
{
System.out.println("Circle::draw()");
}
}

Creating a Facade class DesignMaker.

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();
}
}

Creating a main class FacadePattern.

public class FacadePattern
{
public static void main(String[] args)
{
DesignMaker designMaker = new DesignMaker();
designMaker.drawCircle();
designMaker.drawTriangle();
designMaker.drawRhombus();
}
}

shape Output

Result will be as follows.

Circle::draw()
Triangle::draw()
Rhombus::draw()

Summary

shape Key Points

  • Façade Pattern – Complexity is reduced.
  • Façade Pattern – Interface is designed to provide access to the client.
  • Facade objects are singletons.