[{"data":1,"prerenderedAt":1058},["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\u002Fprototype":20,"pattern-group:\u002Flearn\u002Fdesign-patterns\u002Fprototype":1029,"i-lucide:lightbulb":1046,"i-lucide:wand-sparkles":1048,"i-lucide:circle-dot":1050,"i-lucide:factory":1052,"i-lucide:hammer":1054,"i-lucide:copy":1056},{"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":1015,"example":1016,"extension":1017,"group":1018,"icon":1019,"meta":1020,"navigation":265,"order":238,"path":1021,"rawbody":1022,"seo":1023,"sitemap":1024,"source":1025,"stem":1026,"summary":1027,"__hash__":1028},"patterns\u002Flearn\u002Fdesign-patterns\u002Fprototype.md","Prototype",{"type":24,"value":25,"toc":1005},"minimark",[26,31,35,43,46,50,66,70,75,86,90,112,116,123,164,171,175,614,842,995,1001],[27,28,30],"h2",{"id":29},"the-idea","The idea",[32,33,34],"p",{},"Sometimes creating an object from scratch is slow or awkward. It may need many settings, data from a file or a long calculation. If you already have an object that is close to what you need, it is easier to copy it.",[32,36,37,38,42],{},"The Prototype pattern gives objects a ",[39,40,41],"code",{},"clone()"," method. Each class knows how to copy itself, so client code can make a copy without knowing the exact class. You keep a ready-made object, the prototype, clone it whenever you need a new one, and then change the few fields that differ.",[32,44,45],{},"Because the object makes the copy itself, the copy can include private state that outside code could not reach.",[27,47,49],{"id":48},"when-to-use-it","When to use it",[51,52,53,57,60,63],"ul",{},[54,55,56],"li",{},"Creating an object is expensive, and many of your objects start out the same.",[54,58,59],{},"You need copies of an object, but your code only knows it through an interface or a base class.",[54,61,62],{},"You want a set of preset objects, such as default configurations, that callers copy and adjust.",[54,64,65],{},"The classes to create are only known at runtime, for example plugins registered as sample objects.",[27,67,69],{"id":68},"pros-and-cons","Pros and cons",[71,72,74],"h3",{"id":73},"pros","Pros",[51,76,77,80,83],{},[54,78,79],{},"You create new objects without naming their class.",[54,81,82],{},"Making many similar instances is easy: copy the prototype and change what differs.",[54,84,85],{},"You avoid repeating long setup code for each new object.",[71,87,89],{"id":88},"cons","Cons",[51,91,92,98,101],{},[54,93,94,95,97],{},"Every class in the hierarchy must implement ",[39,96,41],{},", and keep it up to date when fields are added.",[54,99,100],{},"Copying objects that refer to other objects is tricky. You must choose between a shallow copy, which shares the inner objects, and a deep copy, which copies them too.",[54,102,103,104,107,108,111],{},"Java's own ",[39,105,106],{},"Cloneable"," interface and ",[39,109,110],{},"Object.clone()"," are easy to misuse, so a copy method written by hand, as in this example, is often clearer.",[27,113,115],{"id":114},"the-example","The example",[32,117,118,119,122],{},"The example copies a car. A ",[39,120,121],{},"FordPrototype"," starts as a red Mustang with a 5.0L engine. The demo clones it twice: one copy is painted blue, and the other gets a bigger engine. The original stays as it was.",[51,124,125,140,153],{},[54,126,127,133,134,136,137,139],{},[128,129,130],"strong",{},[39,131,132],{},"CarPrototype",": the prototype. An abstract class with the car's fields, getters and setters, and an abstract ",[39,135,41],{}," method that returns a ",[39,138,132],{},".",[54,141,142,146,147,149,150,152],{},[128,143,144],{},[39,145,121],{},": the concrete prototype. Its constructor sets the default values, and its ",[39,148,41],{}," creates a new ",[39,151,121],{}," and copies each field into it.",[54,154,155,160,161,163],{},[128,156,157],{},[39,158,159],{},"Demo",": the client. It keeps one prototype, calls ",[39,162,41],{}," to get new cars, and changes the colour of one and the engine size of the other.",[32,165,166,167,170],{},"The fields are all ",[39,168,169],{},"String","s, which can't be changed in Java, so copying the references is enough here.",[27,172,174],{"id":173},"the-code","The code",[176,177,183],"pre",{"className":178,"code":179,"filename":180,"language":181,"meta":182,"style":182},"language-java shiki shiki-themes haytham-dark haytham-light","public abstract class CarPrototype {\n    protected String maker;\n    protected String model;\n    protected String color;\n    protected String engineSize;\n\n    public abstract CarPrototype clone();\n\n    public String getMake() {\n        return maker;\n    }\n\n    public void setMaker(String maker) {\n        this.maker = maker;\n    }\n\n    public String getModel() {\n        return model;\n    }\n\n    public void setModel(String model) {\n        this.model = model;\n    }\n\n    public String getColor() {\n        return color;\n    }\n\n    public void setColor(String color) {\n        this.color = color;\n    }\n\n    public String getEngineSize() {\n        return engineSize;\n    }\n\n    public void setEngineSize(String engineSize) {\n        this.engineSize = engineSize;\n    }\n}\n","CarPrototype.java","java","",[39,184,185,208,224,236,248,260,267,284,289,304,314,320,325,348,368,373,378,392,401,406,411,431,447,452,456,470,479,484,489,509,525,530,534,548,557,562,567,587,603,608],{"__ignoreMap":182},[186,187,190,194,197,200,204],"span",{"class":188,"line":189},"line",1,[186,191,193],{"class":192},"sVsk0","public",[186,195,196],{"class":192}," abstract",[186,198,199],{"class":192}," class",[186,201,203],{"class":202},"sNwy7"," CarPrototype",[186,205,207],{"class":206},"sVG6I"," {\n",[186,209,211,214,217,221],{"class":188,"line":210},2,[186,212,213],{"class":192},"    protected",[186,215,216],{"class":192}," String",[186,218,220],{"class":219},"sWFPS"," maker",[186,222,223],{"class":206},";\n",[186,225,227,229,231,234],{"class":188,"line":226},3,[186,228,213],{"class":192},[186,230,216],{"class":192},[186,232,233],{"class":219}," model",[186,235,223],{"class":206},[186,237,239,241,243,246],{"class":188,"line":238},4,[186,240,213],{"class":192},[186,242,216],{"class":192},[186,244,245],{"class":219}," color",[186,247,223],{"class":206},[186,249,251,253,255,258],{"class":188,"line":250},5,[186,252,213],{"class":192},[186,254,216],{"class":192},[186,256,257],{"class":219}," engineSize",[186,259,223],{"class":206},[186,261,263],{"class":188,"line":262},6,[186,264,266],{"emptyLinePlaceholder":265},true,"\n",[186,268,270,273,275,277,281],{"class":188,"line":269},7,[186,271,272],{"class":192},"    public",[186,274,196],{"class":192},[186,276,203],{"class":192},[186,278,280],{"class":279},"sVz7n"," clone",[186,282,283],{"class":206},"();\n",[186,285,287],{"class":188,"line":286},8,[186,288,266],{"emptyLinePlaceholder":265},[186,290,292,294,296,299,302],{"class":188,"line":291},9,[186,293,272],{"class":192},[186,295,216],{"class":192},[186,297,298],{"class":279}," getMake",[186,300,301],{"class":206},"()",[186,303,207],{"class":206},[186,305,307,310,312],{"class":188,"line":306},10,[186,308,309],{"class":192},"        return",[186,311,220],{"class":219},[186,313,223],{"class":206},[186,315,317],{"class":188,"line":316},11,[186,318,319],{"class":206},"    }\n",[186,321,323],{"class":188,"line":322},12,[186,324,266],{"emptyLinePlaceholder":265},[186,326,328,330,333,336,339,341,343,346],{"class":188,"line":327},13,[186,329,272],{"class":192},[186,331,332],{"class":192}," void",[186,334,335],{"class":279}," setMaker",[186,337,338],{"class":206},"(",[186,340,169],{"class":192},[186,342,220],{"class":219},[186,344,345],{"class":206},")",[186,347,207],{"class":206},[186,349,351,355,357,361,364,366],{"class":188,"line":350},14,[186,352,354],{"class":353},"sheII","        this",[186,356,139],{"class":206},[186,358,360],{"class":359},"s4Awn","maker",[186,362,363],{"class":206}," =",[186,365,220],{"class":219},[186,367,223],{"class":206},[186,369,371],{"class":188,"line":370},15,[186,372,319],{"class":206},[186,374,376],{"class":188,"line":375},16,[186,377,266],{"emptyLinePlaceholder":265},[186,379,381,383,385,388,390],{"class":188,"line":380},17,[186,382,272],{"class":192},[186,384,216],{"class":192},[186,386,387],{"class":279}," getModel",[186,389,301],{"class":206},[186,391,207],{"class":206},[186,393,395,397,399],{"class":188,"line":394},18,[186,396,309],{"class":192},[186,398,233],{"class":219},[186,400,223],{"class":206},[186,402,404],{"class":188,"line":403},19,[186,405,319],{"class":206},[186,407,409],{"class":188,"line":408},20,[186,410,266],{"emptyLinePlaceholder":265},[186,412,414,416,418,421,423,425,427,429],{"class":188,"line":413},21,[186,415,272],{"class":192},[186,417,332],{"class":192},[186,419,420],{"class":279}," setModel",[186,422,338],{"class":206},[186,424,169],{"class":192},[186,426,233],{"class":219},[186,428,345],{"class":206},[186,430,207],{"class":206},[186,432,434,436,438,441,443,445],{"class":188,"line":433},22,[186,435,354],{"class":353},[186,437,139],{"class":206},[186,439,440],{"class":359},"model",[186,442,363],{"class":206},[186,444,233],{"class":219},[186,446,223],{"class":206},[186,448,450],{"class":188,"line":449},23,[186,451,319],{"class":206},[186,453,454],{"class":188,"line":5},[186,455,266],{"emptyLinePlaceholder":265},[186,457,459,461,463,466,468],{"class":188,"line":458},25,[186,460,272],{"class":192},[186,462,216],{"class":192},[186,464,465],{"class":279}," getColor",[186,467,301],{"class":206},[186,469,207],{"class":206},[186,471,473,475,477],{"class":188,"line":472},26,[186,474,309],{"class":192},[186,476,245],{"class":219},[186,478,223],{"class":206},[186,480,482],{"class":188,"line":481},27,[186,483,319],{"class":206},[186,485,487],{"class":188,"line":486},28,[186,488,266],{"emptyLinePlaceholder":265},[186,490,492,494,496,499,501,503,505,507],{"class":188,"line":491},29,[186,493,272],{"class":192},[186,495,332],{"class":192},[186,497,498],{"class":279}," setColor",[186,500,338],{"class":206},[186,502,169],{"class":192},[186,504,245],{"class":219},[186,506,345],{"class":206},[186,508,207],{"class":206},[186,510,512,514,516,519,521,523],{"class":188,"line":511},30,[186,513,354],{"class":353},[186,515,139],{"class":206},[186,517,518],{"class":359},"color",[186,520,363],{"class":206},[186,522,245],{"class":219},[186,524,223],{"class":206},[186,526,528],{"class":188,"line":527},31,[186,529,319],{"class":206},[186,531,532],{"class":188,"line":11},[186,533,266],{"emptyLinePlaceholder":265},[186,535,537,539,541,544,546],{"class":188,"line":536},33,[186,538,272],{"class":192},[186,540,216],{"class":192},[186,542,543],{"class":279}," getEngineSize",[186,545,301],{"class":206},[186,547,207],{"class":206},[186,549,551,553,555],{"class":188,"line":550},34,[186,552,309],{"class":192},[186,554,257],{"class":219},[186,556,223],{"class":206},[186,558,560],{"class":188,"line":559},35,[186,561,319],{"class":206},[186,563,565],{"class":188,"line":564},36,[186,566,266],{"emptyLinePlaceholder":265},[186,568,570,572,574,577,579,581,583,585],{"class":188,"line":569},37,[186,571,272],{"class":192},[186,573,332],{"class":192},[186,575,576],{"class":279}," setEngineSize",[186,578,338],{"class":206},[186,580,169],{"class":192},[186,582,257],{"class":219},[186,584,345],{"class":206},[186,586,207],{"class":206},[186,588,590,592,594,597,599,601],{"class":188,"line":589},38,[186,591,354],{"class":353},[186,593,139],{"class":206},[186,595,596],{"class":359},"engineSize",[186,598,363],{"class":206},[186,600,257],{"class":219},[186,602,223],{"class":206},[186,604,606],{"class":188,"line":605},39,[186,607,319],{"class":206},[186,609,611],{"class":188,"line":610},40,[186,612,613],{"class":206},"}\n",[176,615,618],{"className":178,"code":616,"filename":617,"language":181,"meta":182,"style":182},"public class FordPrototype extends CarPrototype {\n    public FordPrototype() {\n        maker = \"Ford\";\n        model = \"Mustang\";\n        color = \"Red\";\n        engineSize = \"5.0L\";\n    }\n\n    @Override\n    public CarPrototype clone() {\n        FordPrototype fordPrototype = new FordPrototype();\n        fordPrototype.setMaker(this.maker);\n        fordPrototype.setModel(this.model);\n        fordPrototype.setColor(this.color);\n        fordPrototype.setEngineSize(this.engineSize);\n        \n        return fordPrototype;\n    }\n}\n","FordPrototype.java",[39,619,620,636,646,660,672,684,696,700,704,712,724,741,763,782,801,820,825,834,838],{"__ignoreMap":182},[186,621,622,624,626,629,632,634],{"class":188,"line":189},[186,623,193],{"class":192},[186,625,199],{"class":192},[186,627,628],{"class":202}," FordPrototype",[186,630,631],{"class":192}," extends",[186,633,203],{"class":202},[186,635,207],{"class":206},[186,637,638,640,642,644],{"class":188,"line":210},[186,639,272],{"class":192},[186,641,628],{"class":279},[186,643,301],{"class":206},[186,645,207],{"class":206},[186,647,648,651,654,658],{"class":188,"line":226},[186,649,650],{"class":219},"        maker ",[186,652,653],{"class":206},"=",[186,655,657],{"class":656},"sUOzB"," \"Ford\"",[186,659,223],{"class":206},[186,661,662,665,667,670],{"class":188,"line":238},[186,663,664],{"class":219},"        model ",[186,666,653],{"class":206},[186,668,669],{"class":656}," \"Mustang\"",[186,671,223],{"class":206},[186,673,674,677,679,682],{"class":188,"line":250},[186,675,676],{"class":219},"        color ",[186,678,653],{"class":206},[186,680,681],{"class":656}," \"Red\"",[186,683,223],{"class":206},[186,685,686,689,691,694],{"class":188,"line":262},[186,687,688],{"class":219},"        engineSize ",[186,690,653],{"class":206},[186,692,693],{"class":656}," \"5.0L\"",[186,695,223],{"class":206},[186,697,698],{"class":188,"line":269},[186,699,319],{"class":206},[186,701,702],{"class":188,"line":286},[186,703,266],{"emptyLinePlaceholder":265},[186,705,706,709],{"class":188,"line":291},[186,707,708],{"class":206},"    @",[186,710,711],{"class":192},"Override\n",[186,713,714,716,718,720,722],{"class":188,"line":306},[186,715,272],{"class":192},[186,717,203],{"class":192},[186,719,280],{"class":279},[186,721,301],{"class":206},[186,723,207],{"class":206},[186,725,726,729,732,734,737,739],{"class":188,"line":316},[186,727,728],{"class":192},"        FordPrototype",[186,730,731],{"class":219}," fordPrototype ",[186,733,653],{"class":206},[186,735,736],{"class":192}," new",[186,738,628],{"class":279},[186,740,283],{"class":206},[186,742,743,746,748,751,753,756,758,760],{"class":188,"line":322},[186,744,745],{"class":219},"        fordPrototype",[186,747,139],{"class":206},[186,749,750],{"class":279},"setMaker",[186,752,338],{"class":206},[186,754,755],{"class":353},"this",[186,757,139],{"class":206},[186,759,360],{"class":359},[186,761,762],{"class":206},");\n",[186,764,765,767,769,772,774,776,778,780],{"class":188,"line":327},[186,766,745],{"class":219},[186,768,139],{"class":206},[186,770,771],{"class":279},"setModel",[186,773,338],{"class":206},[186,775,755],{"class":353},[186,777,139],{"class":206},[186,779,440],{"class":359},[186,781,762],{"class":206},[186,783,784,786,788,791,793,795,797,799],{"class":188,"line":350},[186,785,745],{"class":219},[186,787,139],{"class":206},[186,789,790],{"class":279},"setColor",[186,792,338],{"class":206},[186,794,755],{"class":353},[186,796,139],{"class":206},[186,798,518],{"class":359},[186,800,762],{"class":206},[186,802,803,805,807,810,812,814,816,818],{"class":188,"line":370},[186,804,745],{"class":219},[186,806,139],{"class":206},[186,808,809],{"class":279},"setEngineSize",[186,811,338],{"class":206},[186,813,755],{"class":353},[186,815,139],{"class":206},[186,817,596],{"class":359},[186,819,762],{"class":206},[186,821,822],{"class":188,"line":375},[186,823,824],{"class":219},"        \n",[186,826,827,829,832],{"class":188,"line":380},[186,828,309],{"class":192},[186,830,831],{"class":219}," fordPrototype",[186,833,223],{"class":206},[186,835,836],{"class":188,"line":394},[186,837,319],{"class":206},[186,839,840],{"class":188,"line":403},[186,841,613],{"class":206},[176,843,846],{"className":178,"code":844,"filename":845,"language":181,"meta":182,"style":182},"public class Demo {\n    public static void main(String[] args) {\n        FordPrototype fordPrototype = new FordPrototype();\n        \n        FordPrototype ford1 = (FordPrototype) fordPrototype.clone();\n        ford1.setColor(\"Blue\");\n\n        FordPrototype ford2 = (FordPrototype) fordPrototype.clone();\n        ford2.setEngineSize(\"5.5L\");\n    }\n}\n","Demo.java",[39,847,848,859,885,899,903,928,944,948,971,987,991],{"__ignoreMap":182},[186,849,850,852,854,857],{"class":188,"line":189},[186,851,193],{"class":192},[186,853,199],{"class":192},[186,855,856],{"class":202}," Demo",[186,858,207],{"class":206},[186,860,861,863,866,868,871,873,875,878,881,883],{"class":188,"line":210},[186,862,272],{"class":192},[186,864,865],{"class":192}," static",[186,867,332],{"class":192},[186,869,870],{"class":279}," main",[186,872,338],{"class":206},[186,874,169],{"class":192},[186,876,877],{"class":206},"[]",[186,879,880],{"class":219}," args",[186,882,345],{"class":206},[186,884,207],{"class":206},[186,886,887,889,891,893,895,897],{"class":188,"line":226},[186,888,728],{"class":192},[186,890,731],{"class":219},[186,892,653],{"class":206},[186,894,736],{"class":192},[186,896,628],{"class":279},[186,898,283],{"class":206},[186,900,901],{"class":188,"line":238},[186,902,824],{"class":219},[186,904,905,907,910,912,915,917,919,921,923,926],{"class":188,"line":250},[186,906,728],{"class":192},[186,908,909],{"class":219}," ford1 ",[186,911,653],{"class":206},[186,913,914],{"class":206}," (",[186,916,121],{"class":219},[186,918,345],{"class":206},[186,920,831],{"class":219},[186,922,139],{"class":206},[186,924,925],{"class":279},"clone",[186,927,283],{"class":206},[186,929,930,933,935,937,939,942],{"class":188,"line":262},[186,931,932],{"class":219},"        ford1",[186,934,139],{"class":206},[186,936,790],{"class":279},[186,938,338],{"class":206},[186,940,941],{"class":656},"\"Blue\"",[186,943,762],{"class":206},[186,945,946],{"class":188,"line":269},[186,947,266],{"emptyLinePlaceholder":265},[186,949,950,952,955,957,959,961,963,965,967,969],{"class":188,"line":286},[186,951,728],{"class":192},[186,953,954],{"class":219}," ford2 ",[186,956,653],{"class":206},[186,958,914],{"class":206},[186,960,121],{"class":219},[186,962,345],{"class":206},[186,964,831],{"class":219},[186,966,139],{"class":206},[186,968,925],{"class":279},[186,970,283],{"class":206},[186,972,973,976,978,980,982,985],{"class":188,"line":291},[186,974,975],{"class":219},"        ford2",[186,977,139],{"class":206},[186,979,809],{"class":279},[186,981,338],{"class":206},[186,983,984],{"class":656},"\"5.5L\"",[186,986,762],{"class":206},[186,988,989],{"class":188,"line":306},[186,990,319],{"class":206},[186,992,993],{"class":188,"line":316},[186,994,613],{"class":206},[32,996,997,998,1000],{},"Running ",[39,999,159],{}," prints nothing: it makes the two copies and changes them, but doesn't print them.",[1002,1003,1004],"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":182,"searchDepth":210,"depth":210,"links":1006},[1007,1008,1009,1013,1014],{"id":29,"depth":210,"text":30},{"id":48,"depth":210,"text":49},{"id":68,"depth":210,"text":69,"children":1010},[1011,1012],{"id":73,"depth":226,"text":74},{"id":88,"depth":226,"text":89},{"id":114,"depth":210,"text":115},{"id":173,"depth":210,"text":174},"The Prototype pattern creates new objects by copying an existing one, so you skip the setup and change only what differs. Learn when copying beats building, with a worked Java example of a car.","Copies of a Ford Mustang","md","Creational","lucide:copy",{},"\u002Flearn\u002Fdesign-patterns\u002Fprototype","---\ntitle: Prototype\ngroup: Creational\nicon: lucide:copy\nsummary: 'Create a new object by copying an existing one, then change only what differs.'\ndescription: 'The Prototype pattern creates new objects by copying an existing one, so you skip the setup and change only what differs. Learn when copying beats building, with a worked Java example of a car.'\nexample: 'Copies of a Ford Mustang'\norder: 4\nsource: https:\u002F\u002Fgithub.com\u002FHaythamasalama\u002FdesignPattern\u002Ftree\u002Fmaster\u002Fsrc\u002FCreational\u002FPrototype\n---\n\n## The idea\n\nSometimes creating an object from scratch is slow or awkward. It may need many settings, data from a file or a long calculation. If you already have an object that is close to what you need, it is easier to copy it.\n\nThe Prototype pattern gives objects a `clone()` method. Each class knows how to copy itself, so client code can make a copy without knowing the exact class. You keep a ready-made object, the prototype, clone it whenever you need a new one, and then change the few fields that differ.\n\nBecause the object makes the copy itself, the copy can include private state that outside code could not reach.\n\n## When to use it\n\n- Creating an object is expensive, and many of your objects start out the same.\n- You need copies of an object, but your code only knows it through an interface or a base class.\n- You want a set of preset objects, such as default configurations, that callers copy and adjust.\n- The classes to create are only known at runtime, for example plugins registered as sample objects.\n\n## Pros and cons\n\n### Pros\n\n- You create new objects without naming their class.\n- Making many similar instances is easy: copy the prototype and change what differs.\n- You avoid repeating long setup code for each new object.\n\n### Cons\n\n- Every class in the hierarchy must implement `clone()`, and keep it up to date when fields are added.\n- Copying objects that refer to other objects is tricky. You must choose between a shallow copy, which shares the inner objects, and a deep copy, which copies them too.\n- Java's own `Cloneable` interface and `Object.clone()` are easy to misuse, so a copy method written by hand, as in this example, is often clearer.\n\n## The example\n\nThe example copies a car. A `FordPrototype` starts as a red Mustang with a 5.0L engine. The demo clones it twice: one copy is painted blue, and the other gets a bigger engine. The original stays as it was.\n\n- **`CarPrototype`**: the prototype. An abstract class with the car's fields, getters and setters, and an abstract `clone()` method that returns a `CarPrototype`.\n- **`FordPrototype`**: the concrete prototype. Its constructor sets the default values, and its `clone()` creates a new `FordPrototype` and copies each field into it.\n- **`Demo`**: the client. It keeps one prototype, calls `clone()` to get new cars, and changes the colour of one and the engine size of the other.\n\nThe fields are all `String`s, which can't be changed in Java, so copying the references is enough here.\n\n## The code\n\n```java [CarPrototype.java]\npublic abstract class CarPrototype {\n    protected String maker;\n    protected String model;\n    protected String color;\n    protected String engineSize;\n\n    public abstract CarPrototype clone();\n\n    public String getMake() {\n        return maker;\n    }\n\n    public void setMaker(String maker) {\n        this.maker = maker;\n    }\n\n    public String getModel() {\n        return model;\n    }\n\n    public void setModel(String model) {\n        this.model = model;\n    }\n\n    public String getColor() {\n        return color;\n    }\n\n    public void setColor(String color) {\n        this.color = color;\n    }\n\n    public String getEngineSize() {\n        return engineSize;\n    }\n\n    public void setEngineSize(String engineSize) {\n        this.engineSize = engineSize;\n    }\n}\n```\n\n```java [FordPrototype.java]\npublic class FordPrototype extends CarPrototype {\n    public FordPrototype() {\n        maker = \"Ford\";\n        model = \"Mustang\";\n        color = \"Red\";\n        engineSize = \"5.0L\";\n    }\n\n    @Override\n    public CarPrototype clone() {\n        FordPrototype fordPrototype = new FordPrototype();\n        fordPrototype.setMaker(this.maker);\n        fordPrototype.setModel(this.model);\n        fordPrototype.setColor(this.color);\n        fordPrototype.setEngineSize(this.engineSize);\n        \n        return fordPrototype;\n    }\n}\n```\n\n```java [Demo.java]\npublic class Demo {\n    public static void main(String[] args) {\n        FordPrototype fordPrototype = new FordPrototype();\n        \n        FordPrototype ford1 = (FordPrototype) fordPrototype.clone();\n        ford1.setColor(\"Blue\");\n\n        FordPrototype ford2 = (FordPrototype) fordPrototype.clone();\n        ford2.setEngineSize(\"5.5L\");\n    }\n}\n```\n\nRunning `Demo` prints nothing: it makes the two copies and changes them, but doesn't print them.\n",{"title":22,"description":1015},{"loc":1021},"https:\u002F\u002Fgithub.com\u002FHaythamasalama\u002FdesignPattern\u002Ftree\u002Fmaster\u002Fsrc\u002FCreational\u002FPrototype","learn\u002Fdesign-patterns\u002Fprototype","Create a new object by copying an existing one, then change only what differs.","NhzB5iY8gjMzX19PhJOYdkaoMjL3RYBY_5vLfQwXD40",[1030,1035,1040,1045],{"path":1031,"title":1032,"icon":1033,"summary":1034,"order":189},"\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":1036,"title":1037,"icon":1038,"summary":1039,"order":210},"\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":1041,"title":1042,"icon":1043,"summary":1044,"order":226},"\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":1021,"title":22,"icon":1019,"summary":1027,"order":238},{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1047},"\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":1049},"\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":1051},"\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":1053},"\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":1055},"\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":1057},"\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>",1793131416425]