2914. Design Pattern - Facade
Facade Pattern


Structural Pattern: Facade Pattern.

1. Facade Pattern

The Facade pattern hides the complexities of the system and provides an interface to the client using which the client can access the system.

2. Implementation

2.1 Shape

public interface Shape {
    void draw();
}

public class Circle implements Shape {

    @Override
    public void draw() {
        System.out.println("Circle::draw()");
    }
}

public class Rectangle implements Shape {

    @Override
    public void draw() {
        System.out.println("Rectangle::draw()");
    }
}

public class Square implements Shape {

    @Override
    public void draw() {
        System.out.println("Square::draw()");
    }
}

2.2 Facade Class


public class ShapeMaker {
    private Shape circle;
    private Shape rectangle;
    private Shape square;

    public ShapeMaker() {
        circle = new Circle();
        rectangle = new Rectangle();
        square = new Square();
    }

    public void drawCircle(){
        circle.draw();
    }
    public void drawRectangle(){
        rectangle.draw();
    }
    public void drawSquare(){
        square.draw();
    }
}

2.3 Client

public class Client {
    public void run() {
        ShapeMaker shapeMaker = new ShapeMaker();

        shapeMaker.drawCircle();
        shapeMaker.drawRectangle();
        shapeMaker.drawSquare();
    }
}

Output.

Circle::draw()
Rectangle::draw()
Square::draw()

3. Source Files

4. References