{"id":9268,"date":"2020-11-16T17:40:43","date_gmt":"2020-11-16T08:40:43","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=9268"},"modified":"2020-11-16T14:42:06","modified_gmt":"2020-11-16T05:42:06","slug":"facade-design-pattern","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/9268","title":{"rendered":"Facade Design Pattern"},"content":{"rendered":"\n<p>Facade is an structural design style that provides a simplified interface for a library, framework, or any other complex set of classes.<\/p>\n\n\n\n<p>Let&#8217;s say you need to make your code work with a broad set of objects belonging to a sophisticated library or framework. This typically involves initializing all of these objects, tracking dependencies, executing methods in the correct order, and so on.<\/p>\n\n\n\n<p>As a result, the business logic of the class is tightly coupled with the implementation details of the third-party class, making it difficult to understand and maintain.<\/p>\n\n\n\n<p>Facade is a class that provides a simple interface to a complex subsystem containing many moving parts. Facades can provide limited functionality compared to working directly with subsystems. However, this only includes features that the client really cares about.<\/p>\n\n\n\n<p>Lets look at the examples as below.<\/p>\n\n\n\n<h3>PHP Sample Code<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n\/\/ 3rd party module classes\nclass Kitchen\n{\n    public function cook()\n    {\n        echo \"cooking order\";\n    }\n}\n\nclass Cashier\n{\n    public function bill()\n    {\n        echo \"printing bill\";\n    }\n}\n\n\n\/\/Waiter facade hiding the complexity of other 3rd party classes.\n\/\/wrapper class of our all third party packages.\nclass Waiter\n{\n    public $kitchen;\n    public $cashier;\n\n    public function __construct()\n    {\n        $this->kitchen = new Kitchen();\n        $this->cashier = new Cashier();\n    }\n\n    \/\/providing a simple method called order for client\n    public function order()\n    {\n        $this->kitchen->cook();\n        $this->cashier->bill();\n    }\n}\n\n\n$waiter = new Waiter();\n$waiter->order();\n\n<\/code><\/pre>\n\n\n\n<h3><a href=\"https:\/\/github.com\/HlaingTinHtun\/GOF-Design-Patterns\/blob\/master\/articles\/GOF_Patterns\/structural_patterns\/facade.md#c-sample-code\"><\/a>C# Sample Code<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\n\nnamespace HelloWorld\n{\n\n    \/\/Waiter facade hiding the complexity of other 3rd party classes.\n    \/\/wrapper class of our all third party packages.\n    public class Facade\n    {\n        protected Kitchen _kitchen;\n\n        protected Cashier _cashier;\n\n        public Facade()\n        {\n            this._kitchen = new Kitchen();\n            this._cashier = new Cashier();\n        }\n\n        \/\/providing a simple method called order for client\n        public string order()\n        {\n            string result = \"\";\n            result += this._kitchen.cook();\n            result += this._cashier.bill();\n            return result;\n        }\n    }\n\n    \/\/ 3rd party module classes\n    public class Kitchen\n    {\n        public string cook()\n        {\n            return \"cook order\";\n        }\n    }\n\n    public class Cashier\n    {\n        public string bill()\n        {\n            return \"print bill\";\n        }\n\n    }\n\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            Facade facade = new Facade();\n            Console.Write(facade.order());\n        }\n    }\n\n}\n<\/code><\/pre>\n\n\n\n<p>By Yuuma.<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9268\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/9268\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Facade Design Pattern\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9268\" data-text=\"Facade Design Pattern\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9268\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9268\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Facade is an structural design style that provides a simplified interface for a library, framework, or any oth [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9268"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=9268"}],"version-history":[{"count":2,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9268\/revisions"}],"predecessor-version":[{"id":9270,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9268\/revisions\/9270"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=9268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=9268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=9268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}