Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Facade Design Pattern in Modern C++
Vishal Chovatiya
Vishal Chovatiya

Posted on • Originally published atvishalchovatiya.com

     

Facade Design Pattern in Modern C++

Facade Design Pattern is a Structural Design Pattern usedto provide a unified interface to a complex system. It is same as Facade in building architecture, a Facade is 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.
  • Serve as a launching point for a broader refactor of monolithic or tightly-coupled systems in favour of more loosely-coupled code. Frankly speaking, even I don't understand this point. I have just copied this from Wikipedia.

/!\: This article has been originally published on myblog. If you are interested in receiving my latest articles,please sign up to my newsletter.

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 acedilla 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.

By the way, If you haven't check out my other articles on Structural Design Patterns, then here is the list:

  1. Adapter
  2. Bridge
  3. Composite
  4. Decorator
  5. Facade
  6. Flyweight
  7. Proxy

The code snippets you see throughout this series of articles are simplified not sophisticated. So you often see me not using keywords likeoverride,final,public(while inheritance) just to make code compact & consumable(most of the time) in single standard screen size. I also preferstruct instead ofclass just to save line by not writing "public:" sometimes and also missvirtual destructor, constructor,copy constructor, prefixstd::, 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 throughWhat 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 usedModern C++ features up to C++17 in most cases). So if you don't have access to the latest compiler you can usehttps://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 & coming back. (Facade functions represent buttons...). So when I come & leave I just make one call & it takes care of everything...
// Stolen from: https://en.wikibooks.org/wiki/C%2B%2B_Programming/Code/Design_PatternsstructAlarm{voidalarm_on(){cout<<"Alarm is on and house is secured"<<endl;}voidalarm_off(){cout<<"Alarm is off and you can go into the house"<<endl;}};structAc{voidac_on(){cout<<"Ac is on"<<endl;}voidac_off(){cout<<"AC is off"<<endl;}};structTv{voidtv_on(){cout<<"Tv is on"<<endl;}voidtv_off(){cout<<"TV is off"<<endl;}};structHouseFacade{voidgo_to_work(){m_ac.ac_off();m_tv.tv_off();m_alarm.alarm_on();}voidcome_home(){m_alarm.alarm_off();m_ac.ac_on();m_tv.tv_on();}private:Alarmm_alarm;Acm_ac;Tvm_tv;};intmain(){HouseFacadehf;//Rather than calling 100 different on and off functions thanks to facade I only have 2 functions...hf.go_to_work();hf.come_home();returnEXIT_SUCCESS;}
Enter fullscreen modeExit fullscreen mode
  • Note that we have just combined the different none/somewhat-related classes intoHouseFacade. We would also be able to use interface with polymorphicturn_on() &turn_off() method with override in respectivesubclasses, to create a collection ofAc,Tv,Alarm objects to addComposite 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

  1. Facade defines a higher-level interface that makes the subsystem easier to use by wrapping a complicated subsystem.
  2. 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

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
delta456 profile image
Swastik Baranwal
Open Source Engineer
• Edited on• Edited

I don't thinkstolen is better word here. I prefer using the wordtaken.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Developer⌨, Fitness Freak🏋, Hipster🕴, Blogger👨‍💻, Productivity Hacker⌚, Technical Writer✍️, Tech talker👨‍🎤, Leader👨‍🔬, Always a Student👨‍🎓, Incomplete🔍 & Learning Junkie📚.
  • Location
    Bengaluru
  • Education
    B.Tech in Electronics & Telecommunication
  • Work
    Senior Staff @ Infineon Technologies
  • Joined

More fromVishal Chovatiya

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp