DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • Architecture and Code Design, Pt. 2: Polyglot Persistence Insights To Use Today and in the Upcoming Years
  • Double-Checked Locking Design Pattern in Java
  • Messaging Design Pattern (MDP) In Java

Trending

  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • Docker Base Images Demystified: A Practical Guide
  • A Simple, Convenience Package for the Azure Cosmos DB Go SDK
  • Unlocking the Potential of Apache Iceberg: A Comprehensive Analysis
  1. DZone
  2. Coding
  3. Java
  4. Facade Design Pattern In Java

Facade Design Pattern In Java

By 
Brijesh Saxena user avatar
Brijesh Saxena
DZone Core CORE ·
Oct. 06, 20 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
11.2K Views

Join the DZone community and get the full member experience.

Join For Free

Here I am with another article on design patterns — Facade Design Pattern. A Facade object is use to provide a simple interface by hiding complexities of a complex system.

Facade Design Pattern

  • The Facade is a Structural Design Pattern and one of the Gang of Four design patterns. 
  • The Facade object is used to provide a front-facing interface by masking a more complex underlying system.
  • The Facade may provide a limited or dedicated set of functionalities. But, the functionalities Facade provides are mainly required by the client application. So, its more caring as per client needs.
  • The primary purpose of the Facade is to hide complexities of a system/subsystem by providing simpler interface to deal with.
  • Using Facade is super-easy when we have to deal with a complex system/subsystem having lots of functionalities and different configurations.
  • So, Facade hides minor and inner details of any third party library, system or subsystem we should know before we deal with it. 
  • In Java there are many features like JDBC, JPA, JAX-RS etc. which hides the minor details and provide a simpler interface in form of annotations or easier configuration to deal with.
  • Even our computer system's POST (power-on-self-test) procedure which runs at the time we start our system; is a good example of Facade. it checks RAM, CPU, HDD and other connected peripherals before giving control over to operating system.
  • Facade introduces additional layer of abstraction via Facade. So, if the sub-system changes, we need to do corresponding changes in the facade layer as well.

facade design pattern UML diagram

  • We may also have multiple Facade objects one dealing with few subsystems and other dealing with some other subsystems.

facade design pattern UML diagram

I hope we are now clear about what is Facade? To understand it more clearly and the use of Facade in our code, let's take an example of Home Appliance we normally have in our home.

Home Appliance Application using Facade Design Pattern

For easier understanding of usage of Facade, I am here using the sample example application code what used in the Command Design Pattern. I only did some required changes and added the code for additional appliances and functionalities to make example more clear and interesting. 

The example also help you to compare Facade with Command Design Pattern.

Code for Appliance class:

Java
xxxxxxxxxx
1
46
 
1
package org.trishinfotech.facade.devices;
2

          
3
public abstract class Appliance implements Comparable<Appliance> {
4

          
5
    protected String name;
6
    protected boolean status;
7
    
8
    public Appliance(String name) {
9
        super();
10
        if (name == null || name.trim().isEmpty()) {
11
            new IllegalArgumentException("Appliance name is mandatory for Home Automation");
12
        }
13
        this.name = name;
14
    }
15

          
16
    public String name() {
17
        return name;
18
    }
19
    
20
    // define operations for appliance
21
    public void on() {
22
        if (status) {
23
            System.out.printf("'%s' is already turned on!\n", name);
24
        } else {
25
            status = true;
26
            System.out.printf("Turning On '%s'\n", name);
27
        }
28
    }
29
    
30
    public void off() {
31
        if (!status) {
32
            System.out.printf("'%s' is already turned off!\n", name);
33
        } else {
34
            status = false;
35
            System.out.printf("Turning Off '%s'\n", name);
36
        }
37
    }
38

          
39
    // Appliance should be compared only on name.
40
    @Override
41
    public int compareTo(Appliance other) {
42
        return this.name.compareToIgnoreCase(other.name);
43
    }
44
    
45
}
46

          


I have defined common operations like 'On' and 'Off' here.

Code for Fan class:

Java
xxxxxxxxxx
1
34
 
