{"id":3902,"date":"2022-03-20T11:38:53","date_gmt":"2022-03-20T08:38:53","guid":{"rendered":"https:\/\/furkansandal.com\/?p=3902"},"modified":"2022-12-19T11:46:08","modified_gmt":"2022-12-19T08:46:08","slug":"what-is-the-builder-pattern-how-to-use-it-in-swift","status":"publish","type":"post","link":"https:\/\/furkansandal.com\/what-is-the-builder-pattern-how-to-use-it-in-swift\/","title":{"rendered":"What is the Builder Pattern? How to use it in Swift?"},"content":{"rendered":"

\"builder<\/a><\/p>\n

The Builder Pattern<\/strong> is a creational design pattern<\/strong> that separates the construction of a complex object from its representation<\/strong>, so that the same construction process can create different representations. The builder pattern is often used when the algorithm for creating a complex object should be independent of the parts that make up the object and how they are assembled.<\/p>\n

The builder pattern is used to create complex objects by specifying the type and content of each object in a step-by-step. The final step creates the object.<\/p>\n

Here is an example of how to use the builder pattern in Swift:<\/p>\n

class Builder {\r\n    var product = Product()\r\n\r\n    func setPartA() {\r\n        product.parts.append("PartA1")\r\n    }\r\n\r\n    func setPartB() {\r\n        product.parts.append("PartB1")\r\n    }\r\n\r\n    func setPartC() {\r\n        product.parts.append("PartC1")\r\n    }\r\n}\r\n\r\nclass Product {\r\n    var parts = [String]()\r\n\r\n    func listParts() -&gt; String {\r\n        return "Product parts: " + parts.joined(separator: ", ") + "\\n"\r\n    }\r\n}\r\n\r\n\/\/ Director\r\nclass Director {\r\n    var builder: Builder\r\n\r\n    init(builder: Builder) {\r\n        self.builder = builder\r\n    }\r\n\r\n    func buildMinimalViableProduct() {\r\n        builder.setPartA()\r\n    }\r\n\r\n    func buildFullFeaturedProduct() {\r\n        builder.setPartA()\r\n        builder.setPartB()\r\n        builder.setPartC()\r\n    }\r\n}\r\n\r\n\/\/ Client\r\nlet builder = Builder()\r\nlet director = Director(builder: builder)\r\n\r\nprint("Standard basic product:")\r\ndirector.buildMinimalViableProduct()\r\nprint(builder.product.listParts())\r\n\r\nprint("Standard full featured product:")\r\ndirector.buildFullFeaturedProduct()\r\nprint(builder.product.listParts())\r\n<\/pre>\n

In this example, the Builder<\/strong> class defines the construction steps for creating a Product<\/strong> object. The Director<\/strong> class controls the construction<\/strong> process by calling the suitable steps in the Builder. The Client<\/strong> code creates a Builder<\/strong> object and a Director<\/strong> object, and then calls the Director<\/strong> to construct the desired product. The final step creates the Product<\/strong> object.<\/p>\n","protected":false},"excerpt":{"rendered":"

The Builder Pattern is a creational design pattern that separates the construction of a complex object from its representation, so…<\/p>\n","protected":false},"author":1,"featured_media":3903,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","footnotes":""},"categories":[1,232,155],"tags":[240,238],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/furkansandal.com\/wp-content\/uploads\/2022\/12\/AdobeStock_267524919-2-scaled.jpeg?fit=2560%2C1440&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6BM7I-10W","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/furkansandal.com\/wp-json\/wp\/v2\/posts\/3902"}],"collection":[{"href":"https:\/\/furkansandal.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/furkansandal.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/furkansandal.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/furkansandal.com\/wp-json\/wp\/v2\/comments?post=3902"}],"version-history":[{"count":2,"href":"https:\/\/furkansandal.com\/wp-json\/wp\/v2\/posts\/3902\/revisions"}],"predecessor-version":[{"id":3905,"href":"https:\/\/furkansandal.com\/wp-json\/wp\/v2\/posts\/3902\/revisions\/3905"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/furkansandal.com\/wp-json\/wp\/v2\/media\/3903"}],"wp:attachment":[{"href":"https:\/\/furkansandal.com\/wp-json\/wp\/v2\/media?parent=3902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/furkansandal.com\/wp-json\/wp\/v2\/categories?post=3902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/furkansandal.com\/wp-json\/wp\/v2\/tags?post=3902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}