{"id":4924,"date":"2024-02-18T13:22:19","date_gmt":"2024-02-18T13:22:19","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=4924"},"modified":"2024-11-07T16:39:52","modified_gmt":"2024-11-07T16:39:52","slug":"facade-pattern","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/facade-pattern\/","title":{"rendered":"Facade Pattern"},"content":{"rendered":"<blockquote><p>Facade design pattern simplifies the interface to a complex system. In Facade Pattern, the facade class does all the required tasks instead of allowing the client app to them.<\/p><\/blockquote>\n<p>Imagine walking to a self service restaurant, McDonalds where diner need to select food from UI, Pay the amount and wait in counter, check the screen for token no, collect the food and find a table before eating. Now in a Dine-In restaurant instead of diner doing all these steps we make the waiter do all these and get the food. Waiter is the <strong>Facade<\/strong> and Diner is the <strong>Client<\/strong><\/p>\n<p><strong class=\"ctaHeader3\">Why to use Facade Pattern?<\/strong><br \/>\nClasses in all design patterns are just normal classes. What is important is how they are structured and how they work together to solve a given problem in the best possible way.<\/p>\n<p>The Facade design pattern simplifies the interface to a complex system; because it is usually composed of all the classes which make up the subsystems of the complex system.<\/p>\n<p>A Facade shields the user from the complex details of the system and provides them with a simplified view of it which is easy to use. It also decouples the code that uses the system from the details of the subsystems, making it easier to modify the system later.<\/p>\n<p><strong class=\"ctaHeader3\">When to use Facade Pattern?<\/strong><br \/>\nTo provide  higher-level interface that makes the subsystem easier to use.<\/p>\n<p><strong class=\"ctaHeader3\">How to implement Facade Pattern?<\/strong><br \/>\nInstead of exposing the complex code logic we wrap the complex code in a separate class <strong>facade class<\/strong> and call the method of Facade Class.<\/p>\n<p><strong  class=\"ctaHeader3\">Code Implementation<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2024\/02\/FacadeExample.png\" alt=\"\" \/><\/p>\n<ol>\n<li>OnlineApp has many code logic which includes Creating Order, Inventory Updation, Payment Tracking, Seller Buyer Notification and Shipment<\/li>\n<li>Instead of writing all the code in onlineApp we can move the code to FacadeClass(<strong>OrderFacade.java<\/strong>) and call the method of OrderFacade<\/li>\n<\/ol>\n<p><strong class=\"ctaHeader3\">Code without Facade Pattern<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2024\/02\/withoutFacade.png\" alt=\"\" \/><br \/>\n<strong>OnlineApp.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class OnlineApp {\r\n    public static void main(String&#x5B;] args) {\r\n        System.out.println(&quot;Step 1: Order Created&quot;);\r\n        Order objOrder = new Order();\r\n        objOrder.placeOrder();\r\n        System.out.println(&quot;------------------------&quot;);\r\n\r\n        System.out.println(&quot;Step 2: Inventory Updated&quot;);\r\n        Inventory objInventory = new Inventory();\r\n        objInventory.checkInventory(&quot;Iphone 13&quot;);\r\n        System.out.println(&quot;------------------------&quot;);\r\n\r\n        System.out.println(&quot;Step 3: Payment Succesful&quot;);\r\n        Payment objPayment = new Payment();\r\n        objPayment.checkPaymentStatus(&quot;873901&quot;);\r\n        System.out.println(&quot;------------------------&quot;);\r\n\r\n        System.out.println(&quot;Step 4: Seller and Buyer Notified&quot;);\r\n        Notifications objNotification = new Notifications();\r\n        objNotification.notifyBuyer(&quot;873901&quot;);\r\n        objNotification.notifySeller(&quot;873901&quot;);\r\n        System.out.println(&quot;------------------------&quot;);\r\n\r\n        System.out.println(&quot;Step 5: Shipment Done&quot;);\r\n        Shipment objShipment = new Shipment();\r\n        objShipment.shipProduct(&quot;Road Name, Location&quot;);\r\n        System.out.println(&quot;------------------------&quot;);\r\n\r\n    }\r\n}\r\n<\/pre>\n<p><strong class=\"ctaHeader3\">Code with Facade Pattern<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2024\/02\/withFacade.png\" alt=\"\" \/><br \/>\n<strong>OrderFacade.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class OrderFacade {\r\n    Order objOrder = new Order();\r\n    Inventory objInventory = new Inventory();\r\n    Payment objPayment = new Payment();\r\n    Notifications objNotification = new Notifications();\r\n    Shipment objShipment = new Shipment();\r\n\r\n    public void placeOrder(String orderId) {\r\n        System.out.println(&quot;Step 1: Order Created&quot;);\r\n        objOrder.placeOrder();\r\n        System.out.println(&quot;------------------------&quot;);\r\n\r\n        System.out.println(&quot;Step 2: Inventory Updated&quot;);\r\n        objInventory.checkInventory(&quot;Iphone 13&quot;);\r\n        System.out.println(&quot;------------------------&quot;);\r\n\r\n        System.out.println(&quot;Step 3: Payment Succesful&quot;);\r\n        objPayment.checkPaymentStatus(orderId);\r\n        System.out.println(&quot;------------------------&quot;);\r\n\r\n        System.out.println(&quot;Step 4: Seller and Buyer Notified&quot;);\r\n        objNotification.notifyBuyer(orderId);\r\n        objNotification.notifySeller(orderId);\r\n        System.out.println(&quot;------------------------&quot;);\r\n\r\n        System.out.println(&quot;Step 5: Shipment Done&quot;);\r\n        objShipment.shipProduct(&quot;Road Name, Location&quot;);\r\n        System.out.println(&quot;------------------------&quot;);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>OnlineApp.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class OnlineApp {\r\n    public static void main(String&#x5B;] args) {\r\n        OrderFacade objOrderFacade = new OrderFacade();\r\n        objOrderFacade.placeOrder(&quot;873901&quot;);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nStep 1: Order Created\r\nDisplay Cart Order\r\n------------------------\r\nStep 2: Inventory Updated\r\n------------------------\r\nStep 3: Payment Succesful\r\n------------------------\r\nStep 4: Seller and Buyer Notified\r\nOrder placed by Buyer\r\nOrder Received by Seller\r\n------------------------\r\nStep 5: Shipment Done\r\nShipping toRoad Name, Location\r\n------------------------\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Facade design pattern simplifies the interface to a complex system. In Facade Pattern, the facade class does all the required tasks instead of allowing the client app to them. Imagine walking to a self service restaurant, McDonalds where diner need to select food from UI, Pay the amount and wait in counter, check the screen&hellip; <a href=\"https:\/\/codethataint.com\/blog\/facade-pattern\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[355,357,64],"tags":[],"class_list":["post-4924","post","type-post","status-publish","format-standard","hentry","category-structural-pattern","category-facade-pattern","category-design-patterns"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=4924"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4924\/revisions"}],"predecessor-version":[{"id":5317,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4924\/revisions\/5317"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=4924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=4924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=4924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}