{"id":75,"date":"2023-09-18T21:27:48","date_gmt":"2023-09-18T19:27:48","guid":{"rendered":"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/?p=75"},"modified":"2023-09-18T21:27:48","modified_gmt":"2023-09-18T19:27:48","slug":"wzorce-projektowe-fasada-facade","status":"publish","type":"post","link":"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/?p=75","title":{"rendered":"Wzorce projektowe &#8211; Fasada (facade)"},"content":{"rendered":"\n<p>Celem wzorca projektowego fasada jest ukrycie klas i funkcji aplikacji, do kt\u00f3rych dany u\u017cytkownik nie powinien mie\u0107 dost\u0119pu. Odbywa si\u0119 to za pomoc\u0105 dodatkowej klasy &#8222;fasadowej&#8221;, w kt\u00f3rej znajduj\u0105 si\u0119 funkcje odnosz\u0105ce si\u0119 do docelowych, ukrywanych klas. Jest to bardzo przydatny wzorzec w przypadku aplikacji, w kt\u00f3rych u\u017cytkownicy maj\u0105 r\u00f3\u017cne poziomy dost\u0119pu.<\/p>\n\n\n\n<p>Schemat dzia\u0142ania tego wzorca prezentuje si\u0119 nast\u0119puj\u0105co:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"623\" height=\"322\" src=\"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/wp-content\/uploads\/2023\/09\/facade.jpg\" alt=\"\" class=\"wp-image-76\" srcset=\"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/wp-content\/uploads\/2023\/09\/facade.jpg 623w, https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/wp-content\/uploads\/2023\/09\/facade-300x155.jpg 300w\" sizes=\"auto, (max-width: 623px) 100vw, 623px\" \/><\/figure>\n\n\n\n<p>Przyk\u0142adowo na prostej aplikacji bankowej zastosowanie tego wzorca wygl\u0105da\u0142oby nast\u0119puj\u0105co:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nimport bank.AtmMachine;\nimport bank.BankSystem;\n\npublic class Main {\n    public static void main(String&#91;] args) {\n        AtmMachine atmMachine = new AtmMachine();\n        BankSystem bankSystem = new BankSystem();\n\n        atmMachine.enterPin();\n        if (bankSystem.validatePin() &amp;&amp; bankSystem.validateTransaction()){\n            atmMachine.withdrawCash();\n        }\n    }\n}\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage bank;\n\npublic class AtmMachine {\n    public void checkBalance(){\n\n    }\n\n    public void enterPin(){\n        System.out.println(&quot;PIN entered.&quot;);\n    }\n\n    public void withdrawCash(){\n        System.out.println(&quot;Cash withdrawn successfully!&quot;);\n    }\n}\n\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage bank;\n\npublic class BankSystem {\n    public void transferMoney(){\n\n    }\n\n    public boolean validatePin(){\n        System.out.println(&quot;PIN number validated.&quot;);\n        return true;\n    }\n\n    public boolean validateTransaction(){\n        System.out.println(&quot;Transaction validated.&quot;);\n        return true;\n    }\n\n    public void getTransactionHistory(){\n\n    }\n}\n<\/pre><\/div>\n\n\n<p>Po dodaniu klasy fasadowej oraz niewielkich zmianach w kodzie program ten wygl\u0105da nast\u0119puj\u0105co:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nimport bank.AtmMachineFacade;\n\npublic class Main {\n    public static void main(String&#91;] args) {\n\n        AtmMachineFacade atmMachineFacade = new AtmMachineFacade();\n        atmMachineFacade.withdrawMoney();\n    }\n}\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage bank;\n\nclass AtmMachine {\n    void checkBalance(){\n\n    }\n\n    void enterPin(){\n        System.out.println(&quot;PIN entered.&quot;);\n    }\n\n    void withdrawCash(){\n        System.out.println(&quot;Cash withdrawn successfully!&quot;);\n    }\n}\n\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage bank;\n\nclass BankSystem {\n    void transferMoney(){\n\n    }\n\n    boolean validatePin(){\n        System.out.println(&quot;PIN number validated.&quot;);\n        return true;\n    }\n\n    boolean validateTransaction(){\n        System.out.println(&quot;Transaction validated.&quot;);\n        return true;\n    }\n\n    void getTransactionHistory(){\n\n    }\n}\n\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage bank;\n\npublic class AtmMachineFacade {\n    private AtmMachine atmMachine;\n    private BankSystem bankSystem;\n\n    public AtmMachineFacade() {\n        atmMachine = new AtmMachine();\n        bankSystem = new BankSystem();\n    }\n\n    public void withdrawMoney(){\n        atmMachine.enterPin();\n        if (bankSystem.validatePin() &amp;&amp; bankSystem.validateTransaction()){\n            atmMachine.withdrawCash();\n        }\n    }\n}\n\n<\/pre><\/div>\n\n\n<p>W wyniku zmian klasy AtmMachine i BankSystem sta\u0142y si\u0119 niepubliczne, a kod w klasie g\u0142\u00f3wnej sta\u0142 si\u0119 bardziej przejrzysty. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Celem wzorca projektowego fasada jest ukrycie klas i funkcji aplikacji, do kt\u00f3rych dany u\u017cytkownik nie powinien mie\u0107 dost\u0119pu. Odbywa si\u0119 to za pomoc\u0105 dodatkowej klasy &#8222;fasadowej&#8221;, w kt\u00f3rej znajduj\u0105 si\u0119 funkcje odnosz\u0105ce si\u0119 do docelowych, ukrywanych klas. Jest to bardzo przydatny wzorzec w przypadku aplikacji, w kt\u00f3rych u\u017cytkownicy maj\u0105 r\u00f3\u017cne poziomy dost\u0119pu. Schemat dzia\u0142ania tego [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-75","post","type-post","status-publish","format-standard","hentry","category-blog"],"_links":{"self":[{"href":"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/75","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=75"}],"version-history":[{"count":5,"href":"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/75\/revisions"}],"predecessor-version":[{"id":81,"href":"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/75\/revisions\/81"}],"wp:attachment":[{"href":"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=75"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=75"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=75"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}