[{"data":1,"prerenderedAt":1179},["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\u002Fflyweight":20,"pattern-group:\u002Flearn\u002Fdesign-patterns\u002Fflyweight":1127,"i-lucide:lightbulb":1159,"i-lucide:boxes":1161,"i-lucide:plug":1163,"i-lucide:bridge":1165,"i-lucide:network":1167,"i-lucide:gift":1169,"i-lucide:door-open":1171,"i-lucide:feather":1173,"i-lucide:shield-check":1175,"i-lucide:copy":1177},{"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":1113,"example":1114,"extension":1115,"group":1116,"icon":1117,"meta":1118,"navigation":335,"order":332,"path":1119,"rawbody":1120,"seo":1121,"sitemap":1122,"source":1123,"stem":1124,"summary":1125,"__hash__":1126},"patterns\u002Flearn\u002Fdesign-patterns\u002Fflyweight.md","Flyweight",{"type":24,"value":25,"toc":1103},"minimark",[26,31,35,38,41,45,61,65,70,81,85,96,100,103,158,164,183,187,255,618,861,1085,1091,1099],[27,28,30],"h2",{"id":29},"the-idea","The idea",[32,33,34],"p",{},"Some programs need a huge number of small objects: trees in a game world, characters in a text editor, particles in a simulation. If each object stores all of its data, memory runs out quickly, even though most of that data is the same across objects.",[32,36,37],{},"The Flyweight pattern splits an object's data in two. Intrinsic state is the part that many objects have in common, such as a tree's shape and colour. Extrinsic state is the part that differs for each use, such as where the tree stands. Intrinsic state is stored once, in a shared object called the flyweight. Extrinsic state stays with the caller and is passed in when needed.",[32,39,40],{},"A factory hands out the flyweights. When you ask for a tree with a given shape and colour, it returns the one it already has, and creates it only the first time.",[27,42,44],{"id":43},"when-to-use-it","When to use it",[46,47,48,52,55,58],"ul",{},[49,50,51],"li",{},"The program needs a very large number of similar objects, and memory is a real limit.",[49,53,54],{},"Most of each object's state can be moved out of it and passed in by the caller.",[49,56,57],{},"Once that state is moved out, many objects can be replaced by a few shared ones.",[49,59,60],{},"Your code doesn't compare these objects by identity, so sharing one instance is safe.",[27,62,64],{"id":63},"pros-and-cons","Pros and cons",[66,67,69],"h3",{"id":68},"pros","Pros",[46,71,72,75,78],{},[49,73,74],{},"It uses far less memory when there are many similar objects.",[49,76,77],{},"Existing objects are reused instead of being created again.",[49,79,80],{},"The factory gives you one place to control how many objects exist.",[66,82,84],{"id":83},"cons","Cons",[46,86,87,90,93],{},[49,88,89],{},"The code is more complex: state is split in two, and callers must keep the extrinsic part and pass it in.",[49,91,92],{},"It can trade memory for time, since extrinsic state may need to be worked out or looked up on every call.",[49,94,95],{},"A shared flyweight must not hold state that differs for each use, or the uses overwrite each other.",[27,97,99],{"id":98},"the-example","The example",[32,101,102],{},"The example draws a forest. Every tree has a shape, a colour and a position. Many trees have the same shape and colour, so those are shared. The position is passed in each time a tree is drawn.",[46,104,105,119,139,150],{},[49,106,107,114,115,118],{},[108,109,110],"strong",{},[111,112,113],"code",{},"Tree",": the flyweight interface, with ",[111,116,117],{},"render(x, y)",". The position is the extrinsic state.",[49,120,121,126,127,130,131,134,135,138],{},[108,122,123],{},[111,124,125],{},"OakTree",": the concrete flyweight. It stores the intrinsic state, ",[111,128,129],{},"shape"," and ",[111,132,133],{},"color",", in ",[111,136,137],{},"final"," fields.",[49,140,141,146,147,149],{},[108,142,143],{},[111,144,145],{},"TreeFactory",": the flyweight factory. It keeps a map keyed by shape and colour, creates an ",[111,148,125],{}," the first time a key is asked for, and returns the stored one after that.",[49,151,152,157],{},[108,153,154],{},[111,155,156],{},"Demo",": asks for three cone-shaped green trees and draws each at a different position.",[32,159,160,161,163],{},"All three calls return the same ",[111,162,125],{}," object, so the forest holds one tree object, drawn three times.",[32,165,166,168,169,130,172,175,176,179,180,182],{},[111,167,125],{}," also keeps ",[111,170,171],{},"x",[111,173,174],{},"y"," in fields, set on every ",[111,177,178],{},"render"," call. Because the object is shared, each call overwrites the last position. It works here because ",[111,181,178],{}," prints the position straight away, but in the pattern the flyweight holds only the shared state.",[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 interface Tree {\n    void render(int x, int y);\n}\n","Tree.java","java","",[111,196,197,217,249],{"__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}," interface",[198,210,212],{"class":211},"sNwy7"," Tree",[198,214,216],{"class":215},"sVG6I"," {\n",[198,218,220,223,227,230,233,237,240,243,246],{"class":200,"line":219},2,[198,221,222],{"class":204},"    void",[198,224,226],{"class":225},"sVz7n"," render",[198,228,229],{"class":215},"(",[198,231,232],{"class":204},"int",[198,234,236],{"class":235},"sWFPS"," x",[198,238,239],{"class":215},",",[198,241,242],{"class":204}," int",[198,244,245],{"class":235}," y",[198,247,248],{"class":215},");\n",[198,250,252],{"class":200,"line":251},3,[198,253,254],{"class":215},"}\n",[188,256,259],{"className":190,"code":257,"filename":258,"language":193,"meta":194,"style":194},"public class OakTree implements Tree {\n    private final String shape;\n    private final String color;\n    private int x;\n    private int y;\n\n    public OakTree(String shape, String color) {\n        this.shape = shape;\n        this.color = color;\n    }\n\n    public void setX(int x) {\n        this.x = x;\n    }\n\n    public void setY(int y) {\n        this.y = y;\n    }\n\n    public void render(int x, int y) {\n        setX(x);\n        setY(y);\n        System.out.println(\"Render Oak tree with shape: \" + shape + \" and color: \" + color + \" at x: \" + x + \" y: \" + y);\n    }\n}\n","OakTree.java",[111,260,261,278,295,308,319,330,337,363,383,398,404,409,430,445,450,455,475,490,495,500,525,537,549,609,613],{"__ignoreMap":194},[198,262,263,265,268,271,274,276],{"class":200,"line":201},[198,264,205],{"class":204},[198,266,267],{"class":204}," class",[198,269,270],{"class":211}," OakTree",[198,272,273],{"class":204}," implements",[198,275,212],{"class":211},[198,277,216],{"class":215},[198,279,280,283,286,289,292],{"class":200,"line":219},[198,281,282],{"class":204},"    private",[198,284,285],{"class":204}," final",[198,287,288],{"class":204}," String",[198,290,291],{"class":235}," shape",[198,293,294],{"class":215},";\n",[198,296,297,299,301,303,306],{"class":200,"line":251},[198,298,282],{"class":204},[198,300,285],{"class":204},[198,302,288],{"class":204},[198,304,305],{"class":235}," color",[198,307,294],{"class":215},[198,309,311,313,315,317],{"class":200,"line":310},4,[198,312,282],{"class":204},[198,314,242],{"class":204},[198,316,236],{"class":235},[198,318,294],{"class":215},[198,320,322,324,326,328],{"class":200,"line":321},5,[198,323,282],{"class":204},[198,325,242],{"class":204},[198,327,245],{"class":235},[198,329,294],{"class":215},[198,331,333],{"class":200,"line":332},6,[198,334,336],{"emptyLinePlaceholder":335},true,"\n",[198,338,340,343,345,347,350,352,354,356,358,361],{"class":200,"line":339},7,[198,341,342],{"class":204},"    public",[198,344,270],{"class":225},[198,346,229],{"class":215},[198,348,349],{"class":204},"String",[198,351,291],{"class":235},[198,353,239],{"class":215},[198,355,288],{"class":204},[198,357,305],{"class":235},[198,359,360],{"class":215},")",[198,362,216],{"class":215},[198,364,366,370,373,376,379,381],{"class":200,"line":365},8,[198,367,369],{"class":368},"sheII","        this",[198,371,372],{"class":215},".",[198,374,129],{"class":375},"s4Awn",[198,377,378],{"class":215}," =",[198,380,291],{"class":235},[198,382,294],{"class":215},[198,384,386,388,390,392,394,396],{"class":200,"line":385},9,[198,387,369],{"class":368},[198,389,372],{"class":215},[198,391,133],{"class":375},[198,393,378],{"class":215},[198,395,305],{"class":235},[198,397,294],{"class":215},[198,399,401],{"class":200,"line":400},10,[198,402,403],{"class":215},"    }\n",[198,405,407],{"class":200,"line":406},11,[198,408,336],{"emptyLinePlaceholder":335},[198,410,412,414,417,420,422,424,426,428],{"class":200,"line":411},12,[198,413,342],{"class":204},[198,415,416],{"class":204}," void",[198,418,419],{"class":225}," setX",[198,421,229],{"class":215},[198,423,232],{"class":204},[198,425,236],{"class":235},[198,427,360],{"class":215},[198,429,216],{"class":215},[198,431,433,435,437,439,441,443],{"class":200,"line":432},13,[198,434,369],{"class":368},[198,436,372],{"class":215},[198,438,171],{"class":375},[198,440,378],{"class":215},[198,442,236],{"class":235},[198,444,294],{"class":215},[198,446,448],{"class":200,"line":447},14,[198,449,403],{"class":215},[198,451,453],{"class":200,"line":452},15,[198,454,336],{"emptyLinePlaceholder":335},[198,456,458,460,462,465,467,469,471,473],{"class":200,"line":457},16,[198,459,342],{"class":204},[198,461,416],{"class":204},[198,463,464],{"class":225}," setY",[198,466,229],{"class":215},[198,468,232],{"class":204},[198,470,245],{"class":235},[198,472,360],{"class":215},[198,474,216],{"class":215},[198,476,478,480,482,484,486,488],{"class":200,"line":477},17,[198,479,369],{"class":368},[198,481,372],{"class":215},[198,483,174],{"class":375},[198,485,378],{"class":215},[198,487,245],{"class":235},[198,489,294],{"class":215},[198,491,493],{"class":200,"line":492},18,[198,494,403],{"class":215},[198,496,498],{"class":200,"line":497},19,[198,499,336],{"emptyLinePlaceholder":335},[198,501,503,505,507,509,511,513,515,517,519,521,523],{"class":200,"line":502},20,[198,504,342],{"class":204},[198,506,416],{"class":204},[198,508,226],{"class":225},[198,510,229],{"class":215},[198,512,232],{"class":204},[198,514,236],{"class":235},[198,516,239],{"class":215},[198,518,242],{"class":204},[198,520,245],{"class":235},[198,522,360],{"class":215},[198,524,216],{"class":215},[198,526,528,531,533,535],{"class":200,"line":527},21,[198,529,530],{"class":225},"        setX",[198,532,229],{"class":215},[198,534,171],{"class":235},[198,536,248],{"class":215},[198,538,540,543,545,547],{"class":200,"line":539},22,[198,541,542],{"class":225},"        setY",[198,544,229],{"class":215},[198,546,174],{"class":235},[198,548,248],{"class":215},[198,550,552,555,557,560,562,565,567,571,574,577,580,583,585,588,590,593,595,598,600,603,605,607],{"class":200,"line":551},23,[198,553,554],{"class":235},"        System",[198,556,372],{"class":215},[198,558,559],{"class":375},"out",[198,561,372],{"class":215},[198,563,564],{"class":225},"println",[198,566,229],{"class":215},[198,568,570],{"class":569},"sUOzB","\"Render Oak tree with shape: \"",[198,572,573],{"class":215}," +",[198,575,576],{"class":235}," shape ",[198,578,579],{"class":215},"+",[198,581,582],{"class":569}," \" and color: \"",[198,584,573],{"class":215},[198,586,587],{"class":235}," color ",[198,589,579],{"class":215},[198,591,592],{"class":569}," \" at x: \"",[198,594,573],{"class":215},[198,596,597],{"class":235}," x ",[198,599,579],{"class":215},[198,601,602],{"class":569}," \" y: \"",[198,604,573],{"class":215},[198,606,245],{"class":235},[198,608,248],{"class":215},[198,610,611],{"class":200,"line":5},[198,612,403],{"class":215},[198,614,616],{"class":200,"line":615},25,[198,617,254],{"class":215},[188,619,622],{"className":190,"code":620,"filename":621,"language":193,"meta":194,"style":194},"import java.util.HashMap;\nimport java.util.Map;\n\npublic class TreeFactory {\n    private final Map\u003CString, Tree> treeMap = new HashMap\u003C>();\n\n    public Tree getTree(String shape, String color) {\n        String key = shape + \"-\" + color;\n\n        if (!treeMap.containsKey(key)) {\n            treeMap.put(key, new OakTree(shape, color));\n        }\n\n        return treeMap.get(key);\n    }\n}\n","TreeFactory.java",[111,623,624,644,661,665,676,712,716,741,764,768,794,825,830,834,853,857],{"__ignoreMap":194},[198,625,626,629,632,634,637,639,642],{"class":200,"line":201},[198,627,628],{"class":204},"import",[198,630,631],{"class":204}," java",[198,633,372],{"class":215},[198,635,636],{"class":204},"util",[198,638,372],{"class":215},[198,640,641],{"class":204},"HashMap",[198,643,294],{"class":215},[198,645,646,648,650,652,654,656,659],{"class":200,"line":219},[198,647,628],{"class":204},[198,649,631],{"class":204},[198,651,372],{"class":215},[198,653,636],{"class":204},[198,655,372],{"class":215},[198,657,658],{"class":204},"Map",[198,660,294],{"class":215},[198,662,663],{"class":200,"line":251},[198,664,336],{"emptyLinePlaceholder":335},[198,666,667,669,671,674],{"class":200,"line":310},[198,668,205],{"class":204},[198,670,267],{"class":204},[198,672,673],{"class":211}," TreeFactory",[198,675,216],{"class":215},[198,677,678,680,682,685,688,690,692,694,697,700,703,706,709],{"class":200,"line":321},[198,679,282],{"class":204},[198,681,285],{"class":204},[198,683,684],{"class":204}," Map",[198,686,687],{"class":215},"\u003C",[198,689,349],{"class":204},[198,691,239],{"class":215},[198,693,212],{"class":204},[198,695,696],{"class":215},">",[198,698,699],{"class":235}," treeMap ",[198,701,702],{"class":215},"=",[198,704,705],{"class":204}," new",[198,707,708],{"class":204}," HashMap",[198,710,711],{"class":215},"\u003C>();\n",[198,713,714],{"class":200,"line":332},[198,715,336],{"emptyLinePlaceholder":335},[198,717,718,720,722,725,727,729,731,733,735,737,739],{"class":200,"line":339},[198,719,342],{"class":204},[198,721,212],{"class":204},[198,723,724],{"class":225}," getTree",[198,726,229],{"class":215},[198,728,349],{"class":204},[198,730,291],{"class":235},[198,732,239],{"class":215},[198,734,288],{"class":204},[198,736,305],{"class":235},[198,738,360],{"class":215},[198,740,216],{"class":215},[198,742,743,746,749,751,753,755,758,760,762],{"class":200,"line":365},[198,744,745],{"class":204},"        String",[198,747,748],{"class":235}," key ",[198,750,702],{"class":215},[198,752,576],{"class":235},[198,754,579],{"class":215},[198,756,757],{"class":569}," \"-\"",[198,759,573],{"class":215},[198,761,305],{"class":235},[198,763,294],{"class":215},[198,765,766],{"class":200,"line":385},[198,767,336],{"emptyLinePlaceholder":335},[198,769,770,773,776,779,781,784,786,789,792],{"class":200,"line":400},[198,771,772],{"class":204},"        if",[198,774,775],{"class":215}," (!",[198,777,778],{"class":235},"treeMap",[198,780,372],{"class":215},[198,782,783],{"class":225},"containsKey",[198,785,229],{"class":215},[198,787,788],{"class":235},"key",[198,790,791],{"class":215},"))",[198,793,216],{"class":215},[198,795,796,799,801,804,806,808,810,812,814,816,818,820,822],{"class":200,"line":406},[198,797,798],{"class":235},"            treeMap",[198,800,372],{"class":215},[198,802,803],{"class":225},"put",[198,805,229],{"class":215},[198,807,788],{"class":235},[198,809,239],{"class":215},[198,811,705],{"class":204},[198,813,270],{"class":225},[198,815,229],{"class":215},[198,817,129],{"class":235},[198,819,239],{"class":215},[198,821,305],{"class":235},[198,823,824],{"class":215},"));\n",[198,826,827],{"class":200,"line":411},[198,828,829],{"class":215},"        }\n",[198,831,832],{"class":200,"line":432},[198,833,336],{"emptyLinePlaceholder":335},[198,835,836,839,842,844,847,849,851],{"class":200,"line":447},[198,837,838],{"class":204},"        return",[198,840,841],{"class":235}," treeMap",[198,843,372],{"class":215},[198,845,846],{"class":225},"get",[198,848,229],{"class":215},[198,850,788],{"class":235},[198,852,248],{"class":215},[198,854,855],{"class":200,"line":452},[198,856,403],{"class":215},[198,858,859],{"class":200,"line":457},[198,860,254],{"class":215},[188,862,865],{"className":190,"code":863,"filename":864,"language":193,"meta":194,"style":194},"public class Demo {\n    public static void main(String[] args) {\n        TreeFactory treeFactory = new TreeFactory();\n        \n        Tree tree1 = treeFactory.getTree(\"cone\", \"green\");\n        tree1.render(1, 2);\n\n        Tree tree2 = treeFactory.getTree(\"cone\", \"green\");\n        tree2.render(3, 4);\n\n        Tree tree3 = treeFactory.getTree(\"cone\", \"green\");\n        tree3.render(5, 6);\n    }\n}\n","Demo.java",[111,866,867,878,904,921,926,956,977,981,1006,1027,1031,1056,1077,1081],{"__ignoreMap":194},[198,868,869,871,873,876],{"class":200,"line":201},[198,870,205],{"class":204},[198,872,267],{"class":204},[198,874,875],{"class":211}," Demo",[198,877,216],{"class":215},[198,879,880,882,885,887,890,892,894,897,900,902],{"class":200,"line":219},[198,881,342],{"class":204},[198,883,884],{"class":204}," static",[198,886,416],{"class":204},[198,888,889],{"class":225}," main",[198,891,229],{"class":215},[198,893,349],{"class":204},[198,895,896],{"class":215},"[]",[198,898,899],{"class":235}," args",[198,901,360],{"class":215},[198,903,216],{"class":215},[198,905,906,909,912,914,916,918],{"class":200,"line":251},[198,907,908],{"class":204},"        TreeFactory",[198,910,911],{"class":235}," treeFactory ",[198,913,702],{"class":215},[198,915,705],{"class":204},[198,917,673],{"class":225},[198,919,920],{"class":215},"();\n",[198,922,923],{"class":200,"line":310},[198,924,925],{"class":235},"        \n",[198,927,928,931,934,936,939,941,944,946,949,951,954],{"class":200,"line":321},[198,929,930],{"class":204},"        Tree",[198,932,933],{"class":235}," tree1 ",[198,935,702],{"class":215},[198,937,938],{"class":235}," treeFactory",[198,940,372],{"class":215},[198,942,943],{"class":225},"getTree",[198,945,229],{"class":215},[198,947,948],{"class":569},"\"cone\"",[198,950,239],{"class":215},[198,952,953],{"class":569}," \"green\"",[198,955,248],{"class":215},[198,957,958,961,963,965,967,970,972,975],{"class":200,"line":332},[198,959,960],{"class":235},"        tree1",[198,962,372],{"class":215},[198,964,178],{"class":225},[198,966,229],{"class":215},[198,968,969],{"class":368},"1",[198,971,239],{"class":215},[198,973,974],{"class":368}," 2",[198,976,248],{"class":215},[198,978,979],{"class":200,"line":339},[198,980,336],{"emptyLinePlaceholder":335},[198,982,983,985,988,990,992,994,996,998,1000,1002,1004],{"class":200,"line":365},[198,984,930],{"class":204},[198,986,987],{"class":235}," tree2 ",[198,989,702],{"class":215},[198,991,938],{"class":235},[198,993,372],{"class":215},[198,995,943],{"class":225},[198,997,229],{"class":215},[198,999,948],{"class":569},[198,1001,239],{"class":215},[198,1003,953],{"class":569},[198,1005,248],{"class":215},[198,1007,1008,1011,1013,1015,1017,1020,1022,1025],{"class":200,"line":385},[198,1009,1010],{"class":235},"        tree2",[198,1012,372],{"class":215},[198,1014,178],{"class":225},[198,1016,229],{"class":215},[198,1018,1019],{"class":368},"3",[198,1021,239],{"class":215},[198,1023,1024],{"class":368}," 4",[198,1026,248],{"class":215},[198,1028,1029],{"class":200,"line":400},[198,1030,336],{"emptyLinePlaceholder":335},[198,1032,1033,1035,1038,1040,1042,1044,1046,1048,1050,1052,1054],{"class":200,"line":406},[198,1034,930],{"class":204},[198,1036,1037],{"class":235}," tree3 ",[198,1039,702],{"class":215},[198,1041,938],{"class":235},[198,1043,372],{"class":215},[198,1045,943],{"class":225},[198,1047,229],{"class":215},[198,1049,948],{"class":569},[198,1051,239],{"class":215},[198,1053,953],{"class":569},[198,1055,248],{"class":215},[198,1057,1058,1061,1063,1065,1067,1070,1072,1075],{"class":200,"line":411},[198,1059,1060],{"class":235},"        tree3",[198,1062,372],{"class":215},[198,1064,178],{"class":225},[198,1066,229],{"class":215},[198,1068,1069],{"class":368},"5",[198,1071,239],{"class":215},[198,1073,1074],{"class":368}," 6",[198,1076,248],{"class":215},[198,1078,1079],{"class":200,"line":432},[198,1080,403],{"class":215},[198,1082,1083],{"class":200,"line":447},[198,1084,254],{"class":215},[32,1086,1087,1088,1090],{},"Running ",[111,1089,156],{}," prints:",[188,1092,1097],{"className":1093,"code":1095,"language":1096,"meta":194},[1094],"language-text","Render Oak tree with shape: cone and color: green at x: 1 y: 2\nRender Oak tree with shape: cone and color: green at x: 3 y: 4\nRender Oak tree with shape: cone and color: green at x: 5 y: 6\n","text",[111,1098,1095],{"__ignoreMap":194},[1100,1101,1102],"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 pre.shiki code .sWFPS, html code.shiki .sWFPS{--shiki-default:#C4C4CC;--shiki-light:#3F3F46}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 .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 pre.shiki code .sUOzB, html code.shiki .sUOzB{--shiki-default:#9EC7FE;--shiki-light:#2873D1}",{"title":194,"searchDepth":219,"depth":219,"links":1104},[1105,1106,1107,1111,1112],{"id":29,"depth":219,"text":30},{"id":43,"depth":219,"text":44},{"id":63,"depth":219,"text":64,"children":1108},[1109,1110],{"id":68,"depth":251,"text":69},{"id":83,"depth":251,"text":84},{"id":98,"depth":219,"text":99},{"id":185,"depth":219,"text":186},"The Flyweight pattern shares the common state of many similar objects, so a program can hold large numbers of them in little memory. Explained with a worked Java example of trees in a forest.","Trees in a forest that share their shape and colour","md","Structural","lucide:feather",{},"\u002Flearn\u002Fdesign-patterns\u002Fflyweight","---\ntitle: Flyweight\ngroup: Structural\nicon: lucide:feather\nsummary: 'Share the common part of many similar objects, so large numbers of them cost little memory.'\ndescription: 'The Flyweight pattern shares the common state of many similar objects, so a program can hold large numbers of them in little memory. Explained with a worked Java example of trees in a forest.'\nexample: 'Trees in a forest that share their shape and colour'\norder: 6\nsource: https:\u002F\u002Fgithub.com\u002FHaythamasalama\u002FdesignPattern\u002Ftree\u002Fmaster\u002Fsrc\u002FStructural\u002FFlyweight\n---\n\n## The idea\n\nSome programs need a huge number of small objects: trees in a game world, characters in a text editor, particles in a simulation. If each object stores all of its data, memory runs out quickly, even though most of that data is the same across objects.\n\nThe Flyweight pattern splits an object's data in two. Intrinsic state is the part that many objects have in common, such as a tree's shape and colour. Extrinsic state is the part that differs for each use, such as where the tree stands. Intrinsic state is stored once, in a shared object called the flyweight. Extrinsic state stays with the caller and is passed in when needed.\n\nA factory hands out the flyweights. When you ask for a tree with a given shape and colour, it returns the one it already has, and creates it only the first time.\n\n## When to use it\n\n- The program needs a very large number of similar objects, and memory is a real limit.\n- Most of each object's state can be moved out of it and passed in by the caller.\n- Once that state is moved out, many objects can be replaced by a few shared ones.\n- Your code doesn't compare these objects by identity, so sharing one instance is safe.\n\n## Pros and cons\n\n### Pros\n\n- It uses far less memory when there are many similar objects.\n- Existing objects are reused instead of being created again.\n- The factory gives you one place to control how many objects exist.\n\n### Cons\n\n- The code is more complex: state is split in two, and callers must keep the extrinsic part and pass it in.\n- It can trade memory for time, since extrinsic state may need to be worked out or looked up on every call.\n- A shared flyweight must not hold state that differs for each use, or the uses overwrite each other.\n\n## The example\n\nThe example draws a forest. Every tree has a shape, a colour and a position. Many trees have the same shape and colour, so those are shared. The position is passed in each time a tree is drawn.\n\n- **`Tree`**: the flyweight interface, with `render(x, y)`. The position is the extrinsic state.\n- **`OakTree`**: the concrete flyweight. It stores the intrinsic state, `shape` and `color`, in `final` fields.\n- **`TreeFactory`**: the flyweight factory. It keeps a map keyed by shape and colour, creates an `OakTree` the first time a key is asked for, and returns the stored one after that.\n- **`Demo`**: asks for three cone-shaped green trees and draws each at a different position.\n\nAll three calls return the same `OakTree` object, so the forest holds one tree object, drawn three times.\n\n`OakTree` also keeps `x` and `y` in fields, set on every `render` call. Because the object is shared, each call overwrites the last position. It works here because `render` prints the position straight away, but in the pattern the flyweight holds only the shared state.\n\n## The code\n\n```java [Tree.java]\npublic interface Tree {\n    void render(int x, int y);\n}\n```\n\n```java [OakTree.java]\npublic class OakTree implements Tree {\n    private final String shape;\n    private final String color;\n    private int x;\n    private int y;\n\n    public OakTree(String shape, String color) {\n        this.shape = shape;\n        this.color = color;\n    }\n\n    public void setX(int x) {\n        this.x = x;\n    }\n\n    public void setY(int y) {\n        this.y = y;\n    }\n\n    public void render(int x, int y) {\n        setX(x);\n        setY(y);\n        System.out.println(\"Render Oak tree with shape: \" + shape + \" and color: \" + color + \" at x: \" + x + \" y: \" + y);\n    }\n}\n```\n\n```java [TreeFactory.java]\nimport java.util.HashMap;\nimport java.util.Map;\n\npublic class TreeFactory {\n    private final Map\u003CString, Tree> treeMap = new HashMap\u003C>();\n\n    public Tree getTree(String shape, String color) {\n        String key = shape + \"-\" + color;\n\n        if (!treeMap.containsKey(key)) {\n            treeMap.put(key, new OakTree(shape, color));\n        }\n\n        return treeMap.get(key);\n    }\n}\n```\n\n```java [Demo.java]\npublic class Demo {\n    public static void main(String[] args) {\n        TreeFactory treeFactory = new TreeFactory();\n        \n        Tree tree1 = treeFactory.getTree(\"cone\", \"green\");\n        tree1.render(1, 2);\n\n        Tree tree2 = treeFactory.getTree(\"cone\", \"green\");\n        tree2.render(3, 4);\n\n        Tree tree3 = treeFactory.getTree(\"cone\", \"green\");\n        tree3.render(5, 6);\n    }\n}\n```\n\nRunning `Demo` prints:\n\n```text\nRender Oak tree with shape: cone and color: green at x: 1 y: 2\nRender Oak tree with shape: cone and color: green at x: 3 y: 4\nRender Oak tree with shape: cone and color: green at x: 5 y: 6\n```\n",{"title":22,"description":1113},{"loc":1119},"https:\u002F\u002Fgithub.com\u002FHaythamasalama\u002FdesignPattern\u002Ftree\u002Fmaster\u002Fsrc\u002FStructural\u002FFlyweight","learn\u002Fdesign-patterns\u002Fflyweight","Share the common part of many similar objects, so large numbers of them cost little memory.","kX-R6GGYX-5bSil-0dTsGIh1ITOANSNToHX4GwmvijU",[1128,1133,1138,1143,1148,1153,1154],{"path":1129,"title":1130,"icon":1131,"summary":1132,"order":201},"\u002Flearn\u002Fdesign-patterns\u002Fadapter","Adapter","lucide:plug","A thin wrapper that lets a class work with code that expects a different interface.",{"path":1134,"title":1135,"icon":1136,"summary":1137,"order":219},"\u002Flearn\u002Fdesign-patterns\u002Fbridge","Bridge","lucide:bridge","Split a class into two parts that can change on their own: what it does, and how it gets done.",{"path":1139,"title":1140,"icon":1141,"summary":1142,"order":251},"\u002Flearn\u002Fdesign-patterns\u002Fcomposite","Composite","lucide:network","Treat a single object and a group of objects the same way, by building them into a tree.",{"path":1144,"title":1145,"icon":1146,"summary":1147,"order":310},"\u002Flearn\u002Fdesign-patterns\u002Fdecorator","Decorator","lucide:gift","Wrap an object in another object that adds behaviour, without changing the original class.",{"path":1149,"title":1150,"icon":1151,"summary":1152,"order":321},"\u002Flearn\u002Fdesign-patterns\u002Ffacade","Facade","lucide:door-open","One simple class in front of a complex system, so callers talk to it instead of to every part.",{"path":1119,"title":22,"icon":1117,"summary":1125,"order":332},{"path":1155,"title":1156,"icon":1157,"summary":1158,"order":339},"\u002Flearn\u002Fdesign-patterns\u002Fproxy","Proxy","lucide:shield-check","A stand-in with the same interface as the real object, which controls when and how the real one is used.",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1160},"\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":1162},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"M2.97 12.92A2 2 0 0 0 2 14.63v3.24a2 2 0 0 0 .97 1.71l3 1.8a2 2 0 0 0 2.06 0L12 19v-5.5l-5-3zM7 16.5l-4.74-2.85M7 16.5l5-3m-5 3v5.17m5-8.17V19l3.97 2.38a2 2 0 0 0 2.06 0l3-1.8a2 2 0 0 0 .97-1.71v-3.24a2 2 0 0 0-.97-1.71L17 10.5zm5 3l-5-3m5 3l4.74-2.85M17 16.5v5.17\"\u002F>\u003Cpath d=\"M7.97 4.42A2 2 0 0 0 7 6.13v4.37l5 3l5-3V6.13a2 2 0 0 0-.97-1.71l-3-1.8a2 2 0 0 0-2.06 0zM12 8L7.26 5.15M12 8l4.74-2.85M12 13.5V8\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1164},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M12 22v-5m3-9V2m2 6a1 1 0 0 1 1 1v4a4 4 0 0 1-4 4h-4a4 4 0 0 1-4-4V9a1 1 0 0 1 1-1zM9 8V2\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1166},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M10 9.728V16m4-6.272V16m4 4V4m4 7l-4-4A7.5 7.5 0 0 1 6 7l-4 4m20 5H2m4 4V4\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1168},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Crect width=\"6\" height=\"6\" x=\"16\" y=\"16\" rx=\"1\"\u002F>\u003Crect width=\"6\" height=\"6\" x=\"2\" y=\"16\" rx=\"1\"\u002F>\u003Crect width=\"6\" height=\"6\" x=\"9\" y=\"2\" rx=\"1\"\u002F>\u003Cpath d=\"M5 16v-3a1 1 0 0 1 1-1h12a1 1 0 0 1 1 1v3m-7-4V8\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1170},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"M12 7v14m8-10v8a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2v-8m3.5-4a1 1 0 0 1 0-5A4.8 8 0 0 1 12 7a4.8 8 0 0 1 4.5-5a1 1 0 0 1 0 5\"\u002F>\u003Crect width=\"18\" height=\"4\" x=\"3\" y=\"7\" rx=\"1\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1172},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"M10 21H2m8-18H7a2 2 0 0 0-2 2v16m9-9h.01\"\u002F>\u003Cpath d=\"M19 21V5a2 2 0 0 0-1.675-1.974l-6.163-1.013A1 1 0 0 0 10 3v18a1 1 0 0 0 1.124.992zm3 0h-3\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1174},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M14.086 18.412A2 2 0 0 1 12.67 19H5v-7.672a2 2 0 0 1 .586-1.414L11.75 3.75a6 6 0 1 1 8.49 8.49zM16 8L2 22m15.488-7H9\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1176},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z\"\u002F>\u003Cpath d=\"m9 12l2 2l4-4\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1178},"\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>",1793131416681]