{"id":5405,"date":"2021-12-28T04:21:32","date_gmt":"2021-12-28T04:21:32","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=5405"},"modified":"2021-12-29T05:51:14","modified_gmt":"2021-12-29T05:51:14","slug":"facade-pattern","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/facade-pattern\/","title":{"rendered":"Facade Pattern"},"content":{"rendered":"<p>Facades are used to simplify the use of existing complex interfaces. It isolates a client from a sub-system so that the client can interact with the facade rather than with the sub-system. This is especially useful when working with legacy code, third-party code, complex code, or hard to refactor\/test code. So in short, a mask turns some **** into something good.<\/p>\n<p>A good frontend will have a constructor with <a href=\"https:\/\/studysection.com\/blog\/what-is-an-api\/\">interface<\/a>-type-indicating parameters and no new keyword.<\/p>\n<h2>How to use it<\/h2>\n<ul>\n<li>Create a square that will be the mask.<\/li>\n<li>Pass the parent class to the constructor of the facade as an interface-type-hint parameter.<\/li>\n<li>Create methods that use the methods of the parent class.<\/li>\n<\/ul>\n<h3>How it works<\/h3>\n<ul>\n<li>The client requests something.<\/li>\n<li>The request goes to the mask.<\/li>\n<li>The facade communicates with related subsystems to work.<\/li>\n<li>Sub-system responses are returned after the job is done &#8211; (Optional).<\/li>\n<li>Return to Client &#8211; (Optional).<\/li>\n<\/ul>\n<p><strong>Example<\/strong><br \/>\n<code>&lt;?php<br \/>\ninterface VegetableInterface<br \/>\n{<br \/>\n   public function doit();<br \/>\n}<br \/>\nclass WashingVeg implements VegetableInterface<br \/>\n{<br \/>\n   public function doit()<br \/>\n   {<br \/>\n       echo \"My Vegetables is washed done<\/br>\";<br \/>\n   }<br \/>\n}<br \/>\nclass Chopping implements VegetableInterface<br \/>\n{<br \/>\n   public function doit()<br \/>\n   {<br \/>\n       echo \"Vegetable chopped,onion etc done<\/br>\";<br \/>\n   }<br \/>\n}<br \/>\nclass AddingOil implements VegetableInterface<br \/>\n{<br \/>\n   public function doit()<br \/>\n   {<br \/>\n       echo \"Adding Oil in cooker and onion cook upto 5 mints<\/br>\";<br \/>\n   }<br \/>\n}<br \/>\nclass AddingWater implements VegetableInterface<br \/>\n{<br \/>\n   public function doit()<br \/>\n   {<br \/>\n       echo \"Some water is added<\/br>\";<br \/>\n   }<br \/>\n}<br \/>\nclass Cooked implements VegetableInterface<br \/>\n{<br \/>\n   public function doit()<br \/>\n   {<br \/>\n       echo \"Put all veg and wait 10 mint for making it<\/br>\";<br \/>\n   }<br \/>\n}<br \/>\n$washingObj = new WashingVeg();<br \/>\n$washingObj->doit();<br \/>\n$choppingObj = new Chopping();<br \/>\n$choppingObj->doit();<br \/>\n$addingOilObj = new AddingOil();<br \/>\n$addingOilObj->doit();<br \/>\n$addingWaterObj = new AddingWater();<br \/>\n$addingWaterObj->doit();<br \/>\n$CookerObj = new Cooked();<br \/>\n$CookerObj->doit();<br \/>\n?><\/code><\/p>\n<p>As you can see above, we are cooking vegetables. We have repeated the same step four times. It will be even more painful. What would be an ideal scenario to cook vegetables in one step. To better rephrase the above code, we can create a class called VegMakingFacade that handles all the tasks needed to create rice as follows:<br \/>\n<code>&lt;?php<br \/>\ninterface VegetableInterface<br \/>\n{<br \/>\n   public function doit();<br \/>\n}<br \/>\nclass WashingVeg implements VegetableInterface<br \/>\n{<br \/>\n   public function doit()<br \/>\n   {<br \/>\n       echo \"My Vegetables is washed done<\/br>\";<br \/>\n   }<br \/>\n}<br \/>\nclass Chopping implements VegetableInterface<br \/>\n{<br \/>\n   public function doit()<br \/>\n   {<br \/>\n       echo \"Vegetable chopped,onion etc done<\/br>\";<br \/>\n   }<br \/>\n}<br \/>\nclass AddingOil implements VegetableInterface<br \/>\n{<br \/>\n   public function doit()<br \/>\n   {<br \/>\n       echo \"Adding Oil in cooker and onion cook upto 5 mints<\/br>\";<br \/>\n   }<br \/>\n}<br \/>\nclass AddingWater implements VegetableInterface<br \/>\n{<br \/>\n   public function doit()<br \/>\n   {<br \/>\n       echo \"Some water is added<\/br>\";<br \/>\n   }<br \/>\n}<br \/>\nclass Cooked implements VegetableInterface<br \/>\n{<br \/>\n   public function doit()<br \/>\n   {<br \/>\n       echo \"Put all veg and wait 10 mint for making it<\/br>\";<br \/>\n   }<br \/>\n}<br \/>\nclass VegMakingFacade<br \/>\n{<br \/>\n   public static function makeVeg()<br \/>\n   {<br \/>\n       (new WashingVeg)->doit();<br \/>\n       (new Chopping)->doit();<br \/>\n       (new AddingOil)->doit();<br \/>\n       (new AddingWater)->doit();<br \/>\n       (new Cooked)->doit();<br \/>\n   }<br \/>\n}<br \/>\nVegMakingFacade::makeVeg();<\/code><\/p>\n<p>All the complexities of instantiating different classes to make vegetables are handled by the VegMakingFacade class and its only static method called makeVeg() and the user can easily create vegetables by just writing a single line of code.<\/p>\n<p><small><em>jQuery presents a tree-like structure of all the elements on a webpage simplifying the syntax and further manipulating such elements. The <a href=\"https:\/\/www.studysection.com\/jquery-3.x-foundation\">jQuery Certification exam<\/a> by StudySection will secure your fundamental knowledge and a basic understanding of jQuery as an asset to improve your skills.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Facades are used to simplify the use of existing complex interfaces. It isolates a client from a sub-system so that<\/p>\n","protected":false},"author":1,"featured_media":5406,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[742,200],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Facade Pattern - StudySection Blog<\/title>\n<meta name=\"description\" content=\"The facade is used to simplify the use of existing complex interfaces. It isolates a client from a sub-system\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/facade-pattern\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Facade Pattern - StudySection Blog\" \/>\n<meta property=\"og:description\" content=\"The facade is used to simplify the use of existing complex interfaces. It isolates a client from a sub-system\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/facade-pattern\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2021-12-28T04:21:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-29T05:51:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/12\/Facade.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/facade-pattern\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/facade-pattern\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Facade Pattern\",\"datePublished\":\"2021-12-28T04:21:32+00:00\",\"dateModified\":\"2021-12-29T05:51:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/facade-pattern\/\"},\"wordCount\":292,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"Facade Pattern\",\"php\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/facade-pattern\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/facade-pattern\/\",\"url\":\"https:\/\/studysection.com\/blog\/facade-pattern\/\",\"name\":\"Facade Pattern - StudySection Blog\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2021-12-28T04:21:32+00:00\",\"dateModified\":\"2021-12-29T05:51:14+00:00\",\"description\":\"The facade is used to simplify the use of existing complex interfaces. It isolates a client from a sub-system\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/facade-pattern\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/facade-pattern\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/facade-pattern\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Facade Pattern\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Facade Pattern - StudySection Blog","description":"The facade is used to simplify the use of existing complex interfaces. It isolates a client from a sub-system","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/studysection.com\/blog\/facade-pattern\/","og_locale":"en_US","og_type":"article","og_title":"Facade Pattern - StudySection Blog","og_description":"The facade is used to simplify the use of existing complex interfaces. It isolates a client from a sub-system","og_url":"https:\/\/studysection.com\/blog\/facade-pattern\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2021-12-28T04:21:32+00:00","article_modified_time":"2021-12-29T05:51:14+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/12\/Facade.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/facade-pattern\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/facade-pattern\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Facade Pattern","datePublished":"2021-12-28T04:21:32+00:00","dateModified":"2021-12-29T05:51:14+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/facade-pattern\/"},"wordCount":292,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["Facade Pattern","php"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/facade-pattern\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/facade-pattern\/","url":"https:\/\/studysection.com\/blog\/facade-pattern\/","name":"Facade Pattern - StudySection Blog","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2021-12-28T04:21:32+00:00","dateModified":"2021-12-29T05:51:14+00:00","description":"The facade is used to simplify the use of existing complex interfaces. It isolates a client from a sub-system","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/facade-pattern\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/facade-pattern\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/facade-pattern\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Facade Pattern"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":242,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5405"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=5405"}],"version-history":[{"count":4,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5405\/revisions"}],"predecessor-version":[{"id":5414,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/5405\/revisions\/5414"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/5406"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=5405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=5405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=5405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}