[{"data":1,"prerenderedAt":1236},["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\u002Ffactory":20,"pattern-group:\u002Flearn\u002Fdesign-patterns\u002Ffactory":1207,"i-lucide:lightbulb":1224,"i-lucide:wand-sparkles":1226,"i-lucide:circle-dot":1228,"i-lucide:factory":1230,"i-lucide:hammer":1232,"i-lucide:copy":1234},{"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":1193,"example":1194,"extension":1195,"group":1196,"icon":1197,"meta":1198,"navigation":273,"order":257,"path":1199,"rawbody":1200,"seo":1201,"sitemap":1202,"source":1203,"stem":1204,"summary":1205,"__hash__":1206},"patterns\u002Flearn\u002Fdesign-patterns\u002Ffactory.md","Factory",{"type":24,"value":25,"toc":1183},"minimark",[26,31,56,63,66,70,89,93,98,109,113,143,147,150,221,225,321,548,747,871,1056,1165,1171,1179],[27,28,30],"h2",{"id":29},"the-idea","The idea",[32,33,34,35,39,40,43,44,47,48,51,52,55],"p",{},"Code that calls ",[36,37,38],"code",{},"new"," on a concrete class is tied to that class. If a shop's ordering code says ",[36,41,42],{},"new MargheritaPizza()"," and ",[36,45,46],{},"new PepperoniPizza()",", every new pizza means editing the ordering code, and the same ",[36,49,50],{},"if"," or ",[36,53,54],{},"switch"," tends to appear in several places.",[32,57,58,59,62],{},"A factory moves that decision into one object. The client asks the factory for \"a pepperoni pizza\", and the factory decides which class to create. The client only knows the common interface, ",[36,60,61],{},"Pizza",", so it works with any pizza the factory can make.",[32,64,65],{},"The Gang of Four book describes two related patterns. In Factory Method, subclasses decide which class to create. Abstract Factory creates families of related objects. The example here is the simpler form both build on, often called a simple factory: one class with one method that picks the concrete class.",[27,67,69],{"id":68},"when-to-use-it","When to use it",[71,72,73,77,80,83],"ul",{},[74,75,76],"li",{},"You don't know until runtime which concrete class you need, for example when it comes from user input or a config file.",[74,78,79],{},"The same creation logic would otherwise be repeated in several places.",[74,81,82],{},"You want to add new types without changing the code that uses them.",[74,84,85,86,88],{},"Creating an object takes more than a call to ",[36,87,38],{},", such as setting defaults or looking something up first.",[27,90,92],{"id":91},"pros-and-cons","Pros and cons",[94,95,97],"h3",{"id":96},"pros","Pros",[71,99,100,103,106],{},[74,101,102],{},"Client code creates objects without naming their concrete classes.",[74,104,105],{},"Adding a new type is easy: write the class and add one case to the factory.",[74,107,108],{},"Creation logic lives in one place, so it is easier to change and to test.",[94,110,112],{"id":111},"cons","Cons",[71,114,115,118,124],{},[74,116,117],{},"It adds classes to the system: an interface, a factory and one class per type.",[74,119,120,121,123],{},"A simple factory still has a ",[36,122,54],{}," that must change for every new type. Factory Method avoids this by moving the choice into subclasses.",[74,125,126,127,130,131,134,135,138,139,142],{},"The client has to handle types the factory doesn't know. Here, ",[36,128,129],{},"createPizza"," returns ",[36,132,133],{},"null"," for an unknown name, and ",[36,136,137],{},"orderPizza"," then fails with a ",[36,140,141],{},"NullPointerException",".",[27,144,146],{"id":145},"the-example","The example",[32,148,149],{},"The example is a pizza shop. The store takes an order by name, asks a factory for the matching pizza, and then runs the same four steps on it, whatever kind it is.",[71,151,152,173,186,202,213],{},[74,153,154,159,160,163,164,163,167,43,170,142],{},[155,156,157],"strong",{},[36,158,61],{},": the product interface: ",[36,161,162],{},"prepare",", ",[36,165,166],{},"bake",[36,168,169],{},"cut",[36,171,172],{},"box",[74,174,175,43,180,185],{},[155,176,177],{},[36,178,179],{},"MargheritaPizza",[155,181,182],{},[36,183,184],{},"PepperoniPizza",": the concrete products.",[74,187,188,193,194,196,197,199,200,142],{},[155,189,190],{},[36,191,192],{},"PizzaFactory",": the factory. ",[36,195,129],{}," turns a name into the right ",[36,198,61],{}," with a ",[36,201,54],{},[74,203,204,209,210,212],{},[155,205,206],{},[36,207,208],{},"PizzaStore",": the client. It receives the factory in its constructor and only ever works with the ",[36,211,61],{}," interface.",[74,214,215,220],{},[155,216,217],{},[36,218,219],{},"Demo",": builds the store and orders one pizza of each kind.",[27,222,224],{"id":223},"the-code","The code",[226,227,233],"pre",{"className":228,"code":229,"filename":230,"language":231,"meta":232,"style":232},"language-java shiki shiki-themes haytham-dark haytham-light","public interface Pizza {\n    void prepare();\n\n    void bake();\n\n    void cut();\n\n    void box();\n}\n","Pizza.java","java","",[36,234,235,255,268,275,285,290,300,305,315],{"__ignoreMap":232},[236,237,240,244,247,251],"span",{"class":238,"line":239},"line",1,[236,241,243],{"class":242},"sVsk0","public",[236,245,246],{"class":242}," interface",[236,248,250],{"class":249},"sNwy7"," Pizza",[236,252,254],{"class":253},"sVG6I"," {\n",[236,256,258,261,265],{"class":238,"line":257},2,[236,259,260],{"class":242},"    void",[236,262,264],{"class":263},"sVz7n"," prepare",[236,266,267],{"class":253},"();\n",[236,269,271],{"class":238,"line":270},3,[236,272,274],{"emptyLinePlaceholder":273},true,"\n",[236,276,278,280,283],{"class":238,"line":277},4,[236,279,260],{"class":242},[236,281,282],{"class":263}," bake",[236,284,267],{"class":253},[236,286,288],{"class":238,"line":287},5,[236,289,274],{"emptyLinePlaceholder":273},[236,291,293,295,298],{"class":238,"line":292},6,[236,294,260],{"class":242},[236,296,297],{"class":263}," cut",[236,299,267],{"class":253},[236,301,303],{"class":238,"line":302},7,[236,304,274],{"emptyLinePlaceholder":273},[236,306,308,310,313],{"class":238,"line":307},8,[236,309,260],{"class":242},[236,311,312],{"class":263}," box",[236,314,267],{"class":253},[236,316,318],{"class":238,"line":317},9,[236,319,320],{"class":253},"}\n",[226,322,325],{"className":228,"code":323,"filename":324,"language":231,"meta":232,"style":232},"class MargheritaPizza implements Pizza {\n    @Override\n    public void prepare() {\n        System.out.println(\"Preparing Margherita Pizza...\");\n    }\n\n    @Override\n    public void bake() {\n        System.out.println(\"Baking Margherita Pizza...\");\n    }\n\n    @Override\n    public void cut() {\n        System.out.println(\"Cutting Margherita Pizza...\");\n    }\n\n    @Override\n    public void box() {\n        System.out.println(\"Boxing Margherita Pizza...\");\n    }\n}\n","MargheritaPizza.java",[36,326,327,342,350,365,392,397,401,407,419,438,443,448,455,468,488,493,498,505,518,538,543],{"__ignoreMap":232},[236,328,329,332,335,338,340],{"class":238,"line":239},[236,330,331],{"class":242},"class",[236,333,334],{"class":249}," MargheritaPizza",[236,336,337],{"class":242}," implements",[236,339,250],{"class":249},[236,341,254],{"class":253},[236,343,344,347],{"class":238,"line":257},[236,345,346],{"class":253},"    @",[236,348,349],{"class":242},"Override\n",[236,351,352,355,358,360,363],{"class":238,"line":270},[236,353,354],{"class":242},"    public",[236,356,357],{"class":242}," void",[236,359,264],{"class":263},[236,361,362],{"class":253},"()",[236,364,254],{"class":253},[236,366,367,371,373,377,379,382,385,389],{"class":238,"line":277},[236,368,370],{"class":369},"sWFPS","        System",[236,372,142],{"class":253},[236,374,376],{"class":375},"s4Awn","out",[236,378,142],{"class":253},[236,380,381],{"class":263},"println",[236,383,384],{"class":253},"(",[236,386,388],{"class":387},"sUOzB","\"Preparing Margherita Pizza...\"",[236,390,391],{"class":253},");\n",[236,393,394],{"class":238,"line":287},[236,395,396],{"class":253},"    }\n",[236,398,399],{"class":238,"line":292},[236,400,274],{"emptyLinePlaceholder":273},[236,402,403,405],{"class":238,"line":302},[236,404,346],{"class":253},[236,406,349],{"class":242},[236,408,409,411,413,415,417],{"class":238,"line":307},[236,410,354],{"class":242},[236,412,357],{"class":242},[236,414,282],{"class":263},[236,416,362],{"class":253},[236,418,254],{"class":253},[236,420,421,423,425,427,429,431,433,436],{"class":238,"line":317},[236,422,370],{"class":369},[236,424,142],{"class":253},[236,426,376],{"class":375},[236,428,142],{"class":253},[236,430,381],{"class":263},[236,432,384],{"class":253},[236,434,435],{"class":387},"\"Baking Margherita Pizza...\"",[236,437,391],{"class":253},[236,439,441],{"class":238,"line":440},10,[236,442,396],{"class":253},[236,444,446],{"class":238,"line":445},11,[236,447,274],{"emptyLinePlaceholder":273},[236,449,451,453],{"class":238,"line":450},12,[236,452,346],{"class":253},[236,454,349],{"class":242},[236,456,458,460,462,464,466],{"class":238,"line":457},13,[236,459,354],{"class":242},[236,461,357],{"class":242},[236,463,297],{"class":263},[236,465,362],{"class":253},[236,467,254],{"class":253},[236,469,471,473,475,477,479,481,483,486],{"class":238,"line":470},14,[236,472,370],{"class":369},[236,474,142],{"class":253},[236,476,376],{"class":375},[236,478,142],{"class":253},[236,480,381],{"class":263},[236,482,384],{"class":253},[236,484,485],{"class":387},"\"Cutting Margherita Pizza...\"",[236,487,391],{"class":253},[236,489,491],{"class":238,"line":490},15,[236,492,396],{"class":253},[236,494,496],{"class":238,"line":495},16,[236,497,274],{"emptyLinePlaceholder":273},[236,499,501,503],{"class":238,"line":500},17,[236,502,346],{"class":253},[236,504,349],{"class":242},[236,506,508,510,512,514,516],{"class":238,"line":507},18,[236,509,354],{"class":242},[236,511,357],{"class":242},[236,513,312],{"class":263},[236,515,362],{"class":253},[236,517,254],{"class":253},[236,519,521,523,525,527,529,531,533,536],{"class":238,"line":520},19,[236,522,370],{"class":369},[236,524,142],{"class":253},[236,526,376],{"class":375},[236,528,142],{"class":253},[236,530,381],{"class":263},[236,532,384],{"class":253},[236,534,535],{"class":387},"\"Boxing Margherita Pizza...\"",[236,537,391],{"class":253},[236,539,541],{"class":238,"line":540},20,[236,542,396],{"class":253},[236,544,546],{"class":238,"line":545},21,[236,547,320],{"class":253},[226,549,552],{"className":228,"code":550,"filename":551,"language":231,"meta":232,"style":232},"class PepperoniPizza implements Pizza {\n    @Override\n    public void prepare() {\n        System.out.println(\"Preparing Pepperoni  Pizza...\");\n    }\n\n    @Override\n    public void bake() {\n        System.out.println(\"Baking Pepperoni  Pizza...\");\n    }\n\n    @Override\n    public void cut() {\n        System.out.println(\"Cutting Pepperoni  Pizza...\");\n    }\n\n    @Override\n    public void box() {\n        System.out.println(\"Boxing Pepperoni  Pizza...\");\n    }\n}\n","PepperoniPizza.java",[36,553,554,567,573,585,604,608,612,618,630,649,653,657,663,675,694,698,702,708,720,739,743],{"__ignoreMap":232},[236,555,556,558,561,563,565],{"class":238,"line":239},[236,557,331],{"class":242},[236,559,560],{"class":249}," PepperoniPizza",[236,562,337],{"class":242},[236,564,250],{"class":249},[236,566,254],{"class":253},[236,568,569,571],{"class":238,"line":257},[236,570,346],{"class":253},[236,572,349],{"class":242},[236,574,575,577,579,581,583],{"class":238,"line":270},[236,576,354],{"class":242},[236,578,357],{"class":242},[236,580,264],{"class":263},[236,582,362],{"class":253},[236,584,254],{"class":253},[236,586,587,589,591,593,595,597,599,602],{"class":238,"line":277},[236,588,370],{"class":369},[236,590,142],{"class":253},[236,592,376],{"class":375},[236,594,142],{"class":253},[236,596,381],{"class":263},[236,598,384],{"class":253},[236,600,601],{"class":387},"\"Preparing Pepperoni  Pizza...\"",[236,603,391],{"class":253},[236,605,606],{"class":238,"line":287},[236,607,396],{"class":253},[236,609,610],{"class":238,"line":292},[236,611,274],{"emptyLinePlaceholder":273},[236,613,614,616],{"class":238,"line":302},[236,615,346],{"class":253},[236,617,349],{"class":242},[236,619,620,622,624,626,628],{"class":238,"line":307},[236,621,354],{"class":242},[236,623,357],{"class":242},[236,625,282],{"class":263},[236,627,362],{"class":253},[236,629,254],{"class":253},[236,631,632,634,636,638,640,642,644,647],{"class":238,"line":317},[236,633,370],{"class":369},[236,635,142],{"class":253},[236,637,376],{"class":375},[236,639,142],{"class":253},[236,641,381],{"class":263},[236,643,384],{"class":253},[236,645,646],{"class":387},"\"Baking Pepperoni  Pizza...\"",[236,648,391],{"class":253},[236,650,651],{"class":238,"line":440},[236,652,396],{"class":253},[236,654,655],{"class":238,"line":445},[236,656,274],{"emptyLinePlaceholder":273},[236,658,659,661],{"class":238,"line":450},[236,660,346],{"class":253},[236,662,349],{"class":242},[236,664,665,667,669,671,673],{"class":238,"line":457},[236,666,354],{"class":242},[236,668,357],{"class":242},[236,670,297],{"class":263},[236,672,362],{"class":253},[236,674,254],{"class":253},[236,676,677,679,681,683,685,687,689,692],{"class":238,"line":470},[236,678,370],{"class":369},[236,680,142],{"class":253},[236,682,376],{"class":375},[236,684,142],{"class":253},[236,686,381],{"class":263},[236,688,384],{"class":253},[236,690,691],{"class":387},"\"Cutting Pepperoni  Pizza...\"",[236,693,391],{"class":253},[236,695,696],{"class":238,"line":490},[236,697,396],{"class":253},[236,699,700],{"class":238,"line":495},[236,701,274],{"emptyLinePlaceholder":273},[236,703,704,706],{"class":238,"line":500},[236,705,346],{"class":253},[236,707,349],{"class":242},[236,709,710,712,714,716,718],{"class":238,"line":507},[236,711,354],{"class":242},[236,713,357],{"class":242},[236,715,312],{"class":263},[236,717,362],{"class":253},[236,719,254],{"class":253},[236,721,722,724,726,728,730,732,734,737],{"class":238,"line":520},[236,723,370],{"class":369},[236,725,142],{"class":253},[236,727,376],{"class":375},[236,729,142],{"class":253},[236,731,381],{"class":263},[236,733,384],{"class":253},[236,735,736],{"class":387},"\"Boxing Pepperoni  Pizza...\"",[236,738,391],{"class":253},[236,740,741],{"class":238,"line":540},[236,742,396],{"class":253},[236,744,745],{"class":238,"line":545},[236,746,320],{"class":253},[226,748,751],{"className":228,"code":749,"filename":750,"language":231,"meta":232,"style":232},"public class PizzaFactory {\n    public Pizza createPizza(String type) {\n        return switch (type.toLowerCase()) {\n            case \"margherita\" -> new MargheritaPizza();\n            case \"pepperoni\" -> new PepperoniPizza();\n            default -> null;\n        };\n    }\n}\n","PizzaFactory.java",[36,752,753,765,787,811,829,844,858,863,867],{"__ignoreMap":232},[236,754,755,757,760,763],{"class":238,"line":239},[236,756,243],{"class":242},[236,758,759],{"class":242}," class",[236,761,762],{"class":249}," PizzaFactory",[236,764,254],{"class":253},[236,766,767,769,771,774,776,779,782,785],{"class":238,"line":257},[236,768,354],{"class":242},[236,770,250],{"class":242},[236,772,773],{"class":263}," createPizza",[236,775,384],{"class":253},[236,777,778],{"class":242},"String",[236,780,781],{"class":369}," type",[236,783,784],{"class":253},")",[236,786,254],{"class":253},[236,788,789,792,795,798,801,803,806,809],{"class":238,"line":270},[236,790,791],{"class":242},"        return",[236,793,794],{"class":242}," switch",[236,796,797],{"class":253}," (",[236,799,800],{"class":369},"type",[236,802,142],{"class":253},[236,804,805],{"class":263},"toLowerCase",[236,807,808],{"class":253},"())",[236,810,254],{"class":253},[236,812,813,816,819,822,825,827],{"class":238,"line":277},[236,814,815],{"class":242},"            case",[236,817,818],{"class":387}," \"margherita\"",[236,820,821],{"class":242}," ->",[236,823,824],{"class":242}," new",[236,826,334],{"class":263},[236,828,267],{"class":253},[236,830,831,833,836,838,840,842],{"class":238,"line":287},[236,832,815],{"class":242},[236,834,835],{"class":387}," \"pepperoni\"",[236,837,821],{"class":242},[236,839,824],{"class":242},[236,841,560],{"class":263},[236,843,267],{"class":253},[236,845,846,849,851,855],{"class":238,"line":292},[236,847,848],{"class":242},"            default",[236,850,821],{"class":242},[236,852,854],{"class":853},"sheII"," null",[236,856,857],{"class":253},";\n",[236,859,860],{"class":238,"line":302},[236,861,862],{"class":253},"        };\n",[236,864,865],{"class":238,"line":307},[236,866,396],{"class":253},[236,868,869],{"class":238,"line":317},[236,870,320],{"class":253},[226,872,875],{"className":228,"code":873,"filename":874,"language":231,"meta":232,"style":232},"public class PizzaStore {\n    private final PizzaFactory pizzaFactory;\n\n    public PizzaStore(PizzaFactory pizzaFactory) {\n        this.pizzaFactory = pizzaFactory;\n    }\n\n    public Pizza orderPizza(String type) {\n        Pizza pizza = pizzaFactory.createPizza(type);\n\n        pizza.prepare();\n        pizza.bake();\n        pizza.cut();\n        pizza.box();\n\n        return pizza;\n    }\n}\n","PizzaStore.java",[36,876,877,888,903,907,923,940,944,948,967,990,994,1005,1015,1025,1035,1039,1048,1052],{"__ignoreMap":232},[236,878,879,881,883,886],{"class":238,"line":239},[236,880,243],{"class":242},[236,882,759],{"class":242},[236,884,885],{"class":249}," PizzaStore",[236,887,254],{"class":253},[236,889,890,893,896,898,901],{"class":238,"line":257},[236,891,892],{"class":242},"    private",[236,894,895],{"class":242}," final",[236,897,762],{"class":242},[236,899,900],{"class":369}," pizzaFactory",[236,902,857],{"class":253},[236,904,905],{"class":238,"line":270},[236,906,274],{"emptyLinePlaceholder":273},[236,908,909,911,913,915,917,919,921],{"class":238,"line":277},[236,910,354],{"class":242},[236,912,885],{"class":263},[236,914,384],{"class":253},[236,916,192],{"class":242},[236,918,900],{"class":369},[236,920,784],{"class":253},[236,922,254],{"class":253},[236,924,925,928,930,933,936,938],{"class":238,"line":287},[236,926,927],{"class":853},"        this",[236,929,142],{"class":253},[236,931,932],{"class":375},"pizzaFactory",[236,934,935],{"class":253}," =",[236,937,900],{"class":369},[236,939,857],{"class":253},[236,941,942],{"class":238,"line":292},[236,943,396],{"class":253},[236,945,946],{"class":238,"line":302},[236,947,274],{"emptyLinePlaceholder":273},[236,949,950,952,954,957,959,961,963,965],{"class":238,"line":307},[236,951,354],{"class":242},[236,953,250],{"class":242},[236,955,956],{"class":263}," orderPizza",[236,958,384],{"class":253},[236,960,778],{"class":242},[236,962,781],{"class":369},[236,964,784],{"class":253},[236,966,254],{"class":253},[236,968,969,972,975,978,980,982,984,986,988],{"class":238,"line":317},[236,970,971],{"class":242},"        Pizza",[236,973,974],{"class":369}," pizza ",[236,976,977],{"class":253},"=",[236,979,900],{"class":369},[236,981,142],{"class":253},[236,983,129],{"class":263},[236,985,384],{"class":253},[236,987,800],{"class":369},[236,989,391],{"class":253},[236,991,992],{"class":238,"line":440},[236,993,274],{"emptyLinePlaceholder":273},[236,995,996,999,1001,1003],{"class":238,"line":445},[236,997,998],{"class":369},"        pizza",[236,1000,142],{"class":253},[236,1002,162],{"class":263},[236,1004,267],{"class":253},[236,1006,1007,1009,1011,1013],{"class":238,"line":450},[236,1008,998],{"class":369},[236,1010,142],{"class":253},[236,1012,166],{"class":263},[236,1014,267],{"class":253},[236,1016,1017,1019,1021,1023],{"class":238,"line":457},[236,1018,998],{"class":369},[236,1020,142],{"class":253},[236,1022,169],{"class":263},[236,1024,267],{"class":253},[236,1026,1027,1029,1031,1033],{"class":238,"line":470},[236,1028,998],{"class":369},[236,1030,142],{"class":253},[236,1032,172],{"class":263},[236,1034,267],{"class":253},[236,1036,1037],{"class":238,"line":490},[236,1038,274],{"emptyLinePlaceholder":273},[236,1040,1041,1043,1046],{"class":238,"line":495},[236,1042,791],{"class":242},[236,1044,1045],{"class":369}," pizza",[236,1047,857],{"class":253},[236,1049,1050],{"class":238,"line":500},[236,1051,396],{"class":253},[236,1053,1054],{"class":238,"line":507},[236,1055,320],{"class":253},[226,1057,1060],{"className":228,"code":1058,"filename":1059,"language":231,"meta":232,"style":232},"public class Demo {\n    public static void main(String[] args) {\n        PizzaStore pizzaStore = new PizzaStore(new PizzaFactory());\n\n        pizzaStore.orderPizza(\"margherita\");\n        pizzaStore.orderPizza(\"pepperoni\");\n    }\n}\n","Demo.java",[36,1061,1062,1073,1099,1122,1126,1142,1157,1161],{"__ignoreMap":232},[236,1063,1064,1066,1068,1071],{"class":238,"line":239},[236,1065,243],{"class":242},[236,1067,759],{"class":242},[236,1069,1070],{"class":249}," Demo",[236,1072,254],{"class":253},[236,1074,1075,1077,1080,1082,1085,1087,1089,1092,1095,1097],{"class":238,"line":257},[236,1076,354],{"class":242},[236,1078,1079],{"class":242}," static",[236,1081,357],{"class":242},[236,1083,1084],{"class":263}," main",[236,1086,384],{"class":253},[236,1088,778],{"class":242},[236,1090,1091],{"class":253},"[]",[236,1093,1094],{"class":369}," args",[236,1096,784],{"class":253},[236,1098,254],{"class":253},[236,1100,1101,1104,1107,1109,1111,1113,1115,1117,1119],{"class":238,"line":270},[236,1102,1103],{"class":242},"        PizzaStore",[236,1105,1106],{"class":369}," pizzaStore ",[236,1108,977],{"class":253},[236,1110,824],{"class":242},[236,1112,885],{"class":263},[236,1114,384],{"class":253},[236,1116,38],{"class":242},[236,1118,762],{"class":263},[236,1120,1121],{"class":253},"());\n",[236,1123,1124],{"class":238,"line":277},[236,1125,274],{"emptyLinePlaceholder":273},[236,1127,1128,1131,1133,1135,1137,1140],{"class":238,"line":287},[236,1129,1130],{"class":369},"        pizzaStore",[236,1132,142],{"class":253},[236,1134,137],{"class":263},[236,1136,384],{"class":253},[236,1138,1139],{"class":387},"\"margherita\"",[236,1141,391],{"class":253},[236,1143,1144,1146,1148,1150,1152,1155],{"class":238,"line":292},[236,1145,1130],{"class":369},[236,1147,142],{"class":253},[236,1149,137],{"class":263},[236,1151,384],{"class":253},[236,1153,1154],{"class":387},"\"pepperoni\"",[236,1156,391],{"class":253},[236,1158,1159],{"class":238,"line":302},[236,1160,396],{"class":253},[236,1162,1163],{"class":238,"line":307},[236,1164,320],{"class":253},[32,1166,1167,1168,1170],{},"Running ",[36,1169,219],{}," prints:",[226,1172,1177],{"className":1173,"code":1175,"language":1176,"meta":232},[1174],"language-text","Preparing Margherita Pizza...\nBaking Margherita Pizza...\nCutting Margherita Pizza...\nBoxing Margherita Pizza...\nPreparing Pepperoni  Pizza...\nBaking Pepperoni  Pizza...\nCutting Pepperoni  Pizza...\nBoxing Pepperoni  Pizza...\n","text",[36,1178,1175],{"__ignoreMap":232},[1180,1181,1182],"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 .sVz7n, html code.shiki .sVz7n{--shiki-default:#EDEDEF;--shiki-light:#0B0B0C}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 .sWFPS, html code.shiki .sWFPS{--shiki-default:#C4C4CC;--shiki-light:#3F3F46}html pre.shiki code .s4Awn, html code.shiki .s4Awn{--shiki-default:#C5DDFE;--shiki-light:#534DAF}html pre.shiki code .sUOzB, html code.shiki .sUOzB{--shiki-default:#9EC7FE;--shiki-light:#2873D1}html pre.shiki code .sheII, html code.shiki .sheII{--shiki-default:#70ADFF;--shiki-light:#195CAE}",{"title":232,"searchDepth":257,"depth":257,"links":1184},[1185,1186,1187,1191,1192],{"id":29,"depth":257,"text":30},{"id":68,"depth":257,"text":69},{"id":91,"depth":257,"text":92,"children":1188},[1189,1190],{"id":96,"depth":270,"text":97},{"id":111,"depth":270,"text":112},{"id":145,"depth":257,"text":146},{"id":223,"depth":257,"text":224},"The Factory pattern moves the choice of which class to create into one place, so client code asks for an object by name and gets back an interface. Explained with a worked Java example of a pizza shop.","A pizza shop that makes margherita and pepperoni","md","Creational","lucide:factory",{},"\u002Flearn\u002Fdesign-patterns\u002Ffactory","---\ntitle: Factory\ngroup: Creational\nicon: lucide:factory\nsummary: 'One place decides which concrete class to create, so the rest of the code only asks for what it needs.'\ndescription: 'The Factory pattern moves the choice of which class to create into one place, so client code asks for an object by name and gets back an interface. Explained with a worked Java example of a pizza shop.'\nexample: 'A pizza shop that makes margherita and pepperoni'\norder: 2\nsource: https:\u002F\u002Fgithub.com\u002FHaythamasalama\u002FdesignPattern\u002Ftree\u002Fmaster\u002Fsrc\u002FCreational\u002FFactory\n---\n\n## The idea\n\nCode that calls `new` on a concrete class is tied to that class. If a shop's ordering code says `new MargheritaPizza()` and `new PepperoniPizza()`, every new pizza means editing the ordering code, and the same `if` or `switch` tends to appear in several places.\n\nA factory moves that decision into one object. The client asks the factory for \"a pepperoni pizza\", and the factory decides which class to create. The client only knows the common interface, `Pizza`, so it works with any pizza the factory can make.\n\nThe Gang of Four book describes two related patterns. In Factory Method, subclasses decide which class to create. Abstract Factory creates families of related objects. The example here is the simpler form both build on, often called a simple factory: one class with one method that picks the concrete class.\n\n## When to use it\n\n- You don't know until runtime which concrete class you need, for example when it comes from user input or a config file.\n- The same creation logic would otherwise be repeated in several places.\n- You want to add new types without changing the code that uses them.\n- Creating an object takes more than a call to `new`, such as setting defaults or looking something up first.\n\n## Pros and cons\n\n### Pros\n\n- Client code creates objects without naming their concrete classes.\n- Adding a new type is easy: write the class and add one case to the factory.\n- Creation logic lives in one place, so it is easier to change and to test.\n\n### Cons\n\n- It adds classes to the system: an interface, a factory and one class per type.\n- A simple factory still has a `switch` that must change for every new type. Factory Method avoids this by moving the choice into subclasses.\n- The client has to handle types the factory doesn't know. Here, `createPizza` returns `null` for an unknown name, and `orderPizza` then fails with a `NullPointerException`.\n\n## The example\n\nThe example is a pizza shop. The store takes an order by name, asks a factory for the matching pizza, and then runs the same four steps on it, whatever kind it is.\n\n- **`Pizza`**: the product interface: `prepare`, `bake`, `cut` and `box`.\n- **`MargheritaPizza`** and **`PepperoniPizza`**: the concrete products.\n- **`PizzaFactory`**: the factory. `createPizza` turns a name into the right `Pizza` with a `switch`.\n- **`PizzaStore`**: the client. It receives the factory in its constructor and only ever works with the `Pizza` interface.\n- **`Demo`**: builds the store and orders one pizza of each kind.\n\n## The code\n\n```java [Pizza.java]\npublic interface Pizza {\n    void prepare();\n\n    void bake();\n\n    void cut();\n\n    void box();\n}\n```\n\n```java [MargheritaPizza.java]\nclass MargheritaPizza implements Pizza {\n    @Override\n    public void prepare() {\n        System.out.println(\"Preparing Margherita Pizza...\");\n    }\n\n    @Override\n    public void bake() {\n        System.out.println(\"Baking Margherita Pizza...\");\n    }\n\n    @Override\n    public void cut() {\n        System.out.println(\"Cutting Margherita Pizza...\");\n    }\n\n    @Override\n    public void box() {\n        System.out.println(\"Boxing Margherita Pizza...\");\n    }\n}\n```\n\n```java [PepperoniPizza.java]\nclass PepperoniPizza implements Pizza {\n    @Override\n    public void prepare() {\n        System.out.println(\"Preparing Pepperoni  Pizza...\");\n    }\n\n    @Override\n    public void bake() {\n        System.out.println(\"Baking Pepperoni  Pizza...\");\n    }\n\n    @Override\n    public void cut() {\n        System.out.println(\"Cutting Pepperoni  Pizza...\");\n    }\n\n    @Override\n    public void box() {\n        System.out.println(\"Boxing Pepperoni  Pizza...\");\n    }\n}\n```\n\n```java [PizzaFactory.java]\npublic class PizzaFactory {\n    public Pizza createPizza(String type) {\n        return switch (type.toLowerCase()) {\n            case \"margherita\" -> new MargheritaPizza();\n            case \"pepperoni\" -> new PepperoniPizza();\n            default -> null;\n        };\n    }\n}\n```\n\n```java [PizzaStore.java]\npublic class PizzaStore {\n    private final PizzaFactory pizzaFactory;\n\n    public PizzaStore(PizzaFactory pizzaFactory) {\n        this.pizzaFactory = pizzaFactory;\n    }\n\n    public Pizza orderPizza(String type) {\n        Pizza pizza = pizzaFactory.createPizza(type);\n\n        pizza.prepare();\n        pizza.bake();\n        pizza.cut();\n        pizza.box();\n\n        return pizza;\n    }\n}\n```\n\n```java [Demo.java]\npublic class Demo {\n    public static void main(String[] args) {\n        PizzaStore pizzaStore = new PizzaStore(new PizzaFactory());\n\n        pizzaStore.orderPizza(\"margherita\");\n        pizzaStore.orderPizza(\"pepperoni\");\n    }\n}\n```\n\nRunning `Demo` prints:\n\n```text\nPreparing Margherita Pizza...\nBaking Margherita Pizza...\nCutting Margherita Pizza...\nBoxing Margherita Pizza...\nPreparing Pepperoni  Pizza...\nBaking Pepperoni  Pizza...\nCutting Pepperoni  Pizza...\nBoxing Pepperoni  Pizza...\n```\n",{"title":22,"description":1193},{"loc":1199},"https:\u002F\u002Fgithub.com\u002FHaythamasalama\u002FdesignPattern\u002Ftree\u002Fmaster\u002Fsrc\u002FCreational\u002FFactory","learn\u002Fdesign-patterns\u002Ffactory","One place decides which concrete class to create, so the rest of the code only asks for what it needs.","HpGXnGLv7qKesoEWltZ_xlNBGxavpS-5oFd_aNv5bYI",[1208,1213,1214,1219],{"path":1209,"title":1210,"icon":1211,"summary":1212,"order":239},"\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":1199,"title":22,"icon":1197,"summary":1205,"order":257},{"path":1215,"title":1216,"icon":1217,"summary":1218,"order":270},"\u002Flearn\u002Fdesign-patterns\u002Fbuilder","Builder","lucide:hammer","Build a complex object step by step, setting only the parts you need, then get the finished object in one call.",{"path":1220,"title":1221,"icon":1222,"summary":1223,"order":277},"\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":1225},"\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":1227},"\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":1229},"\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":1231},"\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":1233},"\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":1235},"\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>",1793131416363]