1
package org.trishinfotech.facade.devices;
2

          
3
public abstract class Fan extends Appliance {
4

          
5
    public static int TOP_SPEED = 4;
6
    
7
    public static int LOWEST_SPEED = 1;
8
    
9
    protected int currentSpeed = 1;
10
    
11
    public Fan(String name) {
12
        super(name);
13
    }
14
    
15
    // define operations for fan
16
    public void increase() {
17
        if (currentSpeed < TOP_SPEED) {
18
            currentSpeed++;
19
            System.out.printf("Encreasing Speed of '%s' to '%d'.\n", name, currentSpeed);
20
        } else {
21
            System.out.printf("'%s' is already running at top speed!\n", name);
22
        }
23
    }
24
    
25
    public void decrease() {
26
        if (currentSpeed > LOWEST_SPEED) {
27
            currentSpeed--;
28
            System.out.printf("Decreasing Speed of '%s' to '%d'.\n", name, currentSpeed);
29
        } else {
30
            System.out.printf("'%s' is laready running at lowest speed!\n", name);
31
        }
32
    }
33
}
34

          


I have added operations like 'Increase' and 'Decrease' speed here and its a sub-type of Appliance.

Code for Light class:

Java
xxxxxxxxxx
1
10
 
1
package org.trishinfotech.facade.devices;
2

          
3
public abstract class Light extends Appliance {
4

          
5
    public Light(String name) {
6
        super(name);
7
    }
8

          
9
}
10

          


No additional operations since we already have 'On' and 'Off' defined.

Code for SoundBar class:

Java
xxxxxxxxxx
1
66
 
1
package org.trishinfotech.facade.devices;
2

          
3
public abstract class SoundBar extends Appliance {
4

          
5
    public static int TOP_VOLUME = 30;
6

          
7
    public static int LOWEST_VOLUME = 0;
8

          
9
    protected String soundMode;
10
    protected int currentVolume = 1;
11
    protected int volumeWhenMute;
12
    
13
    public SoundBar(String name) {
14
        super(name);
15
    }
16

          
17
    // define operations for SoundBar
18
    public void setSoundMode(String soundMode) {
19
        this.soundMode = soundMode;
20
        System.out.printf("Setting Sound-Mode of '%s' to '%s'.\n", name, soundMode);
21
    }
22

          
23
    public void increaseVolume() {
24
        if (currentVolume < TOP_VOLUME) {
25
            currentVolume++;
26
            System.out.printf("Encreasing volume of '%s' to '%d'.\n", name, currentVolume);
27
        } else {
28
            System.out.printf("'%s' is already on top volume!\n", name);
29
        }
30
    }
31

          
32
    public void decreaseVolume() {
33
        if (currentVolume > LOWEST_VOLUME) {
34
            currentVolume--;
35
            System.out.printf("Decreasing volume of '%s' to '%d'.\n", name, currentVolume);
36
        } else {
37
            System.out.printf("'%s' is already on mute!\n", name);
38
        }
39
    }
40

          
41
    public void volume(int volume) {
42
        if (volume >= LOWEST_VOLUME && volume <= TOP_VOLUME) {
43
            currentVolume = volume;
44
            System.out.printf("Setting volume of '%s' to '%d'.\n", name, currentVolume);
45
        } else {
46
            System.out.printf("Volume of '%s' is supports range between '%d' and '%d'!\n", name, LOWEST_VOLUME,
47
                    TOP_VOLUME);
48
        }
49
    }
50

          
51
    public void mute() {
52
        if (currentVolume != LOWEST_VOLUME) {
53
            volumeWhenMute = currentVolume;
54
            currentVolume = 0;
55
            System.out.printf("Putting '%s' on mute!\n", name);
56
        } else {
57
            currentVolume = volumeWhenMute;
58
            System.out.printf("Unmuting '%s'. Setting volume back to '%d'!\n", name, currentVolume);
59
        }
60
    }
61
    
62
    public String soundMode() {
63
        return soundMode;
64
    }
65
}
66

          


Here, I have added code for operations like 'Sound-Mode' and 'Increase', 'Decrease' , 'Setting' volume and 'Mute'.

Code for TV class:

Java
xxxxxxxxxx
1
69
 
1
package org.trishinfotech.facade.devices;
2

          
3
public abstract class TV extends Appliance {
4

          
5
    public static int TOP_VOLUME = 30;
6

          
7
    public static int LOWEST_VOLUME = 0;
8

          
9
    public static int TOP_CHANNEL_NO = 999;
10

          
11
    public static int LOWEST_CHANNEL_NO = 1;
12

          
13
    protected int currentVolume = 1;
14
    protected int currentChannel = 1;
15
    protected int volumeWhenMute;
16

          
17
    public TV(String name) {
18
        super(name);
19
    }
20

          
21
    // define operations for TV
22
    public void increaseVolume() {
23
        if (currentVolume < TOP_VOLUME) {
24
            currentVolume++;
25
            System.out.printf("Encreasing volume of '%s' to '%d'.\n", name, currentVolume);
26
        } else {
27
            System.out.printf("'%s' is already on top volume!\n", name);
28
        }
29
    }
30

          
31
    public void decreaseVolume() {
32
        if (currentVolume > LOWEST_VOLUME) {
33
            currentVolume--;
34
            System.out.printf("Decreasing volume of '%s' to '%d'.\n", name, currentVolume);
35
        } else {
36
            System.out.printf("'%s' is already on mute!\n", name);
37
        }
38
    }
39

          
40
    public void mute() {
41
        if (currentVolume != LOWEST_VOLUME) {
42
            volumeWhenMute = currentVolume;
43
            currentVolume = 0;
44
            System.out.printf("Putting '%s' on mute!\n", name);
45
        } else {
46
            currentVolume = volumeWhenMute;
47
            System.out.printf("Unmuting '%s'. Setting volume back to '%d'!\n", name, currentVolume);
48
        }
49
    }
50

          
51
    public void increaseChannel() {
52
        if (currentChannel < TOP_CHANNEL_NO) {
53
            currentChannel++;
54
            System.out.printf("Encreasing channel of '%s' to '%d'.\n", name, currentChannel);
55
        } else {
56
            System.out.printf("'%s' is already showing channel '%d'!\n", name, currentChannel);
57
        }
58
    }
59

          
60
    public void decreaseChannel() {
61
        if (currentChannel > LOWEST_CHANNEL_NO) {
62
            currentChannel--;
63
            System.out.printf("Decreasing channel of '%s' to '%d'.\n", name, currentChannel);
64
        } else {
65
            System.out.printf("'%s' is already showing channel '%d'!\n", name, currentChannel);
66
        }
67
    }
68
}
69

          


I have added operations like 'Increase/Decrease Volume', 'Increase/Decrease Channel'  and 'Sound Mute'.

Code for CoffeeMaker class:

Java
xxxxxxxxxx
1
12
 
1
package org.trishinfotech.facade.devices.kitchen;
2

          
3
import org.trishinfotech.facade.devices.Appliance;
4

          
5
public class CoffeeMaker extends Appliance {
6

          
7
    public CoffeeMaker() {
8
        super("CoffeeMaker");
9
    }
10

          
11
}
12

          


Code for ElectricGrill class:

Java
xxxxxxxxxx
1
22
 
1
package org.trishinfotech.facade.devices.kitchen;
2

          
3
import org.trishinfotech.facade.devices.Appliance;
4

          
5
public class ElectricGrill extends Appliance {
6

          
7
    protected int temp;
8

          
9
    public ElectricGrill() {
10
        super("ElectricGrill");
11
    }
12

          
13
    public void setTemp(int temp) {
14
        this.temp = temp;
15
        System.out.printf("Setting '%s' temprature to '%d'.\n", name, temp);
16
    }
17
    
18
    public int temperature() {
19
        return temp;
20
    }
21
}
22

          


Here, I have added operation to set 'Temperature'.

Code for KitchenLight class:

Java
xxxxxxxxxx
1
12
 
1
package org.trishinfotech.facade.devices.kitchen;
2

          
3
import org.trishinfotech.facade.devices.Light;
4

          
5
public class KitchenLight extends Light {
6

          
7
    public KitchenLight() {
8
        super("KitchenLight");
9
    }
10

          
11
}
12

          


Code for Microwave class:

Java
xxxxxxxxxx
1
45
 
1
package org.trishinfotech.facade.devices.kitchen;
2

          
3
import org.trishinfotech.facade.devices.Appliance;
4

          
5
public class Microwave extends Appliance {
6

          
7
    protected int temp;
8
    protected int time;
9
    protected boolean grillOn = false;
10
    
11
    public Microwave() {
12
        super("Microwave");
13
    }
14

          
15
    public void grillOn() {
16
        this.grillOn = true;
17
        System.out.printf("Turning on grill of '%s'.\n", name);
18
    }
19
    
20
    public void grillOff() {
21
        this.grillOn = false;
22
        System.out.printf("Turning off grill of '%s'.\n", name);
23
    }
24
    
25
    public void setOnPreHeat(int temp, int time) {
26
        this.temp = temp;
27
        this.time = time;
28
        System.out.printf("Setting '%s' on Pre-Heat, temprature '%d', time '%d' minutes.\n", name, temp, time);
29
    }
30
    
31
    public void bake(String pizzaName, int temp, int time) {
32
        this.temp = temp;
33
        this.time = time;
34
        System.out.printf("Baking '%s' in '%s' for temprature '%d', time '%d' minutes.\n", pizzaName, name, temp, time);
35
    }
36
    
37
    public int temp() {
38
        return temp;
39
    }
40
    
41
    public int time() {
42
        return time;
43
    }
44
}
45

          


Here, I have defined operations like 'Grilling On/Off', 'Pre-Heating' and 'Baking'.

Code for Refrigerator class:

Java
xxxxxxxxxx
1
25
 
1
package org.trishinfotech.facade.devices.kitchen;
2

          
3
import org.trishinfotech.facade.devices.Appliance;
4

          
5
public class Refrigerator extends Appliance {
6

          
7
    protected static final String PARTY = "party";
8
    protected static final String NORMAL = "normal";
9
    protected String mode = NORMAL;
10

          
11
    public Refrigerator() {
12
        super("Refrigerator");
13
    }
14

          
15
    public void partyMode() {
16
        mode = PARTY;
17
        System.out.printf("Setting '%s' Cooling to 'Party'.\n", name);
18
    }
19

          
20
    public void normalMode() {
21
        mode = NORMAL;
22
        System.out.printf("Setting '%s' Cooling to 'Normal'.\n", name);
23
    }
24
}
25

          


Here I have defined 'Mode' like 'Party' for fast cooling and 'Normal' for normal cooling.

Code for LivingRoomFan class:

Java
xxxxxxxxxx
1
12
 
1
package org.trishinfotech.facade.devices.livingroom;
2

          
3
import org.trishinfotech.facade.devices.Fan;
4

          
5
public class LivingRoomFan extends Fan {
6

          
7
    public LivingRoomFan() {
8
        super("LivingRoomFan");
9
    }
10

          
11
}
12

          


Code for LivingRoomFireTV4KStick class:

Java
xxxxxxxxxx
1
48
 
1
package org.trishinfotech.facade.devices.livingroom;
2

          
3
import org.trishinfotech.facade.devices.Appliance;
4
import org.trishinfotech.facade.devices.TV;
5

          
6
public class LivingRoomFireTV4KStick extends Appliance {
7

          
8
    protected TV tv;
9
    protected String appName;
10
    protected String contentName;
11

          
12
    public LivingRoomFireTV4KStick(TV tv) {
13
        super("LivingRoomFireTV4KStick");
14
        this.tv = tv;
15
    }
16

          
17
    // define operations for Fire TV Stick 4K
18
    public void openApp(String appName) {
19
        this.appName = appName;
20
        System.out.printf("Opening '%s' on '%s'.\n", appName, name);
21
    }
22

          
23
    public void selectContent(String contentName) {
24
        this.contentName = contentName;
25
        System.out.printf("Searching '%s' on '%s'.\n", contentName, appName);
26
    }
27

          
28
    public void play() {
29
        System.out.printf("Playing '%s' on '%s'.\n", contentName, appName);
30
    }
31

          
32
    public void closeApp() {
33
        System.out.printf("Closing '%s' on '%s'.\n", appName, name);
34
    }
35
    
36
    public TV tv() {
37
        return tv;
38
    }
39

          
40
    public String appName() {
41
        return appName;
42
    }
43
    
44
    public String contentName() {
45
        return contentName;
46
    }
47
}
48

          


