DEV Community

eidher
eidher

Posted on • Edited on

2

Facade Pattern

Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.

Alt Text

Participants

  • Facade: knows which subsystem classes are responsible for a request. Delegates client requests to appropriate subsystem objects.
  • Subsystem classes: implement subsystem functionality. Handle work assigned by the Facade object. Have no knowledge of the facade and keep no reference to it.

Code

public class Main {

    public static void main(String[] args) {
        Facade facade = new Facade();
        facade.methodA();
        facade.methodB();
    }
}

public class SubSystemOne {
    public void methodOne() {
        System.out.println(" SubSystemOne Method");
    }
}

public class SubSystemTwo {
    public void methodTwo() {
        System.out.println(" SubSystemTwo Method");
    }
}

public class SubSystemThree {
    public void methodThree() {
        System.out.println(" SubSystemThree Method");
    }
}

public class SubSystemFour {
    public void methodFour() {
        System.out.println(" SubSystemFour Method");
    }
}

public class Facade {

    private SubSystemOne one;
    private SubSystemTwo two;
    private SubSystemThree three;
    private SubSystemFour four;

    public Facade() {
        one = new SubSystemOne();
        two = new SubSystemTwo();
        three = new SubSystemThree();
        four = new SubSystemFour();
    }

    public void methodA() {
        System.out.println("\nmethodA() ---- ");
        one.methodOne();
        two.methodTwo();
        four.methodFour();
    }

    public void methodB() {
        System.out.println("\nmethodB() ---- ");
        two.methodTwo();
        three.methodThree();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

methodA() ---- 
 SubSystemOne Method
 SubSystemTwo Method
 SubSystemFour Method

methodB() ---- 
 SubSystemTwo Method
 SubSystemThree Method
Enter fullscreen mode Exit fullscreen mode

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay