{"id":87,"date":"2023-09-18T22:55:09","date_gmt":"2023-09-18T20:55:09","guid":{"rendered":"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/?p=87"},"modified":"2023-09-18T22:55:09","modified_gmt":"2023-09-18T20:55:09","slug":"wzorce-projektowe-budowniczy-builder","status":"publish","type":"post","link":"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/?p=87","title":{"rendered":"Wzorce projektowe &#8211; Budowniczy (builder)"},"content":{"rendered":"\n<p>Wzorzec kreacyjny u\u017cywany w przypadku klas z wieloma potencjalnymi konstruktorami i zmiennymi. U\u0142atwia tworzenie nowych obiekt\u00f3w poprzez wywo\u0142ywanie setter\u00f3w klasy Budowniczy, zamiast wypisywania zmiennych w konstruktorze, co mo\u017ce by\u0107 problematyczne w przypadku klas z wieloma zmiennymi, kt\u00f3rych niekoniecznie chcemy wszystkich u\u017cy\u0107.<\/p>\n\n\n\n<p>Istniej\u0105 dwa rodzaje tego wzorca. Wersja klasyczna:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"812\" height=\"432\" src=\"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/wp-content\/uploads\/2023\/09\/budowniczy1.jpg\" alt=\"\" class=\"wp-image-88\" srcset=\"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/wp-content\/uploads\/2023\/09\/budowniczy1.jpg 812w, https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/wp-content\/uploads\/2023\/09\/budowniczy1-300x160.jpg 300w, https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/wp-content\/uploads\/2023\/09\/budowniczy1-768x409.jpg 768w\" sizes=\"auto, (max-width: 812px) 100vw, 812px\" \/><\/figure>\n\n\n\n<p>Oraz wersja prostsza, wbudowana w klas\u0119, kt\u00f3rej obiekty ma budowa\u0107:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"334\" height=\"351\" src=\"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/wp-content\/uploads\/2023\/09\/budowniczy2.jpg\" alt=\"\" class=\"wp-image-89\" srcset=\"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/wp-content\/uploads\/2023\/09\/budowniczy2.jpg 334w, https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/wp-content\/uploads\/2023\/09\/budowniczy2-285x300.jpg 285w\" sizes=\"auto, (max-width: 334px) 100vw, 334px\" \/><\/figure>\n\n\n\n<p>Wersja uproszczona u\u017cywana jest dla klas, kt\u00f3re b\u0119d\u0105 budowane w jeden spos\u00f3b, natomiast wersja klasyczna, dla klas, kt\u00f3rych obiekty mog\u0105 by\u0107 tworzone na r\u00f3\u017cne sposoby.<\/p>\n\n\n\n<p>W praktyce za\u0142\u00f3\u017cmy, \u017ce mamy aplikacj\u0119, kt\u00f3ra ma nam &#8222;budowa\u0107&#8221; obiekty burrito. kod bez zastosowania budowniczego wygl\u0105da\u0142by tak:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nimport burrito.Burrito;\n\npublic class Main {\n    public static void main(String&#91;] args) {\n\n        Burrito burrito1 = new Burrito(&quot;tortilla&quot;, &quot;beef&quot;, &quot;beans&quot;, &quot;chilli&quot;, &quot;rice&quot;, &quot;guacamole&quot;);\n    }\n}\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage burrito;\n\npublic class Burrito {\n\n\n    private String tortilla;\n    private String beef;\n    private String beans;\n    private String chilli;\n    private String rice;\n    private String guacamole;\n    private String sauce;\n\n    public Burrito(String tortilla, String beef, String beans, String chilli, String rice, String guacamole, String sauce) {\n        this.tortilla = tortilla;\n        this.beef = beef;\n        this.beans = beans;\n        this.chilli = chilli;\n        this.rice = rice;\n        this.guacamole = guacamole;\n        this.sauce = sauce;\n    }\n\n    public Burrito(String tortilla, String beef, String chilli) {\n        this.tortilla = tortilla;\n        this.beef = beef;\n        this.chilli = chilli;\n    }\n\n    public Burrito(String tortilla, String beef, String beans, String chilli, String rice, String guacamole) {\n        this.tortilla = tortilla;\n        this.beef = beef;\n        this.beans = beans;\n        this.chilli = chilli;\n        this.rice = rice;\n        this.guacamole = guacamole;\n    }\n}\n\n<\/pre><\/div>\n\n\n<p>Jak wida\u0107 w przypadkach, kiedy kto\u015b nie \u017cyczy sobie jakiego\u015b sk\u0142adnika w swoim burrito, to nale\u017cy stworzy\u0107 nowy konstruktor bez tego sk\u0142adnika. Po zastosowaniu wzorca wbudowanego budowniczego kod wygl\u0105da\u0142by tak:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nimport burrito.Burrito;\n\npublic class Main {\n    public static void main(String&#91;] args) {\n\n        Burrito burrito = new Burrito.BurritoBuilder()\n                .buildBeans(&quot;beans&quot;)\n                .buildTortilla(&quot;tortilla&quot;)\n                .buildBeef(&quot;beef&quot;)\n                .buildSauce(&quot;sauce&quot;)\n                .build();\n\n    }\n}\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage burrito;\n\npublic class Burrito {\n\n\n    private String tortilla;\n    private String beef;\n    private String beans;\n    private String chilli;\n    private String rice;\n    private String guacamole;\n    private String sauce;\n\n    public Burrito(BurritoBuilder burritoBuilder) {\n        this.tortilla = burritoBuilder.tortilla;\n        this.beef = burritoBuilder.beef;\n        this.beans = burritoBuilder.beans;\n        this.chilli = burritoBuilder.chilli;\n        this.rice = burritoBuilder.rice;\n        this.guacamole = burritoBuilder.guacamole;\n        this.sauce = burritoBuilder.sauce;\n    }\n\n    public String getTortilla() {\n        return tortilla;\n    }\n\n    public String getBeef() {\n        return beef;\n    }\n\n    public String getBeans() {\n        return beans;\n    }\n\n    public String getChilli() {\n        return chilli;\n    }\n\n    public String getRice() {\n        return rice;\n    }\n\n    public String getGuacamole() {\n        return guacamole;\n    }\n\n    public String getSauce() {\n        return sauce;\n    }\n\n    public static class BurritoBuilder{\n        private String tortilla;\n        private String beef;\n        private String beans;\n        private String chilli;\n        private String rice;\n        private String guacamole;\n        private String sauce;\n\n        public BurritoBuilder buildTortilla(String tortilla) {\n            this.tortilla = tortilla;\n            return this;\n        }\n\n        public BurritoBuilder buildBeef(String beef) {\n            this.beef = beef;\n            return this;\n        }\n\n        public BurritoBuilder buildBeans(String beans) {\n            this.beans = beans;\n            return this;\n        }\n\n        public BurritoBuilder buildChilli(String chilli) {\n            this.chilli = chilli;\n            return this;\n        }\n\n        public BurritoBuilder buildRice(String rice) {\n            this.rice = rice;\n            return this;\n        }\n\n        public BurritoBuilder buildGuacamole(String guacamole) {\n            this.guacamole = guacamole;\n            return this;\n        }\n\n        public BurritoBuilder buildSauce(String sauce) {\n            this.sauce = sauce;\n            return this;\n        }\n\n        public Burrito build(){\n            return new Burrito(this);\n        }\n    }\n}\n\n<\/pre><\/div>\n\n\n<p>Dzi\u0119ki tej zmianie nie musimy si\u0119 martwi\u0107 o dodawanie kolejnych konstruktor\u00f3w do klasy i mo\u017cemy zrobi\u0107 takie burrito, na jakie mamy ochot\u0119.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Wzorzec kreacyjny u\u017cywany w przypadku klas z wieloma potencjalnymi konstruktorami i zmiennymi. U\u0142atwia tworzenie nowych obiekt\u00f3w poprzez wywo\u0142ywanie setter\u00f3w klasy Budowniczy, zamiast wypisywania zmiennych w konstruktorze, co mo\u017ce by\u0107 problematyczne w przypadku klas z wieloma zmiennymi, kt\u00f3rych niekoniecznie chcemy wszystkich u\u017cy\u0107. Istniej\u0105 dwa rodzaje tego wzorca. Wersja klasyczna: Oraz wersja prostsza, wbudowana w klas\u0119, kt\u00f3rej [&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-87","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\/87","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=87"}],"version-history":[{"count":1,"href":"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/87\/revisions"}],"predecessor-version":[{"id":90,"href":"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/index.php?rest_route=\/wp\/v2\/posts\/87\/revisions\/90"}],"wp:attachment":[{"href":"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=87"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=87"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jan-napiorkowski.profesjonalnyprogramista.pl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=87"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}