DEV Community

eidher
eidher

Posted on • Edited on

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

Top comments (0)