How to Use Facade Pattern in Java


In this example, we will  see how to implement Facade Pattern in Java.

Source Code

package com.beginner.examples;

// flow 1
class Open {
    public void flow1(){
        System.out.println("open");
    }
}

//flow 2
class Run {
    public void flow2(){
        System.out.println("run");
    } 
}

//flow 3
class Stop {
    public void flow3(){
        System.out.println("stop");
    }
}

// facade
class Facade {
    private Open o = null; 
    private Run r = null;
    private Stop s = null;
    
    public void flow(){
        o = new Open(); 
        r = new Run();
        s = new Stop();
        
        o.flow1();
        r.flow2();
        s.flow3();
    }
}

public class FacadeExample {
    public static void main(String[] args) {
        Facade f = new Facade();
        // start the flow
        f.flow();
    }
}

Output:

open
run
stop
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments