{"id":6473,"date":"2022-11-07T13:48:42","date_gmt":"2022-11-07T13:48:42","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/the-facade-pattern\/"},"modified":"2022-11-07T13:48:42","modified_gmt":"2022-11-07T13:48:42","slug":"the-facade-pattern","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/the-facade-pattern\/","title":{"rendered":"The Facade Pattern"},"content":{"rendered":"<p>Today, I write about the Facade Pattern. The Facade Pattern is a structural pattern and has one purpose: to provide a simplified interface to any complex system.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6385\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/patterns.png\" alt=\"patterns\" width=\"650\" height=\"330\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/p>\n<p>The key idea of the <code>Facade Pattern<\/code> is to provide a simplified interface to a complex system, consisting of a set of interfaces. A facade defines a higher-level interface that makes the subsystem easier to use. The high-level interface intends not to support all use cases of the complex system, but only the most important. Despite the Facade Pattern for the simplified interface, it is often still possible to use the complex system directly.<\/p>\n<p>The Facade Pattern is an ideal starting point for decoupling complex systems by introducing layers. Additionally, it can be used as a starting point for deprecating the old interface.<\/p>\n<p>Here are the facts.<\/p>\n<h2>Facade Pattern<\/h2>\n<h3>Purpose<\/h3>\n<ul>\n<li>Provides a simplified interface to a set of interfaces<\/li>\n<\/ul>\n<h3>Use Case<\/h3>\n<ul>\n<li>Simplified access to a complex system<\/li>\n<li>A complex system contains many dependencies which are decoupled through the facade<\/li>\n<li>Introduction of layers within a complex system; the layer help to decouple the complex system<\/li>\n<\/ul>\n<h3>Structure<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6471\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/11\/Facade.png\" alt=\"Facade\" width=\"650\" height=\"295\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/11\/Facade.png 670w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/11\/Facade-300x136.png 300w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong><code>Facade<\/code><\/strong><\/p>\n<ul>\n<li>Offers the simplified interface<\/li>\n<li>Delegates requests to the subsystems<\/li>\n<\/ul>\n<p><strong><code>Package<\/code><\/strong><\/p>\n<ul>\n<li>Implements the functionality<\/li>\n<li>Knows nothing about the facade<\/li>\n<\/ul>\n<h3>Example<\/h3>\n<p>The following example is from Wikibook C++Programming: <a href=\"https:\/\/en.wikibooks.org\/wiki\/C%2B%2B_Programming\/Code\/Design_Patterns#Facade\">code patterns design<\/a><\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ from https:\/\/en.wikibooks.org\/wiki\/C%2B%2B_Programming\/Code\/Design_Patterns#Facade<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">using<\/span> <span style=\"color: #006699; font-weight: bold;\">namespace<\/span> std;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Alarm                                                            <span style=\"color: #00aa88; font-weight: bold;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ (2) <\/span><\/span><\/span>\r\n{\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n\t<span style=\"color: #007788; font-weight: bold;\">void<\/span> alarmOn()\r\n\t{\r\n\t\tcout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Alarm is on and house is secured\"<\/span><span style=\"color: #555555;\">&lt;&lt;<\/span>endl;\r\n\t}\r\n\r\n\t<span style=\"color: #007788; font-weight: bold;\">void<\/span> alarmOff()\r\n\t{\r\n\t\tcout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Alarm is off and you can go into the house\"<\/span><span style=\"color: #555555;\">&lt;&lt;<\/span>endl;\r\n\t}\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Ac                                                              <\/span><span style=\"color: #00aa88; font-weight: bold;\"><span style=\"color: #00aa88; font-weight: bold;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span><\/span><\/span>\r\n{\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n\t<span style=\"color: #007788; font-weight: bold;\">void<\/span> acOn()\r\n\t{\r\n\t\tcout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Ac is on\"<\/span><span style=\"color: #555555;\">&lt;&lt;<\/span>endl;\r\n\t}\r\n\r\n\t<span style=\"color: #007788; font-weight: bold;\">void<\/span> acOff()\r\n\t{\r\n\t\tcout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"AC is off\"<\/span><span style=\"color: #555555;\">&lt;&lt;<\/span>endl;\r\n\t}\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Tv                                                              <span style=\"color: #00aa88; font-weight: bold;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span><\/span><\/span>\r\n{\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n\t<span style=\"color: #007788; font-weight: bold;\">void<\/span> tvOn()\r\n\t{\r\n\t\tcout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Tv is on\"<\/span><span style=\"color: #555555;\">&lt;&lt;<\/span>endl;\r\n\t}\r\n\r\n\t<span style=\"color: #007788; font-weight: bold;\">void<\/span> tvOff()\r\n\t{\r\n\t\tcout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"TV is off\"<\/span><span style=\"color: #555555;\">&lt;&lt;<\/span>endl;\r\n\t}\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">HouseFacade                                                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span><\/span>\r\n{\r\n\tAlarm alarm;\r\n\tAc ac;\r\n\tTv tv;\r\n\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n\tHouseFacade(){}\r\n\r\n\t<span style=\"color: #007788; font-weight: bold;\">void<\/span> goToWork()                                              \/\/ ( \r\n\t{\r\n\t\tac.acOff();\r\n\t\ttv.tvOff();\r\n\t\talarm.alarmOn();\r\n\t}\r\n\r\n\t<span style=\"color: #007788; font-weight: bold;\">void<\/span> comeHome()                                             \/\/ (5)       Facade \r\n\t{\r\n\t\talarm.alarmOff();\r\n\t\tac.acOn();\r\n\t\ttv.tvOn();\r\n\t}\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>()\r\n{\r\n\tHouseFacade hf;\r\n\r\n\t<span style=\"color: #0099ff; font-style: italic;\">\/\/Rather than calling 100 different on and off functions thanks to facade I only have 2 functions...<\/span>\r\n\thf.goToWork();\r\n\thf.comeHome();\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The class <code>HouseFacade<\/code> (line 1) provides simplifies the usage of the classes <code>Alarm<\/code>, <code>Ac<\/code>, and <code>TV<\/code> (lines 2 to 4). The simplified interface consists of the two member functions <code>goToWork<\/code> (line 5) and <code>comeHome<\/code> (line 6). Both member functions encapsulate the original interface&#8217;s underlying member functions and guarantee that the member functions are called in the correct sequence.<\/p>\n<p>Finally, here is the output of the program:<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6472\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/11\/facadePattern.png\" alt=\"facadePattern\" width=\"523\" height=\"268\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/11\/facadePattern.png 523w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/11\/facadePattern-300x154.png 300w\" sizes=\"auto, (max-width: 523px) 100vw, 523px\" \/><\/p>\n<p>The Facade is probably the most heavily used Design Pattern in C++.<\/p>\n<h3>Known Uses<\/h3>\n<p>In general, calling a function that triggers an operating system call applies the Facade Pattern. Here are a few examples:<\/p>\n<ul>\n<li>Containers of the Standard Template Library automatically manage their memory.<\/li>\n<li>The threading API triggers calls to the underlying threading infrastructure, such as <a href=\"https:\/\/en.wikipedia.org\/wiki\/Pthreads\">pthread<\/a> or<a href=\"http:\/\/www.cs.rpi.edu\/courses\/fall06\/netprog\/WindowsThreads.html\"> win32 threads<\/a>.<\/li>\n<li>The parallel STL calls the underlying infrastructure, such as the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Threading_Building_Blocks\">Threading Building Blocks<\/a> (TBB) or the<a href=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/parallel\/concrt\/parallel-patterns-library-ppl?view=msvc-170\"> Parallel Patterns Library<\/a> (PPL).<\/li>\n<li>The<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/filesystem\"> filesystem library<\/a> is an abstraction above the underlying operating system-specific filesystem library.<\/li>\n<li>There are more operating system-specific abstractions. Here are a few: <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/chrono\">chrono library<\/a>, <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/numeric\">random library<\/a>, or the <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/utility\/format\">formatting library. <\/a><\/li>\n<\/ul>\n<h3>Related Patterns<\/h3>\n<ul>\n<li>The<a href=\"https:\/\/en.wikipedia.org\/wiki\/Adapter_pattern\"> Adaptor Pattern<\/a> adjusts an existing interface, but the Facade creates a new, simplified interface.<\/li>\n<li>An <a href=\"https:\/\/en.wikipedia.org\/wiki\/Abstract_factory_pattern\">Abstract Factory<\/a> is an alternative way to create a subsystem abstraction transparently.<\/li>\n<li>The <a href=\"https:\/\/en.wikipedia.org\/wiki\/Mediator_pattern\">Mediator Pattern<\/a> coordinates organization between objects, but the Facade creates a new, simplified interface.<\/li>\n<li>The <a href=\"https:\/\/en.wikipedia.org\/wiki\/Singleton_pattern\">Singleton Pattern<\/a> can act as a single access point to a complex subsystem.&nbsp;<\/li>\n<\/ul>\n<p>What are the pros and cons of the Facade Pattern?<\/p>\n<h3>Pros and Cons<\/h3>\n<h4>Pros<\/h4>\n<ul>\n<li>The complexity of the code can be hidden from the client.<\/li>\n<li>The misuse of the complex system is drastically reduced:&nbsp; &#8220;<em>Make interfaces easy to use correctly and hard to use incorrectly.<\/em>&#8221; by Scott Meyer in his post &#8220;<a href=\"https:\/\/www.aristeia.com\/Papers\/IEEE_Software_JulAug_2004_revised.htm\">The Most Important Design Guideline?<\/a>&#8220;.<\/li>\n<li>It helps to port the complex system to another platform because the client only depends on the facade.<\/li>\n<\/ul>\n<h4>Cons<\/h4>\n<ul>\n<li>A Facade may have too many responsibilities, and it ends in the antipattern <a href=\"https:\/\/en.wikipedia.org\/wiki\/God_object\">God Object<\/a>.<\/li>\n<\/ul>\n<p>A Facade Pattern and a Singleton Pattern are pretty similar. They provide a single access point to a complex system. Consequentially, the pros and cons of the Singleton Pattern also apply to the Facade Pattern. Read more about the pros and cons of the Singleton Pattern in my previous post: &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/singleton-pros-and-cons\">The Singleton: Pros and Cons<\/a>&#8220;.<\/p>\n<h2>What&#8217;s Next?<\/h2>\n<p>In my next post, I present the remaining structural pattern from the book &#8220;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Design_Patterns\">Design Patterns: Elements of Reusable Object-Oriented Software&#8221;<\/a>: the Proxy Pattern. The<a href=\"https:\/\/en.wikipedia.org\/wiki\/Proxy_pattern\"> Proxy Pattern<\/a> is used as a placeholder for accessing another object.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, I write about the Facade Pattern. The Facade Pattern is a structural pattern and has one purpose: to provide a simplified interface to any complex system.<\/p>\n","protected":false},"author":21,"featured_media":6385,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[379],"tags":[],"class_list":["post-6473","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-patterns"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6473","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=6473"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6473\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6385"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}