Here, I have added operations like 'Open App', 'Close App', 'Search Content' and 'Play'.

Code for LivingRoomLight class:

Java
xxxxxxxxxx
1
24
 
1
package org.trishinfotech.facade.devices.livingroom;
2

          
3
import org.trishinfotech.facade.devices.Light;
4

          
5
public class LivingRoomLight extends Light {
6

          
7
    protected int brightness = 50;
8
    
9
    public LivingRoomLight() {
10
        super("LivingRoomLight");
11
    }
12

          
13
    public void dim() {
14
        brightness = 20;
15
        System.out.printf("Dimming '%s'.\n", name);
16
    }
17

          
18
    public void bright() {
19
        brightness = 100;
20
        System.out.printf("Setting brightness of '%s' to '%d'.\n", name, brightness);
21
    }
22

          
23
}


Here, I have defined operations to control light brightness by using 'dim' and 'bright'.

Code for LivingRoomSoundBar class: 

Java
xxxxxxxxxx
1
20
 
1
package org.trishinfotech.facade.devices.livingroom;
2

          
3
import org.trishinfotech.facade.devices.SoundBar;
4
import org.trishinfotech.facade.devices.TV;
5

          
6
public class LivingRoomSoundBar extends SoundBar {
7

          
8
    protected TV tv;
9

          
10
    public LivingRoomSoundBar(TV tv) {
11
        super("LivingRoomSoundBar");
12
        this.tv = tv;
13
    }
14

          
15
    public TV tv() {
16
        return tv;
17
    }
18
    
19
}


Code for LivingRoomTV class:

Java
xxxxxxxxxx
1
22
 
1
package org.trishinfotech.facade.devices.livingroom;
2

          
3
import org.trishinfotech.facade.devices.TV;
4

          
5
public class LivingRoomTV extends TV {
6

          
7
    protected String source;
8

          
9
    public LivingRoomTV() {
10
        super("LivingRoomTV");
11
    }
12

          
13
    public void setSource(String source) {
14
        this.source = source;
15
        System.out.printf("Setting Source of '%s' to '%s'.\n", name, source);
16
    }
17

          
18
    public String source() {
19
        return source;
20
    }
21
}


Now, when all the appliances are defined along with their operations, it's time to work on Facade Design Pattern. Suppose we like to a weekend-party at home with friends and family.  Since we have various appliances at home for entertainment and food, we we write a HomeFacade to define our 'Week-End Home Party' operations.

Code for HomeFacade class:

Java
xxxxxxxxxx
1
93
 
