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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Structured Logging
  • A Data-Driven Approach to Application Modernization
  • The Role of AI and Programming in the Gaming Industry: A Look Beyond the Tables
  • Security Challenges for Microservice Applications in Multi-Cloud Environments

Trending

  • Structured Logging
  • A Data-Driven Approach to Application Modernization
  • The Role of AI and Programming in the Gaming Industry: A Look Beyond the Tables
  • Security Challenges for Microservice Applications in Multi-Cloud Environments

Facade Design Pattern in Modern C++

To provide unified interface by hiding system complexities.

Vishal Chovatiya user avatar by
Vishal Chovatiya
·
Oct. 05, 20 · Presentation
Like (3)
Save
Tweet
Share
11.00K Views

Join the DZone community and get the full member experience.

Join For Free

The Facade Design Pattern is a Structural Design Pattern used to provide a unified interface to a complex system. It is the same as a facade in building architecture — an object that serves as a front-facing interface, masking a more complex underlying system. A Facade Design Pattern in C++ can:

  • Improve the readability & usability of a software library by masking interaction with more complex components by providing a single simplified API.
  • Provide a context-specific interface to more generic functionality.

Before we move forward, Let me correct the spelling of Facade i.e. "Façade" & it pronounces as "fa;sa;d". A hook or tail added under the letters C called a cedilla used in most of the European languages to indicate a change of pronunciation for that particular letter. We will not go into details of it, otherwise, we would be out of topic.

The code snippets you see throughout this series of articles are simplified not sophisticated. So you often see me not using keywords like override, final, public(while inheritance) just to make code compact & consumable(most of the time) in single standard screen size. I also prefer struct instead of class just to save line by not writing "public:" sometimes and also miss virtual destructor, constructor, copy constructor, prefix std::, deleting dynamic memory, intentionally. I also consider myself a pragmatic person who wants to convey an idea in the simplest way possible rather than the standard way or using Jargons.

Note:

  •   If you stumbled here directly, then I would suggest you go through What is design pattern? first, even if it is trivial. I believe it will encourage you to explore more on this topic.
  •   All of this code you encounter in this series of articles are compiled using C++20(though I have used Modern C++ features up to C++17 in most cases). So if you don't have access to the latest compiler you can use https://wandbox.org/ which has preinstalled boost library as well.

Intent

To provide unified interface by hiding system complexities.

This is by far the simplest & easiest design pattern I have ever come across.

In other words, the Facade Design Pattern is all about providing a simple & easy to understand interface over a large and sophisticated body of code.

Facade Design Pattern Example in C++

Imagine you set up a smart house where everything is on the remote. So to turn the lights on you push lights on-button -- And same for TV, AC, Alarm, Music, etc...

When you leave a house you would need to push 100 buttons to make sure everything is off & are good to go which could be a little annoying if you are lazy like me.

So, I defined a Facade for leaving and coming back. (Facade functions represent buttons...). So when I come and leave, I just make one call & it takes care of everything...

C++
xxxxxxxxxx
1
40
 
1
struct Alarm {
2
    void alarm_on()    { cout << "Alarm is on and house is secured"<<endl; }
3
    void alarm_off() { cout << "Alarm is off and you can go into the house"<<endl; }
4
};
5
6
struct Ac {
7
    void ac_on()    { cout << "Ac is on"<<endl; }
8
    void ac_off()    { cout << "AC is off"<<endl; }
9
};
10
11
struct Tv {
12
    void tv_on()    { cout << "Tv is on"<<endl; }
13
    void tv_off()    { cout << "TV is off"<<endl; }
14
};
15
16
struct HouseFacade {
17
    void go_to_work() {
18
        m_ac.ac_off();
19
        m_tv.tv_off();
20
        m_alarm.alarm_on();
21
    }
22
    void come_home() {
23
        m_alarm.alarm_off();
24
        m_ac.ac_on();
25
        m_tv.tv_on();
26
    }
27
private:
28
    Alarm   m_alarm;
29
    Ac      m_ac;
30
    Tv      m_tv;
31
};
32
33
int main() {
34
    HouseFacade hf;
35
    //Rather than calling 100 different on and off functions thanks to facade I only have 2 functions...
36
    hf.go_to_work();
37
    hf.come_home();
38
    return EXIT_SUCCESS;
39
}
40
// Stolen from: https://en.wikibooks.org/wiki/C%2B%2B_Programming/Code/Design_Patterns


Note that we have just combined the different none/somewhat-related classes into HouseFacade. We would also be able to use interface with polymorphic turn_on() & turn_off() method with override in respective subclasses, to create a collection of Ac, Tv, Alarm objects to add Composite Design Pattern for more sophistication.

But that will complicate system further & add the learning curve. Which is exactly opposite for what Facade Design Pattern is used in the first place.

Benefits of Facade Design Pattern

Facade defines a higher-level interface that makes the subsystem easier to use by wrapping a complicated subsystem.

This reduces the learning curve necessary to successfully leverage the subsystem.

Summary by FAQs

Is Facade a class which contains a lot of other classes?

Yes. It is a wrapper for many sub-systems in the application.

What makes it a design pattern? For me, it is like a normal class.

All design patterns too are normal classes.

What is the practical use case of the Facade Design Pattern?

A typical application of Facade Design Pattern is console/terminal/command-prompt you find in Linux or Windows is a unified way to access machine functionality provided by OS.

Difference between Adapter & Facade Design Pattern?

Adapter wraps one class and the Facade may represent many classes

Design

Published at DZone with permission of Vishal Chovatiya. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Structured Logging
  • A Data-Driven Approach to Application Modernization
  • The Role of AI and Programming in the Gaming Industry: A Look Beyond the Tables
  • Security Challenges for Microservice Applications in Multi-Cloud Environments

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: