[{"data":1,"prerenderedAt":930},["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\u002Fadapter":20,"pattern-group:\u002Flearn\u002Fdesign-patterns\u002Fadapter":878,"i-lucide:lightbulb":910,"i-lucide:boxes":912,"i-lucide:plug":914,"i-lucide:bridge":916,"i-lucide:network":918,"i-lucide:gift":920,"i-lucide:door-open":922,"i-lucide:feather":924,"i-lucide:shield-check":926,"i-lucide:copy":928},{"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":864,"example":865,"extension":866,"group":867,"icon":868,"meta":869,"navigation":254,"order":220,"path":870,"rawbody":871,"seo":872,"sitemap":873,"source":874,"stem":875,"summary":876,"__hash__":877},"patterns\u002Flearn\u002Fdesign-patterns\u002Fadapter.md","Adapter",{"type":24,"value":25,"toc":854},"minimark",[26,31,44,47,50,54,70,74,79,90,94,102,106,117,202,206,272,315,430,530,683,836,842,850],[27,28,30],"h2",{"id":29},"the-idea","The idea",[32,33,34,35,39,40,43],"p",{},"Sometimes a class does what you need, but its methods don't match what your code expects. Your code calls ",[36,37,38],"code",{},"readData()",", and the class offers ",[36,41,42],{},"read()",". You may not be able to change either side: one is a library, or the other is used in many places.",[32,45,46],{},"An adapter sits between them. It implements the interface your code expects, holds the other object, and turns each call into a call that object understands. It works like a travel plug adapter: neither the socket nor the plug changes.",[32,48,49],{},"The Gang of Four describe two forms. A class adapter inherits from the class it adapts. An object adapter, like the one in this example, holds a reference to it. The object form fits Java better, since a class can extend only one other class.",[27,51,53],{"id":52},"when-to-use-it","When to use it",[55,56,57,61,64,67],"ul",{},[58,59,60],"li",{},"You want to use an existing class, but its interface doesn't match the one your code needs.",[58,62,63],{},"You are adding a third-party library and want to keep its API out of the rest of your code.",[58,65,66],{},"You are replacing one implementation with another that has a different interface, and don't want to change every caller.",[58,68,69],{},"Several existing classes do similar work with different method names, and you want to treat them the same way.",[27,71,73],{"id":72},"pros-and-cons","Pros and cons",[75,76,78],"h3",{"id":77},"pros","Pros",[55,80,81,84,87],{},[58,82,83],{},"You reuse existing classes without changing them.",[58,85,86],{},"It separates concerns: the conversion lives in one class, away from the business logic.",[58,88,89],{},"Client code depends only on its own interface, so you can swap the adapted class later.",[75,91,93],{"id":92},"cons","Cons",[55,95,96,99],{},[58,97,98],{},"It adds a class and a layer of indirection for every interface you adapt.",[58,100,101],{},"If the two interfaces differ a lot, the adapter can grow complex and hide real differences in behaviour.",[27,103,105],{"id":104},"the-example","The example",[32,107,108,109,112,113,116],{},"The example uses storage devices. The client works with memory cards through the ",[36,110,111],{},"MemoryCard"," interface. A flash drive offers the same kind of operations through a different interface, ",[36,114,115],{},"USBDrive",". A card reader adapter lets the client use the flash drive as if it were a memory card.",[55,118,119,134,146,157,168,191],{},[58,120,121,126,127,129,130,133],{},[122,123,124],"strong",{},[36,125,111],{},": the target: the interface the client expects, with ",[36,128,38],{}," and ",[36,131,132],{},"writeData()",".",[58,135,136,140,141,129,143,133],{},[122,137,138],{},[36,139,115],{},": the interface of the class being adapted, with ",[36,142,42],{},[36,144,145],{},"write()",[58,147,148,153,154,156],{},[122,149,150],{},[36,151,152],{},"SDMemoryCard",": an ordinary ",[36,155,111],{}," that the client uses directly.",[58,158,159,164,165,167],{},[122,160,161],{},[36,162,163],{},"FlashDrive",": the adaptee: a ",[36,166,115],{}," that the client cannot use as it is.",[58,169,170,175,176,178,179,181,182,184,185,129,187,184,189,133],{},[122,171,172],{},[36,173,174],{},"USBCardReaderAdapter",": the adapter. It implements ",[36,177,111],{},", holds a ",[36,180,115],{},", and forwards ",[36,183,38],{}," to ",[36,186,42],{},[36,188,132],{},[36,190,145],{},[58,192,193,198,199,201],{},[122,194,195],{},[36,196,197],{},"Demo",": the client. It uses the SD card and then the flash drive, both through the ",[36,200,111],{}," interface.",[27,203,205],{"id":204},"the-code","The code",[207,208,214],"pre",{"className":209,"code":210,"filename":211,"language":212,"meta":213,"style":213},"language-java shiki shiki-themes haytham-dark haytham-light","public interface MemoryCard {\n    void readData();\n\n    void writeData();\n}\n","MemoryCard.java","java","",[36,215,216,236,249,256,266],{"__ignoreMap":213},[217,218,221,225,228,232],"span",{"class":219,"line":220},"line",1,[217,222,224],{"class":223},"sVsk0","public",[217,226,227],{"class":223}," interface",[217,229,231],{"class":230},"sNwy7"," MemoryCard",[217,233,235],{"class":234},"sVG6I"," {\n",[217,237,239,242,246],{"class":219,"line":238},2,[217,240,241],{"class":223},"    void",[217,243,245],{"class":244},"sVz7n"," readData",[217,247,248],{"class":234},"();\n",[217,250,252],{"class":219,"line":251},3,[217,253,255],{"emptyLinePlaceholder":254},true,"\n",[217,257,259,261,264],{"class":219,"line":258},4,[217,260,241],{"class":223},[217,262,263],{"class":244}," writeData",[217,265,248],{"class":234},[217,267,269],{"class":219,"line":268},5,[217,270,271],{"class":234},"}\n",[207,273,276],{"className":209,"code":274,"filename":275,"language":212,"meta":213,"style":213},"public interface USBDrive {\n    void read();\n\n    void write();\n}\n","USBDrive.java",[36,277,278,289,298,302,311],{"__ignoreMap":213},[217,279,280,282,284,287],{"class":219,"line":220},[217,281,224],{"class":223},[217,283,227],{"class":223},[217,285,286],{"class":230}," USBDrive",[217,288,235],{"class":234},[217,290,291,293,296],{"class":219,"line":238},[217,292,241],{"class":223},[217,294,295],{"class":244}," read",[217,297,248],{"class":234},[217,299,300],{"class":219,"line":251},[217,301,255],{"emptyLinePlaceholder":254},[217,303,304,306,309],{"class":219,"line":258},[217,305,241],{"class":223},[217,307,308],{"class":244}," write",[217,310,248],{"class":234},[217,312,313],{"class":219,"line":268},[217,314,271],{"class":234},[207,316,319],{"className":209,"code":317,"filename":318,"language":212,"meta":213,"style":213},"class SDMemoryCard implements MemoryCard {\n    public void readData() {\n        System.out.println(\"Reading data from SD memory card\");\n    }\n\n    public void writeData() {\n        System.out.println(\"Writing data to SD memory card\");\n    }\n}\n","SDMemoryCard.java",[36,320,321,336,351,378,383,387,400,420,425],{"__ignoreMap":213},[217,322,323,326,329,332,334],{"class":219,"line":220},[217,324,325],{"class":223},"class",[217,327,328],{"class":230}," SDMemoryCard",[217,330,331],{"class":223}," implements",[217,333,231],{"class":230},[217,335,235],{"class":234},[217,337,338,341,344,346,349],{"class":219,"line":238},[217,339,340],{"class":223},"    public",[217,342,343],{"class":223}," void",[217,345,245],{"class":244},[217,347,348],{"class":234},"()",[217,350,235],{"class":234},[217,352,353,357,359,363,365,368,371,375],{"class":219,"line":251},[217,354,356],{"class":355},"sWFPS","        System",[217,358,133],{"class":234},[217,360,362],{"class":361},"s4Awn","out",[217,364,133],{"class":234},[217,366,367],{"class":244},"println",[217,369,370],{"class":234},"(",[217,372,374],{"class":373},"sUOzB","\"Reading data from SD memory card\"",[217,376,377],{"class":234},");\n",[217,379,380],{"class":219,"line":258},[217,381,382],{"class":234},"    }\n",[217,384,385],{"class":219,"line":268},[217,386,255],{"emptyLinePlaceholder":254},[217,388,390,392,394,396,398],{"class":219,"line":389},6,[217,391,340],{"class":223},[217,393,343],{"class":223},[217,395,263],{"class":244},[217,397,348],{"class":234},[217,399,235],{"class":234},[217,401,403,405,407,409,411,413,415,418],{"class":219,"line":402},7,[217,404,356],{"class":355},[217,406,133],{"class":234},[217,408,362],{"class":361},[217,410,133],{"class":234},[217,412,367],{"class":244},[217,414,370],{"class":234},[217,416,417],{"class":373},"\"Writing data to SD memory card\"",[217,419,377],{"class":234},[217,421,423],{"class":219,"line":422},8,[217,424,382],{"class":234},[217,426,428],{"class":219,"line":427},9,[217,429,271],{"class":234},[207,431,434],{"className":209,"code":432,"filename":433,"language":212,"meta":213,"style":213},"public class FlashDrive implements USBDrive {\n    public void read() {\n        System.out.println(\"Reading data from flash drive\");\n    }\n\n    public void write() {\n        System.out.println(\"Writing data to flash drive\");\n    }\n}\n","FlashDrive.java",[36,435,436,452,464,483,487,491,503,522,526],{"__ignoreMap":213},[217,437,438,440,443,446,448,450],{"class":219,"line":220},[217,439,224],{"class":223},[217,441,442],{"class":223}," class",[217,444,445],{"class":230}," FlashDrive",[217,447,331],{"class":223},[217,449,286],{"class":230},[217,451,235],{"class":234},[217,453,454,456,458,460,462],{"class":219,"line":238},[217,455,340],{"class":223},[217,457,343],{"class":223},[217,459,295],{"class":244},[217,461,348],{"class":234},[217,463,235],{"class":234},[217,465,466,468,470,472,474,476,478,481],{"class":219,"line":251},[217,467,356],{"class":355},[217,469,133],{"class":234},[217,471,362],{"class":361},[217,473,133],{"class":234},[217,475,367],{"class":244},[217,477,370],{"class":234},[217,479,480],{"class":373},"\"Reading data from flash drive\"",[217,482,377],{"class":234},[217,484,485],{"class":219,"line":258},[217,486,382],{"class":234},[217,488,489],{"class":219,"line":268},[217,490,255],{"emptyLinePlaceholder":254},[217,492,493,495,497,499,501],{"class":219,"line":389},[217,494,340],{"class":223},[217,496,343],{"class":223},[217,498,308],{"class":244},[217,500,348],{"class":234},[217,502,235],{"class":234},[217,504,505,507,509,511,513,515,517,520],{"class":219,"line":402},[217,506,356],{"class":355},[217,508,133],{"class":234},[217,510,362],{"class":361},[217,512,133],{"class":234},[217,514,367],{"class":244},[217,516,370],{"class":234},[217,518,519],{"class":373},"\"Writing data to flash drive\"",[217,521,377],{"class":234},[217,523,524],{"class":219,"line":422},[217,525,382],{"class":234},[217,527,528],{"class":219,"line":427},[217,529,271],{"class":234},[207,531,534],{"className":209,"code":532,"filename":533,"language":212,"meta":213,"style":213},"public class USBCardReaderAdapter implements MemoryCard {\n    private final USBDrive flashDrive;\n\n    public USBCardReaderAdapter(USBDrive flashDrive) {\n        this.flashDrive = flashDrive;\n    }\n\n    public void readData() {\n        flashDrive.read();\n    }\n\n    public void writeData() {\n        flashDrive.write();\n    }\n}\n","USBCardReaderAdapter.java",[36,535,536,551,567,571,588,606,610,614,626,638,643,648,661,673,678],{"__ignoreMap":213},[217,537,538,540,542,545,547,549],{"class":219,"line":220},[217,539,224],{"class":223},[217,541,442],{"class":223},[217,543,544],{"class":230}," USBCardReaderAdapter",[217,546,331],{"class":223},[217,548,231],{"class":230},[217,550,235],{"class":234},[217,552,553,556,559,561,564],{"class":219,"line":238},[217,554,555],{"class":223},"    private",[217,557,558],{"class":223}," final",[217,560,286],{"class":223},[217,562,563],{"class":355}," flashDrive",[217,565,566],{"class":234},";\n",[217,568,569],{"class":219,"line":251},[217,570,255],{"emptyLinePlaceholder":254},[217,572,573,575,577,579,581,583,586],{"class":219,"line":258},[217,574,340],{"class":223},[217,576,544],{"class":244},[217,578,370],{"class":234},[217,580,115],{"class":223},[217,582,563],{"class":355},[217,584,585],{"class":234},")",[217,587,235],{"class":234},[217,589,590,594,596,599,602,604],{"class":219,"line":268},[217,591,593],{"class":592},"sheII","        this",[217,595,133],{"class":234},[217,597,598],{"class":361},"flashDrive",[217,600,601],{"class":234}," =",[217,603,563],{"class":355},[217,605,566],{"class":234},[217,607,608],{"class":219,"line":389},[217,609,382],{"class":234},[217,611,612],{"class":219,"line":402},[217,613,255],{"emptyLinePlaceholder":254},[217,615,616,618,620,622,624],{"class":219,"line":422},[217,617,340],{"class":223},[217,619,343],{"class":223},[217,621,245],{"class":244},[217,623,348],{"class":234},[217,625,235],{"class":234},[217,627,628,631,633,636],{"class":219,"line":427},[217,629,630],{"class":355},"        flashDrive",[217,632,133],{"class":234},[217,634,635],{"class":244},"read",[217,637,248],{"class":234},[217,639,641],{"class":219,"line":640},10,[217,642,382],{"class":234},[217,644,646],{"class":219,"line":645},11,[217,647,255],{"emptyLinePlaceholder":254},[217,649,651,653,655,657,659],{"class":219,"line":650},12,[217,652,340],{"class":223},[217,654,343],{"class":223},[217,656,263],{"class":244},[217,658,348],{"class":234},[217,660,235],{"class":234},[217,662,664,666,668,671],{"class":219,"line":663},13,[217,665,630],{"class":355},[217,667,133],{"class":234},[217,669,670],{"class":244},"write",[217,672,248],{"class":234},[217,674,676],{"class":219,"line":675},14,[217,677,382],{"class":234},[217,679,681],{"class":219,"line":680},15,[217,682,271],{"class":234},[207,684,687],{"className":209,"code":685,"filename":686,"language":212,"meta":213,"style":213},"public class Demo {\n    public static void main(String[] args) {\n        MemoryCard sdCard = new SDMemoryCard();\n        sdCard.readData();\n        sdCard.writeData();\n\n        USBDrive flashDrive = new FlashDrive();\n        MemoryCard adapter = new USBCardReaderAdapter(flashDrive);\n        adapter.readData();\n        adapter.writeData();\n    }\n}\n","Demo.java",[36,688,689,700,727,745,757,768,772,788,807,818,828,832],{"__ignoreMap":213},[217,690,691,693,695,698],{"class":219,"line":220},[217,692,224],{"class":223},[217,694,442],{"class":223},[217,696,697],{"class":230}," Demo",[217,699,235],{"class":234},[217,701,702,704,707,709,712,714,717,720,723,725],{"class":219,"line":238},[217,703,340],{"class":223},[217,705,706],{"class":223}," static",[217,708,343],{"class":223},[217,710,711],{"class":244}," main",[217,713,370],{"class":234},[217,715,716],{"class":223},"String",[217,718,719],{"class":234},"[]",[217,721,722],{"class":355}," args",[217,724,585],{"class":234},[217,726,235],{"class":234},[217,728,729,732,735,738,741,743],{"class":219,"line":251},[217,730,731],{"class":223},"        MemoryCard",[217,733,734],{"class":355}," sdCard ",[217,736,737],{"class":234},"=",[217,739,740],{"class":223}," new",[217,742,328],{"class":244},[217,744,248],{"class":234},[217,746,747,750,752,755],{"class":219,"line":258},[217,748,749],{"class":355},"        sdCard",[217,751,133],{"class":234},[217,753,754],{"class":244},"readData",[217,756,248],{"class":234},[217,758,759,761,763,766],{"class":219,"line":268},[217,760,749],{"class":355},[217,762,133],{"class":234},[217,764,765],{"class":244},"writeData",[217,767,248],{"class":234},[217,769,770],{"class":219,"line":389},[217,771,255],{"emptyLinePlaceholder":254},[217,773,774,777,780,782,784,786],{"class":219,"line":402},[217,775,776],{"class":223},"        USBDrive",[217,778,779],{"class":355}," flashDrive ",[217,781,737],{"class":234},[217,783,740],{"class":223},[217,785,445],{"class":244},[217,787,248],{"class":234},[217,789,790,792,795,797,799,801,803,805],{"class":219,"line":422},[217,791,731],{"class":223},[217,793,794],{"class":355}," adapter ",[217,796,737],{"class":234},[217,798,740],{"class":223},[217,800,544],{"class":244},[217,802,370],{"class":234},[217,804,598],{"class":355},[217,806,377],{"class":234},[217,808,809,812,814,816],{"class":219,"line":427},[217,810,811],{"class":355},"        adapter",[217,813,133],{"class":234},[217,815,754],{"class":244},[217,817,248],{"class":234},[217,819,820,822,824,826],{"class":219,"line":640},[217,821,811],{"class":355},[217,823,133],{"class":234},[217,825,765],{"class":244},[217,827,248],{"class":234},[217,829,830],{"class":219,"line":645},[217,831,382],{"class":234},[217,833,834],{"class":219,"line":650},[217,835,271],{"class":234},[32,837,838,839,841],{},"Running ",[36,840,197],{}," prints:",[207,843,848],{"className":844,"code":846,"language":847,"meta":213},[845],"language-text","Reading data from SD memory card\nWriting data to SD memory card\nReading data from flash drive\nWriting data to flash drive\n","text",[36,849,846],{"__ignoreMap":213},[851,852,853],"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":213,"searchDepth":238,"depth":238,"links":855},[856,857,858,862,863],{"id":29,"depth":238,"text":30},{"id":52,"depth":238,"text":53},{"id":72,"depth":238,"text":73,"children":859},[860,861],{"id":77,"depth":251,"text":78},{"id":92,"depth":251,"text":93},{"id":104,"depth":238,"text":105},{"id":204,"depth":238,"text":205},"The Adapter pattern wraps a class so it matches the interface your code expects, letting two incompatible parts work together without changing either. Explained with a worked Java example of a card reader.","A USB drive used through a memory card interface","md","Structural","lucide:plug",{},"\u002Flearn\u002Fdesign-patterns\u002Fadapter","---\ntitle: Adapter\ngroup: Structural\nicon: lucide:plug\nsummary: 'A thin wrapper that lets a class work with code that expects a different interface.'\ndescription: 'The Adapter pattern wraps a class so it matches the interface your code expects, letting two incompatible parts work together without changing either. Explained with a worked Java example of a card reader.'\nexample: 'A USB drive used through a memory card interface'\norder: 1\nsource: https:\u002F\u002Fgithub.com\u002FHaythamasalama\u002FdesignPattern\u002Ftree\u002Fmaster\u002Fsrc\u002FStructural\u002FAdapter\n---\n\n## The idea\n\nSometimes a class does what you need, but its methods don't match what your code expects. Your code calls `readData()`, and the class offers `read()`. You may not be able to change either side: one is a library, or the other is used in many places.\n\nAn adapter sits between them. It implements the interface your code expects, holds the other object, and turns each call into a call that object understands. It works like a travel plug adapter: neither the socket nor the plug changes.\n\nThe Gang of Four describe two forms. A class adapter inherits from the class it adapts. An object adapter, like the one in this example, holds a reference to it. The object form fits Java better, since a class can extend only one other class.\n\n## When to use it\n\n- You want to use an existing class, but its interface doesn't match the one your code needs.\n- You are adding a third-party library and want to keep its API out of the rest of your code.\n- You are replacing one implementation with another that has a different interface, and don't want to change every caller.\n- Several existing classes do similar work with different method names, and you want to treat them the same way.\n\n## Pros and cons\n\n### Pros\n\n- You reuse existing classes without changing them.\n- It separates concerns: the conversion lives in one class, away from the business logic.\n- Client code depends only on its own interface, so you can swap the adapted class later.\n\n### Cons\n\n- It adds a class and a layer of indirection for every interface you adapt.\n- If the two interfaces differ a lot, the adapter can grow complex and hide real differences in behaviour.\n\n## The example\n\nThe example uses storage devices. The client works with memory cards through the `MemoryCard` interface. A flash drive offers the same kind of operations through a different interface, `USBDrive`. A card reader adapter lets the client use the flash drive as if it were a memory card.\n\n- **`MemoryCard`**: the target: the interface the client expects, with `readData()` and `writeData()`.\n- **`USBDrive`**: the interface of the class being adapted, with `read()` and `write()`.\n- **`SDMemoryCard`**: an ordinary `MemoryCard` that the client uses directly.\n- **`FlashDrive`**: the adaptee: a `USBDrive` that the client cannot use as it is.\n- **`USBCardReaderAdapter`**: the adapter. It implements `MemoryCard`, holds a `USBDrive`, and forwards `readData()` to `read()` and `writeData()` to `write()`.\n- **`Demo`**: the client. It uses the SD card and then the flash drive, both through the `MemoryCard` interface.\n\n## The code\n\n```java [MemoryCard.java]\npublic interface MemoryCard {\n    void readData();\n\n    void writeData();\n}\n```\n\n```java [USBDrive.java]\npublic interface USBDrive {\n    void read();\n\n    void write();\n}\n```\n\n```java [SDMemoryCard.java]\nclass SDMemoryCard implements MemoryCard {\n    public void readData() {\n        System.out.println(\"Reading data from SD memory card\");\n    }\n\n    public void writeData() {\n        System.out.println(\"Writing data to SD memory card\");\n    }\n}\n```\n\n```java [FlashDrive.java]\npublic class FlashDrive implements USBDrive {\n    public void read() {\n        System.out.println(\"Reading data from flash drive\");\n    }\n\n    public void write() {\n        System.out.println(\"Writing data to flash drive\");\n    }\n}\n```\n\n```java [USBCardReaderAdapter.java]\npublic class USBCardReaderAdapter implements MemoryCard {\n    private final USBDrive flashDrive;\n\n    public USBCardReaderAdapter(USBDrive flashDrive) {\n        this.flashDrive = flashDrive;\n    }\n\n    public void readData() {\n        flashDrive.read();\n    }\n\n    public void writeData() {\n        flashDrive.write();\n    }\n}\n```\n\n```java [Demo.java]\npublic class Demo {\n    public static void main(String[] args) {\n        MemoryCard sdCard = new SDMemoryCard();\n        sdCard.readData();\n        sdCard.writeData();\n\n        USBDrive flashDrive = new FlashDrive();\n        MemoryCard adapter = new USBCardReaderAdapter(flashDrive);\n        adapter.readData();\n        adapter.writeData();\n    }\n}\n```\n\nRunning `Demo` prints:\n\n```text\nReading data from SD memory card\nWriting data to SD memory card\nReading data from flash drive\nWriting data to flash drive\n```\n",{"title":22,"description":864},{"loc":870},"https:\u002F\u002Fgithub.com\u002FHaythamasalama\u002FdesignPattern\u002Ftree\u002Fmaster\u002Fsrc\u002FStructural\u002FAdapter","learn\u002Fdesign-patterns\u002Fadapter","A thin wrapper that lets a class work with code that expects a different interface.","_CUaWmHDzt7kGT1VsIe7kHvWZKz9zAoIlbPhU4L7aF8",[879,880,885,890,895,900,905],{"path":870,"title":22,"icon":868,"summary":876,"order":220},{"path":881,"title":882,"icon":883,"summary":884,"order":238},"\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":886,"title":887,"icon":888,"summary":889,"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":891,"title":892,"icon":893,"summary":894,"order":258},"\u002Flearn\u002Fdesign-patterns\u002Fdecorator","Decorator","lucide:gift","Wrap an object in another object that adds behaviour, without changing the original class.",{"path":896,"title":897,"icon":898,"summary":899,"order":268},"\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":901,"title":902,"icon":903,"summary":904,"order":389},"\u002Flearn\u002Fdesign-patterns\u002Fflyweight","Flyweight","lucide:feather","Share the common part of many similar objects, so large numbers of them cost little memory.",{"path":906,"title":907,"icon":908,"summary":909,"order":402},"\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":911},"\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":913},"\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":915},"\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":917},"\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":919},"\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":921},"\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":923},"\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":925},"\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":927},"\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":929},"\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>",1793131416482]