[{"data":1,"prerenderedAt":806},["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\u002Fbridge":20,"pattern-group:\u002Flearn\u002Fdesign-patterns\u002Fbridge":754,"i-lucide:lightbulb":786,"i-lucide:boxes":788,"i-lucide:plug":790,"i-lucide:bridge":792,"i-lucide:network":794,"i-lucide:gift":796,"i-lucide:door-open":798,"i-lucide:feather":800,"i-lucide:shield-check":802,"i-lucide:copy":804},{"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":740,"example":741,"extension":742,"group":743,"icon":744,"meta":745,"navigation":283,"order":217,"path":746,"rawbody":747,"seo":748,"sitemap":749,"source":750,"stem":751,"summary":752,"__hash__":753},"patterns\u002Flearn\u002Fdesign-patterns\u002Fbridge.md","Bridge",{"type":24,"value":25,"toc":730},"minimark",[26,31,44,47,50,54,70,74,79,90,94,102,106,109,184,188,244,355,445,523,623,712,718,726],[27,28,30],"h2",{"id":29},"the-idea","The idea",[32,33,34,35,39,40,43],"p",{},"Say you have two kinds of application and two platforms. With inheritance alone you need a class for every pair: ",[36,37,38],"code",{},"ApplicationAOnWindows",", ",[36,41,42],{},"ApplicationAOnMac",", and so on. Add a third platform and the number of classes grows again. Two separate ideas, which application and which platform, are tangled in one hierarchy.",[32,45,46],{},"The Bridge pattern splits them into two hierarchies. One is the abstraction, the part the client works with: here, the application. The other is the implementation, the part that does the low-level work: here, the platform. The abstraction holds a reference to an implementation and passes work to it. That reference is the bridge.",[32,48,49],{},"Now each side can grow on its own. A new application is one class, a new platform is one class, and any application can run on any platform.",[27,51,53],{"id":52},"when-to-use-it","When to use it",[55,56,57,61,64,67],"ul",{},[58,59,60],"li",{},"A class varies in two independent ways, and a subclass for every combination would multiply classes.",[58,62,63],{},"You want to choose or switch the implementation at runtime.",[58,65,66],{},"You want to change the implementation without touching the code that uses the abstraction.",[58,68,69],{},"Your code must run on several platforms, databases or rendering back ends.",[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],{},"It separates concerns: high-level logic sits in the abstraction, platform details in the implementation.",[58,85,86],{},"You can extend the implementation easily: a new platform needs no change to any application.",[58,88,89],{},"Abstractions and implementations can be combined freely at runtime.",[75,91,93],{"id":92},"cons","Cons",[55,95,96,99],{},[58,97,98],{},"It adds indirection and more types, which isn't worth it for a class that varies in only one way.",[58,100,101],{},"You need to find the right split early. Splitting along the wrong line makes both sides harder to change.",[27,103,105],{"id":104},"the-example","The example",[32,107,108],{},"The example runs applications on operating systems. An application knows what it wants to do. A platform knows how to run code on one operating system.",[55,110,111,128,142,152,166],{},[58,112,113,119,120,123,124,127],{},[114,115,116],"strong",{},[36,117,118],{},"Application",": the abstraction. An abstract class that holds a ",[36,121,122],{},"Platform"," and declares ",[36,125,126],{},"develop()",".",[58,129,130,135,136,138,139,127],{},[114,131,132],{},[36,133,134],{},"ApplicationA",": a refined abstraction. Its ",[36,137,126],{}," hands the work to the platform with ",[36,140,141],{},"platform.execute(\"Application A\")",[58,143,144,148,149,127],{},[114,145,146],{},[36,147,122],{},": the implementor interface, with one method, ",[36,150,151],{},"execute()",[58,153,154,159,160,165],{},[114,155,156],{},[36,157,158],{},"Windows"," and ",[114,161,162],{},[36,163,164],{},"Mac",": concrete implementors. Each runs the code on its own operating system.",[58,167,168,173,174,176,177,179,180,183],{},[114,169,170],{},[36,171,172],{},"Demo",": the client. It pairs ",[36,175,134],{}," with ",[36,178,158],{},". Passing ",[36,181,182],{},"new Mac()"," instead is the only change needed to run it on a Mac.",[27,185,187],{"id":186},"the-code","The code",[189,190,196],"pre",{"className":191,"code":192,"filename":193,"language":194,"meta":195,"style":195},"language-java shiki shiki-themes haytham-dark haytham-light","interface Platform {\n    void execute(String code);\n}\n","Platform.java","java","",[36,197,198,215,238],{"__ignoreMap":195},[199,200,203,207,211],"span",{"class":201,"line":202},"line",1,[199,204,206],{"class":205},"sVsk0","interface",[199,208,210],{"class":209},"sNwy7"," Platform",[199,212,214],{"class":213},"sVG6I"," {\n",[199,216,218,221,225,228,231,235],{"class":201,"line":217},2,[199,219,220],{"class":205},"    void",[199,222,224],{"class":223},"sVz7n"," execute",[199,226,227],{"class":213},"(",[199,229,230],{"class":205},"String",[199,232,234],{"class":233},"sWFPS"," code",[199,236,237],{"class":213},");\n",[199,239,241],{"class":201,"line":240},3,[199,242,243],{"class":213},"}\n",[189,245,248],{"className":191,"code":246,"filename":247,"language":194,"meta":195,"style":195},"public abstract class Application {\n    protected Platform platform;\n\n    public Application(Platform platform) {\n        this.platform = platform;\n    }\n\n    abstract void develop();\n}\n","Application.java",[36,249,250,266,279,285,304,324,330,335,350],{"__ignoreMap":195},[199,251,252,255,258,261,264],{"class":201,"line":202},[199,253,254],{"class":205},"public",[199,256,257],{"class":205}," abstract",[199,259,260],{"class":205}," class",[199,262,263],{"class":209}," Application",[199,265,214],{"class":213},[199,267,268,271,273,276],{"class":201,"line":217},[199,269,270],{"class":205},"    protected",[199,272,210],{"class":205},[199,274,275],{"class":233}," platform",[199,277,278],{"class":213},";\n",[199,280,281],{"class":201,"line":240},[199,282,284],{"emptyLinePlaceholder":283},true,"\n",[199,286,288,291,293,295,297,299,302],{"class":201,"line":287},4,[199,289,290],{"class":205},"    public",[199,292,263],{"class":223},[199,294,227],{"class":213},[199,296,122],{"class":205},[199,298,275],{"class":233},[199,300,301],{"class":213},")",[199,303,214],{"class":213},[199,305,307,311,313,317,320,322],{"class":201,"line":306},5,[199,308,310],{"class":309},"sheII","        this",[199,312,127],{"class":213},[199,314,316],{"class":315},"s4Awn","platform",[199,318,319],{"class":213}," =",[199,321,275],{"class":233},[199,323,278],{"class":213},[199,325,327],{"class":201,"line":326},6,[199,328,329],{"class":213},"    }\n",[199,331,333],{"class":201,"line":332},7,[199,334,284],{"emptyLinePlaceholder":283},[199,336,338,341,344,347],{"class":201,"line":337},8,[199,339,340],{"class":205},"    abstract",[199,342,343],{"class":205}," void",[199,345,346],{"class":223}," develop",[199,348,349],{"class":213},"();\n",[199,351,353],{"class":201,"line":352},9,[199,354,243],{"class":213},[189,356,359],{"className":191,"code":357,"filename":358,"language":194,"meta":195,"style":195},"class Windows implements Platform {\n    @Override\n    public void execute(String code) {\n        System.out.println(\"Running \" + code + \" on Windows\");\n    }\n}\n","Windows.java",[36,360,361,376,384,402,437,441],{"__ignoreMap":195},[199,362,363,366,369,372,374],{"class":201,"line":202},[199,364,365],{"class":205},"class",[199,367,368],{"class":209}," Windows",[199,370,371],{"class":205}," implements",[199,373,210],{"class":209},[199,375,214],{"class":213},[199,377,378,381],{"class":201,"line":217},[199,379,380],{"class":213},"    @",[199,382,383],{"class":205},"Override\n",[199,385,386,388,390,392,394,396,398,400],{"class":201,"line":240},[199,387,290],{"class":205},[199,389,343],{"class":205},[199,391,224],{"class":223},[199,393,227],{"class":213},[199,395,230],{"class":205},[199,397,234],{"class":233},[199,399,301],{"class":213},[199,401,214],{"class":213},[199,403,404,407,409,412,414,417,419,423,426,429,432,435],{"class":201,"line":287},[199,405,406],{"class":233},"        System",[199,408,127],{"class":213},[199,410,411],{"class":315},"out",[199,413,127],{"class":213},[199,415,416],{"class":223},"println",[199,418,227],{"class":213},[199,420,422],{"class":421},"sUOzB","\"Running \"",[199,424,425],{"class":213}," +",[199,427,428],{"class":233}," code ",[199,430,431],{"class":213},"+",[199,433,434],{"class":421}," \" on Windows\"",[199,436,237],{"class":213},[199,438,439],{"class":201,"line":306},[199,440,329],{"class":213},[199,442,443],{"class":201,"line":326},[199,444,243],{"class":213},[189,446,449],{"className":191,"code":447,"filename":448,"language":194,"meta":195,"style":195},"class Mac implements Platform {\n    @Override\n    public void execute(String code) {\n        System.out.println(\"Running \" + code + \" on MAC\");\n    }\n}\n","Mac.java",[36,450,451,464,470,488,515,519],{"__ignoreMap":195},[199,452,453,455,458,460,462],{"class":201,"line":202},[199,454,365],{"class":205},[199,456,457],{"class":209}," Mac",[199,459,371],{"class":205},[199,461,210],{"class":209},[199,463,214],{"class":213},[199,465,466,468],{"class":201,"line":217},[199,467,380],{"class":213},[199,469,383],{"class":205},[199,471,472,474,476,478,480,482,484,486],{"class":201,"line":240},[199,473,290],{"class":205},[199,475,343],{"class":205},[199,477,224],{"class":223},[199,479,227],{"class":213},[199,481,230],{"class":205},[199,483,234],{"class":233},[199,485,301],{"class":213},[199,487,214],{"class":213},[199,489,490,492,494,496,498,500,502,504,506,508,510,513],{"class":201,"line":287},[199,491,406],{"class":233},[199,493,127],{"class":213},[199,495,411],{"class":315},[199,497,127],{"class":213},[199,499,416],{"class":223},[199,501,227],{"class":213},[199,503,422],{"class":421},[199,505,425],{"class":213},[199,507,428],{"class":233},[199,509,431],{"class":213},[199,511,512],{"class":421}," \" on MAC\"",[199,514,237],{"class":213},[199,516,517],{"class":201,"line":306},[199,518,329],{"class":213},[199,520,521],{"class":201,"line":326},[199,522,243],{"class":213},[189,524,527],{"className":191,"code":525,"filename":526,"language":194,"meta":195,"style":195},"public class ApplicationA extends Application {\n    public ApplicationA(Platform platform) {\n        super(platform);\n    }\n\n    @Override\n    void develop() {\n        platform.execute(\"Application A\");\n    }\n}\n","ApplicationA.java",[36,528,529,545,561,572,576,580,586,597,614,618],{"__ignoreMap":195},[199,530,531,533,535,538,541,543],{"class":201,"line":202},[199,532,254],{"class":205},[199,534,260],{"class":205},[199,536,537],{"class":209}," ApplicationA",[199,539,540],{"class":205}," extends",[199,542,263],{"class":209},[199,544,214],{"class":213},[199,546,547,549,551,553,555,557,559],{"class":201,"line":217},[199,548,290],{"class":205},[199,550,537],{"class":223},[199,552,227],{"class":213},[199,554,122],{"class":205},[199,556,275],{"class":233},[199,558,301],{"class":213},[199,560,214],{"class":213},[199,562,563,566,568,570],{"class":201,"line":240},[199,564,565],{"class":309},"        super",[199,567,227],{"class":213},[199,569,316],{"class":233},[199,571,237],{"class":213},[199,573,574],{"class":201,"line":287},[199,575,329],{"class":213},[199,577,578],{"class":201,"line":306},[199,579,284],{"emptyLinePlaceholder":283},[199,581,582,584],{"class":201,"line":326},[199,583,380],{"class":213},[199,585,383],{"class":205},[199,587,588,590,592,595],{"class":201,"line":332},[199,589,220],{"class":205},[199,591,346],{"class":223},[199,593,594],{"class":213},"()",[199,596,214],{"class":213},[199,598,599,602,604,607,609,612],{"class":201,"line":337},[199,600,601],{"class":233},"        platform",[199,603,127],{"class":213},[199,605,606],{"class":223},"execute",[199,608,227],{"class":213},[199,610,611],{"class":421},"\"Application A\"",[199,613,237],{"class":213},[199,615,616],{"class":201,"line":352},[199,617,329],{"class":213},[199,619,621],{"class":201,"line":620},10,[199,622,243],{"class":213},[189,624,627],{"className":191,"code":625,"filename":626,"language":194,"meta":195,"style":195},"public class Demo {\n    public static void main(String[] args) {\n        Application app = new ApplicationA(new Windows());\n        app.develop();\n    }\n}\n","Demo.java",[36,628,629,640,666,692,704,708],{"__ignoreMap":195},[199,630,631,633,635,638],{"class":201,"line":202},[199,632,254],{"class":205},[199,634,260],{"class":205},[199,636,637],{"class":209}," Demo",[199,639,214],{"class":213},[199,641,642,644,647,649,652,654,656,659,662,664],{"class":201,"line":217},[199,643,290],{"class":205},[199,645,646],{"class":205}," static",[199,648,343],{"class":205},[199,650,651],{"class":223}," main",[199,653,227],{"class":213},[199,655,230],{"class":205},[199,657,658],{"class":213},"[]",[199,660,661],{"class":233}," args",[199,663,301],{"class":213},[199,665,214],{"class":213},[199,667,668,671,674,677,680,682,684,687,689],{"class":201,"line":240},[199,669,670],{"class":205},"        Application",[199,672,673],{"class":233}," app ",[199,675,676],{"class":213},"=",[199,678,679],{"class":205}," new",[199,681,537],{"class":223},[199,683,227],{"class":213},[199,685,686],{"class":205},"new",[199,688,368],{"class":223},[199,690,691],{"class":213},"());\n",[199,693,694,697,699,702],{"class":201,"line":287},[199,695,696],{"class":233},"        app",[199,698,127],{"class":213},[199,700,701],{"class":223},"develop",[199,703,349],{"class":213},[199,705,706],{"class":201,"line":306},[199,707,329],{"class":213},[199,709,710],{"class":201,"line":326},[199,711,243],{"class":213},[32,713,714,715,717],{},"Running ",[36,716,172],{}," prints:",[189,719,724],{"className":720,"code":722,"language":723,"meta":195},[721],"language-text","Running Application A on Windows\n","text",[36,725,722],{"__ignoreMap":195},[727,728,729],"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":195,"searchDepth":217,"depth":217,"links":731},[732,733,734,738,739],{"id":29,"depth":217,"text":30},{"id":52,"depth":217,"text":53},{"id":72,"depth":217,"text":73,"children":735},[736,737],{"id":77,"depth":240,"text":78},{"id":92,"depth":240,"text":93},{"id":104,"depth":217,"text":105},{"id":186,"depth":217,"text":187},"The Bridge pattern splits an abstraction from its implementation so each side can grow on its own, without a class for every pair. Explained with a worked Java example of apps that run on two platforms.","An application that runs on Windows or Mac","md","Structural","lucide:bridge",{},"\u002Flearn\u002Fdesign-patterns\u002Fbridge","---\ntitle: Bridge\ngroup: Structural\nicon: lucide:bridge\nsummary: 'Split a class into two parts that can change on their own: what it does, and how it gets done.'\ndescription: 'The Bridge pattern splits an abstraction from its implementation so each side can grow on its own, without a class for every pair. Explained with a worked Java example of apps that run on two platforms.'\nexample: 'An application that runs on Windows or Mac'\norder: 2\nsource: https:\u002F\u002Fgithub.com\u002FHaythamasalama\u002FdesignPattern\u002Ftree\u002Fmaster\u002Fsrc\u002FStructural\u002FBridge\n---\n\n## The idea\n\nSay you have two kinds of application and two platforms. With inheritance alone you need a class for every pair: `ApplicationAOnWindows`, `ApplicationAOnMac`, and so on. Add a third platform and the number of classes grows again. Two separate ideas, which application and which platform, are tangled in one hierarchy.\n\nThe Bridge pattern splits them into two hierarchies. One is the abstraction, the part the client works with: here, the application. The other is the implementation, the part that does the low-level work: here, the platform. The abstraction holds a reference to an implementation and passes work to it. That reference is the bridge.\n\nNow each side can grow on its own. A new application is one class, a new platform is one class, and any application can run on any platform.\n\n## When to use it\n\n- A class varies in two independent ways, and a subclass for every combination would multiply classes.\n- You want to choose or switch the implementation at runtime.\n- You want to change the implementation without touching the code that uses the abstraction.\n- Your code must run on several platforms, databases or rendering back ends.\n\n## Pros and cons\n\n### Pros\n\n- It separates concerns: high-level logic sits in the abstraction, platform details in the implementation.\n- You can extend the implementation easily: a new platform needs no change to any application.\n- Abstractions and implementations can be combined freely at runtime.\n\n### Cons\n\n- It adds indirection and more types, which isn't worth it for a class that varies in only one way.\n- You need to find the right split early. Splitting along the wrong line makes both sides harder to change.\n\n## The example\n\nThe example runs applications on operating systems. An application knows what it wants to do. A platform knows how to run code on one operating system.\n\n- **`Application`**: the abstraction. An abstract class that holds a `Platform` and declares `develop()`.\n- **`ApplicationA`**: a refined abstraction. Its `develop()` hands the work to the platform with `platform.execute(\"Application A\")`.\n- **`Platform`**: the implementor interface, with one method, `execute()`.\n- **`Windows`** and **`Mac`**: concrete implementors. Each runs the code on its own operating system.\n- **`Demo`**: the client. It pairs `ApplicationA` with `Windows`. Passing `new Mac()` instead is the only change needed to run it on a Mac.\n\n## The code\n\n```java [Platform.java]\ninterface Platform {\n    void execute(String code);\n}\n```\n\n```java [Application.java]\npublic abstract class Application {\n    protected Platform platform;\n\n    public Application(Platform platform) {\n        this.platform = platform;\n    }\n\n    abstract void develop();\n}\n```\n\n```java [Windows.java]\nclass Windows implements Platform {\n    @Override\n    public void execute(String code) {\n        System.out.println(\"Running \" + code + \" on Windows\");\n    }\n}\n```\n\n```java [Mac.java]\nclass Mac implements Platform {\n    @Override\n    public void execute(String code) {\n        System.out.println(\"Running \" + code + \" on MAC\");\n    }\n}\n```\n\n```java [ApplicationA.java]\npublic class ApplicationA extends Application {\n    public ApplicationA(Platform platform) {\n        super(platform);\n    }\n\n    @Override\n    void develop() {\n        platform.execute(\"Application A\");\n    }\n}\n```\n\n```java [Demo.java]\npublic class Demo {\n    public static void main(String[] args) {\n        Application app = new ApplicationA(new Windows());\n        app.develop();\n    }\n}\n```\n\nRunning `Demo` prints:\n\n```text\nRunning Application A on Windows\n```\n",{"title":22,"description":740},{"loc":746},"https:\u002F\u002Fgithub.com\u002FHaythamasalama\u002FdesignPattern\u002Ftree\u002Fmaster\u002Fsrc\u002FStructural\u002FBridge","learn\u002Fdesign-patterns\u002Fbridge","Split a class into two parts that can change on their own: what it does, and how it gets done.","YexgcMlr7-zb6DbEnwTUyIGVpmW7WQqqjUQJRd_yYpw",[755,760,761,766,771,776,781],{"path":756,"title":757,"icon":758,"summary":759,"order":202},"\u002Flearn\u002Fdesign-patterns\u002Fadapter","Adapter","lucide:plug","A thin wrapper that lets a class work with code that expects a different interface.",{"path":746,"title":22,"icon":744,"summary":752,"order":217},{"path":762,"title":763,"icon":764,"summary":765,"order":240},"\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":767,"title":768,"icon":769,"summary":770,"order":287},"\u002Flearn\u002Fdesign-patterns\u002Fdecorator","Decorator","lucide:gift","Wrap an object in another object that adds behaviour, without changing the original class.",{"path":772,"title":773,"icon":774,"summary":775,"order":306},"\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":777,"title":778,"icon":779,"summary":780,"order":326},"\u002Flearn\u002Fdesign-patterns\u002Fflyweight","Flyweight","lucide:feather","Share the common part of many similar objects, so large numbers of them cost little memory.",{"path":782,"title":783,"icon":784,"summary":785,"order":332},"\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":787},"\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":789},"\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":791},"\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":793},"\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":795},"\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":797},"\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":799},"\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":801},"\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":803},"\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":805},"\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>",1793131416551]