[{"data":1,"prerenderedAt":1095},["ShallowReactive",2],{"i-simple-icons:github":3,"i-simple-icons:x":8,"i-brand:linkedin":10,"i-simple-icons:medium":14,"i-lucide:mail":16,"i-lucide:sun":18,"pattern:\u002Flearn\u002Fdesign-patterns\u002Fbuilder":20,"pattern-group:\u002Flearn\u002Fdesign-patterns\u002Fbuilder":1066,"i-lucide:lightbulb":1083,"i-lucide:wand-sparkles":1085,"i-lucide:circle-dot":1087,"i-lucide:factory":1089,"i-lucide:hammer":1091,"i-lucide:copy":1093},{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":7},0,24,false,"\u003Cpath fill=\"currentColor\" d=\"M12 .297c-6.63 0-12 5.373-12 12c0 5.303 3.438 9.8 8.205 11.385c.6.113.82-.258.82-.577c0-.285-.01-1.04-.015-2.04c-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729c1.205.084 1.838 1.236 1.838 1.236c1.07 1.835 2.809 1.305 3.495.998c.108-.776.417-1.305.76-1.605c-2.665-.3-5.466-1.332-5.466-5.93c0-1.31.465-2.38 1.235-3.22c-.135-.303-.54-1.523.105-3.176c0 0 1.005-.322 3.3 1.23c.96-.267 1.98-.399 3-.405c1.02.006 2.04.138 3 .405c2.28-1.552 3.285-1.23 3.285-1.23c.645 1.653.24 2.873.12 3.176c.765.84 1.23 1.91 1.23 3.22c0 4.61-2.805 5.625-5.475 5.92c.42.36.81 1.096.81 2.22c0 1.606-.015 2.896-.015 3.286c0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":9},"\u003Cpath fill=\"currentColor\" d=\"M14.234 10.162L22.977 0h-2.072l-7.591 8.824L7.251 0H.258l9.168 13.343L.258 24H2.33l8.016-9.318L16.749 24h6.993zm-2.837 3.299l-.929-1.329L3.076 1.56h3.182l5.965 8.532l.929 1.329l7.754 11.09h-3.182z\"\u002F>",{"left":4,"top":11,"width":12,"height":12,"rotate":4,"vFlip":6,"hFlip":6,"body":13},32,448,"\u003Cpath fill=\"currentColor\" d=\"M416 32L31.9 32C14.3 32 0 46.5 0 64.3L0 447.7C0 465.5 14.3 480 31.9 480L416 480c17.6 0 32-14.5 32-32.3l0-383.4C448 46.5 433.6 32 416 32zM135.4 416l-66.4 0 0-213.8 66.5 0 0 213.8-.1 0zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77zM384.3 416l-66.4 0 0-104c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9l0 105.8-66.4 0 0-213.8 63.7 0 0 29.2 .9 0c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9l0 117.2z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":15},"\u003Cpath fill=\"currentColor\" d=\"M4.21 0A4.2 4.2 0 0 0 0 4.21v15.58A4.2 4.2 0 0 0 4.21 24h15.58A4.2 4.2 0 0 0 24 19.79v-1.093a5 5 0 0 1-.422.02c-2.577 0-4.027-2.146-4.09-4.832a8 8 0 0 1 .022-.708c.093-1.186.475-2.241 1.105-3.022a3.9 3.9 0 0 1 1.395-1.1c.468-.237 1.127-.367 1.664-.367h.023q.151 0 .303.01V4.211A4.2 4.2 0 0 0 19.79 0Zm.198 5.583h4.165l3.588 8.435l3.59-8.435h3.864v.146l-.019.004c-.705.16-1.063.397-1.063 1.254h-.003l.003 10.274c.06.676.424.885 1.063 1.03l.02.004v.145h-4.923v-.145l.019-.005c.639-.144.994-.353 1.054-1.03V7.267l-4.745 11.15h-.261L6.15 7.569v9.445c0 .857.358 1.094 1.063 1.253l.02.004v.147H4.405v-.147l.019-.004c.705-.16 1.065-.397 1.065-1.253V6.987c0-.857-.358-1.094-1.064-1.254l-.018-.004zm19.25 3.668c-1.086.023-1.733 1.323-1.813 3.124H24V9.298a1.4 1.4 0 0 0-.342-.047m-1.862 3.632c-.1 1.756.86 3.239 2.204 3.634v-3.634z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":17},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"m22 7l-8.991 5.727a2 2 0 0 1-2.009 0L2 7\"\u002F>\u003Crect width=\"20\" height=\"16\" x=\"2\" y=\"4\" rx=\"2\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":19},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Ccircle cx=\"12\" cy=\"12\" r=\"4\"\u002F>\u003Cpath d=\"M12 2v2m0 16v2M4.93 4.93l1.41 1.41m11.32 11.32l1.41 1.41M2 12h2m16 0h2M6.34 17.66l-1.41 1.41M19.07 4.93l-1.41 1.41\"\u002F>\u003C\u002Fg>",{"id":21,"title":22,"body":23,"description":1052,"example":1053,"extension":1054,"group":1055,"icon":1056,"meta":1057,"navigation":299,"order":238,"path":1058,"rawbody":1059,"seo":1060,"sitemap":1061,"source":1062,"stem":1063,"summary":1064,"__hash__":1065},"patterns\u002Flearn\u002Fdesign-patterns\u002Fbuilder.md","Builder",{"type":24,"value":25,"toc":1042},"minimark",[26,31,44,47,53,57,73,77,82,101,105,131,135,138,183,187,883,1029,1038],[27,28,30],"h2",{"id":29},"the-idea","The idea",[32,33,34,35,39,40,43],"p",{},"Some objects have many fields. A constructor that takes all of them, like ",[36,37,38],"code",{},"new Car(\"Toyota\", \"Camry\", 2018, \"Red\", true)",", is hard to read: which argument is the colour, and what does ",[36,41,42],{},"true"," mean? Adding a constructor for each combination of optional fields doesn't scale either.",[32,45,46],{},"The Builder pattern separates building an object from the object itself. You create a builder, call one method for each part, and finally ask it for the result. Each call names what it sets, so the code reads like a description of the object.",[32,48,49,50,52],{},"In the Gang of Four book, a director object runs the building steps against a builder interface, so the same steps can produce different representations. The example here uses the form common in Java code, described by Joshua Bloch in Effective Java: a static nested ",[36,51,22],{}," class whose methods return the builder itself, so the calls can be chained.",[27,54,56],{"id":55},"when-to-use-it","When to use it",[58,59,60,64,67,70],"ul",{},[61,62,63],"li",{},"A class has many fields, and several of them are optional.",[61,65,66],{},"You want the finished object to be immutable, but setting every field in one constructor call is hard to read.",[61,68,69],{},"The same building steps should produce different results, such as an HTML and a PDF version of one report.",[61,71,72],{},"Creating the object takes several steps that must happen in order.",[27,74,76],{"id":75},"pros-and-cons","Pros and cons",[78,79,81],"h3",{"id":80},"pros","Pros",[58,83,84,87,98],{},[61,85,86],{},"You build a complex object step by step, and each step is named.",[61,88,89,90,93,94,97],{},"The finished object can be immutable: ",[36,91,92],{},"Car"," has only ",[36,95,96],{},"final"," fields and no setters.",[61,99,100],{},"The same building process can be reused to create different representations.",[78,102,104],{"id":103},"cons","Cons",[58,106,107,110,113],{},[61,108,109],{},"It adds classes to the system: every class you build gets a builder.",[61,111,112],{},"The builder repeats every field of the class it builds, so adding a field means changing two places.",[61,114,115,116,119,120,122,123,126,127,130],{},"Without checks in ",[36,117,118],{},"build()",", you can create an object with missing fields. Here, a ",[36,121,92],{}," built without ",[36,124,125],{},"setMaker"," has a ",[36,128,129],{},"null"," maker.",[27,132,134],{"id":133},"the-example","The example",[32,136,137],{},"The example builds a car. A car has a maker, a model, a year, a colour and a gearbox type, and once built it should not change.",[58,139,140,151,172],{},[61,141,142,147,148,150],{},[143,144,145],"strong",{},[36,146,92],{},": the product. All its fields are ",[36,149,96],{},", and its constructor is package-private, so code outside the package has to go through the builder.",[61,152,153,158,159,161,162,165,166,168,169,171],{},[143,154,155],{},[36,156,157],{},"Car.Builder",": the builder. It holds the same fields as ",[36,160,92],{},", has one setter for each field that returns ",[36,163,164],{},"this",", and ",[36,167,118],{}," passes them all to the ",[36,170,92],{}," constructor.",[61,173,174,179,180,182],{},[143,175,176],{},[36,177,178],{},"Demo",": the client. It chains the setters and calls ",[36,181,118],{}," to get the car.",[27,184,186],{"id":185},"the-code","The code",[188,189,195],"pre",{"className":190,"code":191,"filename":192,"language":193,"meta":194,"style":194},"language-java shiki shiki-themes haytham-dark haytham-light","public class Car {\n    private final String maker;\n    private final String model;\n    private final int year;\n    private final String color;\n    private final boolean isAutomatic;\n\n    Car(String maker, String model, int year, String color, boolean isAutomatic) {\n        this.maker = maker;\n        this.model = model;\n        this.year = year;\n        this.color = color;\n        this.isAutomatic = isAutomatic;\n    }\n\n    public static class Builder {\n        private String maker;\n        private String model;\n        private int year;\n        private String color;\n        private boolean isAutomatic;\n\n        public Builder setMaker(String maker) {\n            this.maker = maker;\n\n            return this;\n        }\n\n        public Builder setModel(String model) {\n            this.model = model;\n\n            return this;\n        }\n\n        public Builder setYear(int year) {\n            this.year = year;\n\n            return this;\n        }\n\n        public Builder setColor(String color) {\n            this.color = color;\n\n            return this;\n        }\n\n        public Builder setAutomatic(boolean isAutomatic) {\n            this.isAutomatic = isAutomatic;\n\n            return this;\n        }\n\n        public Car build() {\n            return new Car(maker, model, year, color, isAutomatic);\n        }\n    }\n}\n","Car.java","java","",[36,196,197,217,236,250,265,279,294,301,346,367,383,399,415,431,437,442,458,470,481,492,503,514,519,540,555,560,571,577,582,602,617,622,630,635,640,661,676,681,690,695,700,720,735,740,749,754,759,780,795,800,809,814,819,834,867,872,877],{"__ignoreMap":194},[198,199,202,206,209,213],"span",{"class":200,"line":201},"line",1,[198,203,205],{"class":204},"sVsk0","public",[198,207,208],{"class":204}," class",[198,210,212],{"class":211},"sNwy7"," Car",[198,214,216],{"class":215},"sVG6I"," {\n",[198,218,220,223,226,229,233],{"class":200,"line":219},2,[198,221,222],{"class":204},"    private",[198,224,225],{"class":204}," final",[198,227,228],{"class":204}," String",[198,230,232],{"class":231},"sWFPS"," maker",[198,234,235],{"class":215},";\n",[198,237,239,241,243,245,248],{"class":200,"line":238},3,[198,240,222],{"class":204},[198,242,225],{"class":204},[198,244,228],{"class":204},[198,246,247],{"class":231}," model",[198,249,235],{"class":215},[198,251,253,255,257,260,263],{"class":200,"line":252},4,[198,254,222],{"class":204},[198,256,225],{"class":204},[198,258,259],{"class":204}," int",[198,261,262],{"class":231}," year",[198,264,235],{"class":215},[198,266,268,270,272,274,277],{"class":200,"line":267},5,[198,269,222],{"class":204},[198,271,225],{"class":204},[198,273,228],{"class":204},[198,275,276],{"class":231}," color",[198,278,235],{"class":215},[198,280,282,284,286,289,292],{"class":200,"line":281},6,[198,283,222],{"class":204},[198,285,225],{"class":204},[198,287,288],{"class":204}," boolean",[198,290,291],{"class":231}," isAutomatic",[198,293,235],{"class":215},[198,295,297],{"class":200,"line":296},7,[198,298,300],{"emptyLinePlaceholder":299},true,"\n",[198,302,304,308,311,314,316,319,321,323,325,327,329,331,333,335,337,339,341,344],{"class":200,"line":303},8,[198,305,307],{"class":306},"sVz7n","    Car",[198,309,310],{"class":215},"(",[198,312,313],{"class":204},"String",[198,315,232],{"class":231},[198,317,318],{"class":215},",",[198,320,228],{"class":204},[198,322,247],{"class":231},[198,324,318],{"class":215},[198,326,259],{"class":204},[198,328,262],{"class":231},[198,330,318],{"class":215},[198,332,228],{"class":204},[198,334,276],{"class":231},[198,336,318],{"class":215},[198,338,288],{"class":204},[198,340,291],{"class":231},[198,342,343],{"class":215},")",[198,345,216],{"class":215},[198,347,349,353,356,360,363,365],{"class":200,"line":348},9,[198,350,352],{"class":351},"sheII","        this",[198,354,355],{"class":215},".",[198,357,359],{"class":358},"s4Awn","maker",[198,361,362],{"class":215}," =",[198,364,232],{"class":231},[198,366,235],{"class":215},[198,368,370,372,374,377,379,381],{"class":200,"line":369},10,[198,371,352],{"class":351},[198,373,355],{"class":215},[198,375,376],{"class":358},"model",[198,378,362],{"class":215},[198,380,247],{"class":231},[198,382,235],{"class":215},[198,384,386,388,390,393,395,397],{"class":200,"line":385},11,[198,387,352],{"class":351},[198,389,355],{"class":215},[198,391,392],{"class":358},"year",[198,394,362],{"class":215},[198,396,262],{"class":231},[198,398,235],{"class":215},[198,400,402,404,406,409,411,413],{"class":200,"line":401},12,[198,403,352],{"class":351},[198,405,355],{"class":215},[198,407,408],{"class":358},"color",[198,410,362],{"class":215},[198,412,276],{"class":231},[198,414,235],{"class":215},[198,416,418,420,422,425,427,429],{"class":200,"line":417},13,[198,419,352],{"class":351},[198,421,355],{"class":215},[198,423,424],{"class":358},"isAutomatic",[198,426,362],{"class":215},[198,428,291],{"class":231},[198,430,235],{"class":215},[198,432,434],{"class":200,"line":433},14,[198,435,436],{"class":215},"    }\n",[198,438,440],{"class":200,"line":439},15,[198,441,300],{"emptyLinePlaceholder":299},[198,443,445,448,451,453,456],{"class":200,"line":444},16,[198,446,447],{"class":204},"    public",[198,449,450],{"class":204}," static",[198,452,208],{"class":204},[198,454,455],{"class":211}," Builder",[198,457,216],{"class":215},[198,459,461,464,466,468],{"class":200,"line":460},17,[198,462,463],{"class":204},"        private",[198,465,228],{"class":204},[198,467,232],{"class":231},[198,469,235],{"class":215},[198,471,473,475,477,479],{"class":200,"line":472},18,[198,474,463],{"class":204},[198,476,228],{"class":204},[198,478,247],{"class":231},[198,480,235],{"class":215},[198,482,484,486,488,490],{"class":200,"line":483},19,[198,485,463],{"class":204},[198,487,259],{"class":204},[198,489,262],{"class":231},[198,491,235],{"class":215},[198,493,495,497,499,501],{"class":200,"line":494},20,[198,496,463],{"class":204},[198,498,228],{"class":204},[198,500,276],{"class":231},[198,502,235],{"class":215},[198,504,506,508,510,512],{"class":200,"line":505},21,[198,507,463],{"class":204},[198,509,288],{"class":204},[198,511,291],{"class":231},[198,513,235],{"class":215},[198,515,517],{"class":200,"line":516},22,[198,518,300],{"emptyLinePlaceholder":299},[198,520,522,525,527,530,532,534,536,538],{"class":200,"line":521},23,[198,523,524],{"class":204},"        public",[198,526,455],{"class":204},[198,528,529],{"class":306}," setMaker",[198,531,310],{"class":215},[198,533,313],{"class":204},[198,535,232],{"class":231},[198,537,343],{"class":215},[198,539,216],{"class":215},[198,541,542,545,547,549,551,553],{"class":200,"line":5},[198,543,544],{"class":351},"            this",[198,546,355],{"class":215},[198,548,359],{"class":358},[198,550,362],{"class":215},[198,552,232],{"class":231},[198,554,235],{"class":215},[198,556,558],{"class":200,"line":557},25,[198,559,300],{"emptyLinePlaceholder":299},[198,561,563,566,569],{"class":200,"line":562},26,[198,564,565],{"class":204},"            return",[198,567,568],{"class":351}," this",[198,570,235],{"class":215},[198,572,574],{"class":200,"line":573},27,[198,575,576],{"class":215},"        }\n",[198,578,580],{"class":200,"line":579},28,[198,581,300],{"emptyLinePlaceholder":299},[198,583,585,587,589,592,594,596,598,600],{"class":200,"line":584},29,[198,586,524],{"class":204},[198,588,455],{"class":204},[198,590,591],{"class":306}," setModel",[198,593,310],{"class":215},[198,595,313],{"class":204},[198,597,247],{"class":231},[198,599,343],{"class":215},[198,601,216],{"class":215},[198,603,605,607,609,611,613,615],{"class":200,"line":604},30,[198,606,544],{"class":351},[198,608,355],{"class":215},[198,610,376],{"class":358},[198,612,362],{"class":215},[198,614,247],{"class":231},[198,616,235],{"class":215},[198,618,620],{"class":200,"line":619},31,[198,621,300],{"emptyLinePlaceholder":299},[198,623,624,626,628],{"class":200,"line":11},[198,625,565],{"class":204},[198,627,568],{"class":351},[198,629,235],{"class":215},[198,631,633],{"class":200,"line":632},33,[198,634,576],{"class":215},[198,636,638],{"class":200,"line":637},34,[198,639,300],{"emptyLinePlaceholder":299},[198,641,643,645,647,650,652,655,657,659],{"class":200,"line":642},35,[198,644,524],{"class":204},[198,646,455],{"class":204},[198,648,649],{"class":306}," setYear",[198,651,310],{"class":215},[198,653,654],{"class":204},"int",[198,656,262],{"class":231},[198,658,343],{"class":215},[198,660,216],{"class":215},[198,662,664,666,668,670,672,674],{"class":200,"line":663},36,[198,665,544],{"class":351},[198,667,355],{"class":215},[198,669,392],{"class":358},[198,671,362],{"class":215},[198,673,262],{"class":231},[198,675,235],{"class":215},[198,677,679],{"class":200,"line":678},37,[198,680,300],{"emptyLinePlaceholder":299},[198,682,684,686,688],{"class":200,"line":683},38,[198,685,565],{"class":204},[198,687,568],{"class":351},[198,689,235],{"class":215},[198,691,693],{"class":200,"line":692},39,[198,694,576],{"class":215},[198,696,698],{"class":200,"line":697},40,[198,699,300],{"emptyLinePlaceholder":299},[198,701,703,705,707,710,712,714,716,718],{"class":200,"line":702},41,[198,704,524],{"class":204},[198,706,455],{"class":204},[198,708,709],{"class":306}," setColor",[198,711,310],{"class":215},[198,713,313],{"class":204},[198,715,276],{"class":231},[198,717,343],{"class":215},[198,719,216],{"class":215},[198,721,723,725,727,729,731,733],{"class":200,"line":722},42,[198,724,544],{"class":351},[198,726,355],{"class":215},[198,728,408],{"class":358},[198,730,362],{"class":215},[198,732,276],{"class":231},[198,734,235],{"class":215},[198,736,738],{"class":200,"line":737},43,[198,739,300],{"emptyLinePlaceholder":299},[198,741,743,745,747],{"class":200,"line":742},44,[198,744,565],{"class":204},[198,746,568],{"class":351},[198,748,235],{"class":215},[198,750,752],{"class":200,"line":751},45,[198,753,576],{"class":215},[198,755,757],{"class":200,"line":756},46,[198,758,300],{"emptyLinePlaceholder":299},[198,760,762,764,766,769,771,774,776,778],{"class":200,"line":761},47,[198,763,524],{"class":204},[198,765,455],{"class":204},[198,767,768],{"class":306}," setAutomatic",[198,770,310],{"class":215},[198,772,773],{"class":204},"boolean",[198,775,291],{"class":231},[198,777,343],{"class":215},[198,779,216],{"class":215},[198,781,783,785,787,789,791,793],{"class":200,"line":782},48,[198,784,544],{"class":351},[198,786,355],{"class":215},[198,788,424],{"class":358},[198,790,362],{"class":215},[198,792,291],{"class":231},[198,794,235],{"class":215},[198,796,798],{"class":200,"line":797},49,[198,799,300],{"emptyLinePlaceholder":299},[198,801,803,805,807],{"class":200,"line":802},50,[198,804,565],{"class":204},[198,806,568],{"class":351},[198,808,235],{"class":215},[198,810,812],{"class":200,"line":811},51,[198,813,576],{"class":215},[198,815,817],{"class":200,"line":816},52,[198,818,300],{"emptyLinePlaceholder":299},[198,820,822,824,826,829,832],{"class":200,"line":821},53,[198,823,524],{"class":204},[198,825,212],{"class":204},[198,827,828],{"class":306}," build",[198,830,831],{"class":215},"()",[198,833,216],{"class":215},[198,835,837,839,842,844,846,848,850,852,854,856,858,860,862,864],{"class":200,"line":836},54,[198,838,565],{"class":204},[198,840,841],{"class":204}," new",[198,843,212],{"class":306},[198,845,310],{"class":215},[198,847,359],{"class":231},[198,849,318],{"class":215},[198,851,247],{"class":231},[198,853,318],{"class":215},[198,855,262],{"class":231},[198,857,318],{"class":215},[198,859,276],{"class":231},[198,861,318],{"class":215},[198,863,291],{"class":231},[198,865,866],{"class":215},");\n",[198,868,870],{"class":200,"line":869},55,[198,871,576],{"class":215},[198,873,875],{"class":200,"line":874},56,[198,876,436],{"class":215},[198,878,880],{"class":200,"line":879},57,[198,881,882],{"class":215},"}\n",[188,884,887],{"className":190,"code":885,"filename":886,"language":193,"meta":194,"style":194},"public class Demo {\n    public static void main(String[] args) {\n        new Car.Builder()\n                .setMaker(\"Toyota\")\n                .setModel(\"Camry\")\n                .setYear(2018)\n                .setColor(\"Red\")\n                .setAutomatic(true)\n                .build();\n    }\n}\n","Demo.java",[36,888,889,900,926,940,956,970,984,998,1011,1021,1025],{"__ignoreMap":194},[198,890,891,893,895,898],{"class":200,"line":201},[198,892,205],{"class":204},[198,894,208],{"class":204},[198,896,897],{"class":211}," Demo",[198,899,216],{"class":215},[198,901,902,904,906,909,912,914,916,919,922,924],{"class":200,"line":219},[198,903,447],{"class":204},[198,905,450],{"class":204},[198,907,908],{"class":204}," void",[198,910,911],{"class":306}," main",[198,913,310],{"class":215},[198,915,313],{"class":204},[198,917,918],{"class":215},"[]",[198,920,921],{"class":231}," args",[198,923,343],{"class":215},[198,925,216],{"class":215},[198,927,928,931,933,935,937],{"class":200,"line":238},[198,929,930],{"class":204},"        new",[198,932,212],{"class":231},[198,934,355],{"class":215},[198,936,22],{"class":306},[198,938,939],{"class":215},"()\n",[198,941,942,945,947,949,953],{"class":200,"line":252},[198,943,944],{"class":215},"                .",[198,946,125],{"class":306},[198,948,310],{"class":215},[198,950,952],{"class":951},"sUOzB","\"Toyota\"",[198,954,955],{"class":215},")\n",[198,957,958,960,963,965,968],{"class":200,"line":267},[198,959,944],{"class":215},[198,961,962],{"class":306},"setModel",[198,964,310],{"class":215},[198,966,967],{"class":951},"\"Camry\"",[198,969,955],{"class":215},[198,971,972,974,977,979,982],{"class":200,"line":281},[198,973,944],{"class":215},[198,975,976],{"class":306},"setYear",[198,978,310],{"class":215},[198,980,981],{"class":351},"2018",[198,983,955],{"class":215},[198,985,986,988,991,993,996],{"class":200,"line":296},[198,987,944],{"class":215},[198,989,990],{"class":306},"setColor",[198,992,310],{"class":215},[198,994,995],{"class":951},"\"Red\"",[198,997,955],{"class":215},[198,999,1000,1002,1005,1007,1009],{"class":200,"line":303},[198,1001,944],{"class":215},[198,1003,1004],{"class":306},"setAutomatic",[198,1006,310],{"class":215},[198,1008,42],{"class":351},[198,1010,955],{"class":215},[198,1012,1013,1015,1018],{"class":200,"line":348},[198,1014,944],{"class":215},[198,1016,1017],{"class":306},"build",[198,1019,1020],{"class":215},"();\n",[198,1022,1023],{"class":200,"line":369},[198,1024,436],{"class":215},[198,1026,1027],{"class":200,"line":385},[198,1028,882],{"class":215},[32,1030,1031,1032,1034,1035,1037],{},"Running ",[36,1033,178],{}," prints nothing: it builds the car and stops there. ",[36,1036,92],{}," has no getters, so there is nothing to show.",[1039,1040,1041],"style",{},"html pre.shiki code .sVsk0, html code.shiki .sVsk0{--shiki-default:#9D9FFE;--shiki-light:#6762D3}html pre.shiki code .sNwy7, html code.shiki .sNwy7{--shiki-default:#D4D7FE;--shiki-light:#3F3B87}html pre.shiki code .sVG6I, html code.shiki .sVG6I{--shiki-default:#85858F;--shiki-light:#71717A}html pre.shiki code .sWFPS, html code.shiki .sWFPS{--shiki-default:#C4C4CC;--shiki-light:#3F3F46}html pre.shiki code .sVz7n, html code.shiki .sVz7n{--shiki-default:#EDEDEF;--shiki-light:#0B0B0C}html pre.shiki code .sheII, html code.shiki .sheII{--shiki-default:#70ADFF;--shiki-light:#195CAE}html pre.shiki code .s4Awn, html code.shiki .s4Awn{--shiki-default:#C5DDFE;--shiki-light:#534DAF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html pre.shiki code .sUOzB, html code.shiki .sUOzB{--shiki-default:#9EC7FE;--shiki-light:#2873D1}",{"title":194,"searchDepth":219,"depth":219,"links":1043},[1044,1045,1046,1050,1051],{"id":29,"depth":219,"text":30},{"id":55,"depth":219,"text":56},{"id":75,"depth":219,"text":76,"children":1047},[1048,1049],{"id":80,"depth":238,"text":81},{"id":103,"depth":238,"text":104},{"id":133,"depth":219,"text":134},{"id":185,"depth":219,"text":186},"The Builder pattern constructs a complex object step by step, so you set only the parts you need and get back a finished object that does not change. Explained with a worked Java example of a car.","A car with maker, model, year, colour and gearbox","md","Creational","lucide:hammer",{},"\u002Flearn\u002Fdesign-patterns\u002Fbuilder","---\ntitle: Builder\ngroup: Creational\nicon: lucide:hammer\nsummary: 'Build a complex object step by step, setting only the parts you need, then get the finished object in one call.'\ndescription: 'The Builder pattern constructs a complex object step by step, so you set only the parts you need and get back a finished object that does not change. Explained with a worked Java example of a car.'\nexample: 'A car with maker, model, year, colour and gearbox'\norder: 3\nsource: https:\u002F\u002Fgithub.com\u002FHaythamasalama\u002FdesignPattern\u002Ftree\u002Fmaster\u002Fsrc\u002FCreational\u002FBuilder\n---\n\n## The idea\n\nSome objects have many fields. A constructor that takes all of them, like `new Car(\"Toyota\", \"Camry\", 2018, \"Red\", true)`, is hard to read: which argument is the colour, and what does `true` mean? Adding a constructor for each combination of optional fields doesn't scale either.\n\nThe Builder pattern separates building an object from the object itself. You create a builder, call one method for each part, and finally ask it for the result. Each call names what it sets, so the code reads like a description of the object.\n\nIn the Gang of Four book, a director object runs the building steps against a builder interface, so the same steps can produce different representations. The example here uses the form common in Java code, described by Joshua Bloch in Effective Java: a static nested `Builder` class whose methods return the builder itself, so the calls can be chained.\n\n## When to use it\n\n- A class has many fields, and several of them are optional.\n- You want the finished object to be immutable, but setting every field in one constructor call is hard to read.\n- The same building steps should produce different results, such as an HTML and a PDF version of one report.\n- Creating the object takes several steps that must happen in order.\n\n## Pros and cons\n\n### Pros\n\n- You build a complex object step by step, and each step is named.\n- The finished object can be immutable: `Car` has only `final` fields and no setters.\n- The same building process can be reused to create different representations.\n\n### Cons\n\n- It adds classes to the system: every class you build gets a builder.\n- The builder repeats every field of the class it builds, so adding a field means changing two places.\n- Without checks in `build()`, you can create an object with missing fields. Here, a `Car` built without `setMaker` has a `null` maker.\n\n## The example\n\nThe example builds a car. A car has a maker, a model, a year, a colour and a gearbox type, and once built it should not change.\n\n- **`Car`**: the product. All its fields are `final`, and its constructor is package-private, so code outside the package has to go through the builder.\n- **`Car.Builder`**: the builder. It holds the same fields as `Car`, has one setter for each field that returns `this`, and `build()` passes them all to the `Car` constructor.\n- **`Demo`**: the client. It chains the setters and calls `build()` to get the car.\n\n## The code\n\n```java [Car.java]\npublic class Car {\n    private final String maker;\n    private final String model;\n    private final int year;\n    private final String color;\n    private final boolean isAutomatic;\n\n    Car(String maker, String model, int year, String color, boolean isAutomatic) {\n        this.maker = maker;\n        this.model = model;\n        this.year = year;\n        this.color = color;\n        this.isAutomatic = isAutomatic;\n    }\n\n    public static class Builder {\n        private String maker;\n        private String model;\n        private int year;\n        private String color;\n        private boolean isAutomatic;\n\n        public Builder setMaker(String maker) {\n            this.maker = maker;\n\n            return this;\n        }\n\n        public Builder setModel(String model) {\n            this.model = model;\n\n            return this;\n        }\n\n        public Builder setYear(int year) {\n            this.year = year;\n\n            return this;\n        }\n\n        public Builder setColor(String color) {\n            this.color = color;\n\n            return this;\n        }\n\n        public Builder setAutomatic(boolean isAutomatic) {\n            this.isAutomatic = isAutomatic;\n\n            return this;\n        }\n\n        public Car build() {\n            return new Car(maker, model, year, color, isAutomatic);\n        }\n    }\n}\n```\n\n```java [Demo.java]\npublic class Demo {\n    public static void main(String[] args) {\n        new Car.Builder()\n                .setMaker(\"Toyota\")\n                .setModel(\"Camry\")\n                .setYear(2018)\n                .setColor(\"Red\")\n                .setAutomatic(true)\n                .build();\n    }\n}\n```\n\nRunning `Demo` prints nothing: it builds the car and stops there. `Car` has no getters, so there is nothing to show.\n",{"title":22,"description":1052},{"loc":1058},"https:\u002F\u002Fgithub.com\u002FHaythamasalama\u002FdesignPattern\u002Ftree\u002Fmaster\u002Fsrc\u002FCreational\u002FBuilder","learn\u002Fdesign-patterns\u002Fbuilder","Build a complex object step by step, setting only the parts you need, then get the finished object in one call.","Jvuzu_gQ_52kQulJdnDXqLuhdvq1yZWoRWsOc18tf0M",[1067,1072,1077,1078],{"path":1068,"title":1069,"icon":1070,"summary":1071,"order":201},"\u002Flearn\u002Fdesign-patterns\u002Fsingleton","Singleton","lucide:circle-dot","A class that makes sure only one instance of itself exists, and hands everyone that same instance.",{"path":1073,"title":1074,"icon":1075,"summary":1076,"order":219},"\u002Flearn\u002Fdesign-patterns\u002Ffactory","Factory","lucide:factory","One place decides which concrete class to create, so the rest of the code only asks for what it needs.",{"path":1058,"title":22,"icon":1056,"summary":1064,"order":238},{"path":1079,"title":1080,"icon":1081,"summary":1082,"order":252},"\u002Flearn\u002Fdesign-patterns\u002Fprototype","Prototype","lucide:copy","Create a new object by copying an existing one, then change only what differs.",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1084},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M15 14c.2-1 .7-1.7 1.5-2.5c1-.9 1.5-2.2 1.5-3.5A6 6 0 0 0 6 8c0 1 .2 2.2 1.5 3.5c.7.7 1.3 1.5 1.5 2.5m0 4h6m-5 4h4\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1086},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"m21.64 3.64l-1.28-1.28a1.21 1.21 0 0 0-1.72 0L2.36 18.64a1.21 1.21 0 0 0 0 1.72l1.28 1.28a1.2 1.2 0 0 0 1.72 0L21.64 5.36a1.2 1.2 0 0 0 0-1.72M14 7l3 3M5 6v4m14 4v4M10 2v2M7 8H3m18 8h-4M11 3H9\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1088},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Ccircle cx=\"12\" cy=\"12\" r=\"1\"\u002F>\u003Ccircle cx=\"12\" cy=\"12\" r=\"10\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1090},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M12 16h.01M16 16h.01M3 19a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V8.5a.5.5 0 0 0-.769-.422l-4.462 2.844A.5.5 0 0 1 15 10.5v-2a.5.5 0 0 0-.769-.422L9.77 10.922A.5.5 0 0 1 9 10.5V5a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2zm5-3h.01\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1092},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"m15 12l-9.373 9.373a1 1 0 0 1-3.001-3L12 9m6 6l4-4\"\u002F>\u003Cpath d=\"m21.5 11.5l-1.914-1.914A2 2 0 0 1 19 8.172v-.344a2 2 0 0 0-.586-1.414l-1.657-1.657A6 6 0 0 0 12.516 3H9l1.243 1.243A6 6 0 0 1 12 8.485V10l2 2h1.172a2 2 0 0 1 1.414.586L18.5 14.5\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1094},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Crect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"\u002F>\u003Cpath d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"\u002F>\u003C\u002Fg>",1793131416410]