1
package org.trishinfotech.facade;
2

          
3
import java.util.List;
4

          
5
import org.trishinfotech.facade.devices.Fan;
6
import org.trishinfotech.facade.devices.Light;
7
import org.trishinfotech.facade.devices.SoundBar;
8
import org.trishinfotech.facade.devices.TV;
9
import org.trishinfotech.facade.devices.kitchen.CoffeeMaker;
10
import org.trishinfotech.facade.devices.kitchen.ElectricGrill;
11
import org.trishinfotech.facade.devices.kitchen.KitchenLight;
12
import org.trishinfotech.facade.devices.kitchen.Microwave;
13
import org.trishinfotech.facade.devices.kitchen.Refrigerator;
14
import org.trishinfotech.facade.devices.livingroom.LivingRoomFan;
15
import org.trishinfotech.facade.devices.livingroom.LivingRoomFireTV4KStick;
16
import org.trishinfotech.facade.devices.livingroom.LivingRoomLight;
17
import org.trishinfotech.facade.devices.livingroom.LivingRoomSoundBar;
18
import org.trishinfotech.facade.devices.livingroom.LivingRoomTV;
19

          
20
public class HomeFacade {
21

          
22
    Fan fan;
23
    LivingRoomFireTV4KStick stick;
24
    Light livingRoomLight;
25
    SoundBar soundBar;
26
    TV tv;
27
    
28
    CoffeeMaker maker;
29
    ElectricGrill grill;
30
    Light kitchenLight;
31
    Microwave microwave;
32
    Refrigerator refrigerator;
33
    
34
    public HomeFacade() {
35
        super();
36
        fan = new LivingRoomFan();
37
        tv = new LivingRoomTV();
38
        stick = new LivingRoomFireTV4KStick(tv);
39
        livingRoomLight = new LivingRoomLight();
40
        soundBar = new LivingRoomSoundBar(tv);
41
        
42
        maker = new CoffeeMaker();
43
        grill = new ElectricGrill();
44
        kitchenLight = new KitchenLight();
45
        microwave = new Microwave();
46
        refrigerator = new Refrigerator();
47
    }
48

          
49
    public void playMovieOnNetflix(String movieName) {
50
        fan.on();
51
        fan.increase();
52
        livingRoomLight.on();
53
        tv.on();
54
        ((LivingRoomTV)tv).setSource("HDMI ARC");
55
        stick.on();
56
        soundBar.on();
57
        soundBar.setSoundMode("Dolby Atmos");
58
        stick.openApp("Netflix");
59
        stick.selectContent(movieName);
60
        ((LivingRoomLight)livingRoomLight).dim();
61
        soundBar.volume(20);
62
        stick.play();
63
    }
64
    
65
    public void prepareFood(List<String> pizzaNames) {
66
        kitchenLight.on();
67
        // normally refrigerator runs always. so no need to turn on.
68
        refrigerator.partyMode(); // for fast cooling
69
        microwave.on();
70
        microwave.setOnPreHeat(200, 5);
71
        microwave.grillOn();
72
        grill.on();
73
        maker.on();
74
        pizzaNames.forEach(pizzaName -> microwave.bake(pizzaName, 400, 10));
75
    }
76
    
77
    public void stopMovie() {
78
        stick.closeApp();
79
        stick.off();
80
        soundBar.off();
81
        tv.off();
82
        ((LivingRoomLight)livingRoomLight).bright();
83
        fan.off();
84
    }
85
    
86
    public void closeKitchen() {
87
        refrigerator.normalMode();
88
        grill.off();
89
        maker.off();
90
        microwave.off();
91
    }
92
}


Here we have facade-methods to deal with 

  • Setting up Home Entertainment System to play the movie.
  • Preparing Food for family and friends.
  • Shutting-down Home Entertainment System when the movie completes.
  • Closing the Kitchen Appliances post our Home-Party.

Now, its time to write our Main application to execute our HomeFacade and test the output:

Java
xxxxxxxxxx
1
31
 
1
package org.trishinfotech.facade;
2

          
3
import java.util.Arrays;
4

          
5
public class Main {
6

          
7
    public static void main(String[] args) {
8
        HomeFacade home = new HomeFacade();
9
        System.out.println("Weekend: Enjoying with friends and family at home...");
10
        System.out.println("-----------------------------------------------------------------");
11
        System.out.println("Setting up movie...");
12
        home.playMovieOnNetflix("Spider-Man: Far From Home");
13
        System.out.println("-----------------------------------------------------------------");
14
        System.out.println("Preparing food...");
15
        home.prepareFood(Arrays.asList("Napoletana Pizza", "Margherita Pizza", "Marinara Pizza",
16
                "Chicago-Style Deep Dish Pizza"));
17
        System.out.println("-----------------------------------------------------------------");
18
        System.out.println("Enjoy Movie with Meal and Drink...");
19
        System.out.println("Movie Completed.");
20
        System.out.println("-----------------------------------------------------------------");
21
        System.out.println("Stopping Movie...");
22
        home.stopMovie();
23
        System.out.println("-----------------------------------------------------------------");
24
        System.out.println("Closing Kitchen...");
25
        home.closeKitchen();
26
        System.out.println("-----------------------------------------------------------------");
27
        System.out.println("Done!");
28
    }
29
    
30
}


Below is the output of the program:

Plain Text
x
48
 
1
Weekend: Enjoying with friends and family at home...
2
-----------------------------------------------------------------
3
Setting up movie...
4
Turning On 'LivingRoomFan'
5
Encreasing Speed of 'LivingRoomFan' to '2'.
6
Turning On 'LivingRoomLight'
7
Turning On 'LivingRoomTV'
8
Setting Source of 'LivingRoomTV' to 'HDMI ARC'.
9
Turning On 'LivingRoomFireTV4KStick'
10
Turning On 'LivingRoomSoundBar'
11
Setting Sound-Mode of 'LivingRoomSoundBar' to 'Dolby Atmos'.
12
Opening 'Netflix' on 'LivingRoomFireTV4KStick'.
13
Searching 'Spider-Man: Far From Home' on 'Netflix'.
14
Dimming 'LivingRoomLight'.
15
Setting volume of 'LivingRoomSoundBar' to '20'.
16
Playing 'Spider-Man: Far From Home' on 'Netflix'.
17
-----------------------------------------------------------------
18
Preparing food...
19
Turning On 'KitchenLight'
20
Setting 'Refrigerator' Cooling to 'Party'.
21
Turning On 'Microwave'
22
Setting 'Microwave' on Pre-Heat, temprature '200', time '5' minutes.
23
Turning On 'ElectricGrill'
24
Turning On 'CoffeeMaker'
25
Baking 'Napoletana Pizza' in 'Microwave' for temprature '400', time '10' minutes.
26
Baking 'Margherita Pizza' in 'Microwave' for temprature '400', time '10' minutes.
27
Baking 'Marinara Pizza' in 'Microwave' for temprature '400', time '10' minutes.
28
Baking 'Chicago-Style Deep Dish Pizza' in 'Microwave' for temprature '400', time '10' minutes.
29
-----------------------------------------------------------------
30
Enjoy Movie with Meal and Drink...
31
Movie Completed.
32
-----------------------------------------------------------------
33
Stopping Movie...
34
Closing 'Netflix' on 'LivingRoomFireTV4KStick'.
35
Turning Off 'LivingRoomFireTV4KStick'
36
Turning Off 'LivingRoomSoundBar'
37
Turning Off 'LivingRoomTV'
38
Setting brightness of 'LivingRoomLight' to '100'.
39
Turning Off 'LivingRoomFan'
40
-----------------------------------------------------------------
41
Closing Kitchen...
42
Setting 'Refrigerator' Cooling to 'Normal'.
43
Turning Off 'ElectricGrill'
44
Turning Off 'CoffeeMaker'
45
Turning Off 'Microwave'
46
-----------------------------------------------------------------
47
Done!
48

          


That's it!

Source Code can be found here: Facade-Design-Pattern-Sample-Code

FAQs:

  1. Facade Pattern Vs Adapter Pattern
    • Adapter Pattern allows to make incompatible system compatible. So, we fix the compatibility issue of the system with the client application. Without Adapter, we can't use the system (incompatible). Adapter generally works with one object. Read more on Adapter Design Pattern.
    • Facade Pattern simplifies the complexity of the system (compatible but complex). Without Facade, we can still use the system. But it will require knowledge of lots of minor and inner details while we do that. Facade works with entire system.
  2. Facade Pattern Vs Command Pattern
    • Facade Pattern hides internal details and provide a simplified interface.
    • Command Pattern encapsulates actions which are required perform a task (undoable set of actions). Read more on Command Design Pattern.
  3. Facade Pattern Vs Mediator Pattern
    • Facade Pattern defines the simplifies interface to a complex system.
    • Mediator Pattern provides a central communication point between components of a system.
  4. Facade Pattern Vs Flyweight Pattern
    • Flyweight Pattern creates smaller reusable objects for the system.
    • Facade Pattern creates single bigger objects to deal with the entire system.
  5. Facade Pattern Vs Proxy Pattern
    • Proxy Pattern is similar to Facade except, it provides same interface as it's service object to make complex objects interchangeable.
  6. Facade Pattern Vs Abstract Factory
    • Abstract Factory is like Facade except it only handles the creation part of objects of the system/subsystem. 
    • Facade handles system's objects operational part as well.
  7. Facade Pattern Vs Singleton Pattern
    • Facade Object normally we create as Singleton while implement since it serves for its purpose.

I hope this tutorial demonstrates the use of facade design pattern.

Liked the article? Please don't forget to press that like button. Happy coding!

Need more articles, please visit my profile: Brijesh Saxena

Java (programming language) Design Facade pattern

Opinions expressed by DZone contributors are their own.

Related

  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues
  • Architecture and Code Design, Pt. 2: Polyglot Persistence Insights To Use Today and in the Upcoming Years
  • Double-Checked Locking Design Pattern in Java
  • Messaging Design Pattern (MDP) In Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!