[{"data":1,"prerenderedAt":1092},["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\u002Ffacade":20,"pattern-group:\u002Flearn\u002Fdesign-patterns\u002Ffacade":1040,"i-lucide:lightbulb":1072,"i-lucide:boxes":1074,"i-lucide:plug":1076,"i-lucide:bridge":1078,"i-lucide:network":1080,"i-lucide:gift":1082,"i-lucide:door-open":1084,"i-lucide:feather":1086,"i-lucide:shield-check":1088,"i-lucide:copy":1090},{"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":1026,"example":1027,"extension":1028,"group":1029,"icon":1030,"meta":1031,"navigation":263,"order":260,"path":1032,"rawbody":1033,"seo":1034,"sitemap":1035,"source":1036,"stem":1037,"summary":1038,"__hash__":1039},"patterns\u002Flearn\u002Fdesign-patterns\u002Ffacade.md","Facade",{"type":24,"value":25,"toc":1016},"minimark",[26,31,35,43,46,50,66,70,75,86,90,98,102,105,151,155,325,487,545,876,998,1004,1012],[27,28,30],"h2",{"id":29},"the-idea","The idea",[32,33,34],"p",{},"A task often needs several classes to work together in the right order. Buying shares, for example, means taking money from a bank account and then adding the shares to a portfolio. If every caller does this itself, every caller has to know about both classes and the order of the steps.",[32,36,37,38,42],{},"A facade is a class that does this work for them. It offers a few simple methods, such as ",[39,40,41],"code",{},"buyStock",", and inside it calls the subsystem classes in the right order. Callers talk to the facade and don't need to know what is behind it.",[32,44,45],{},"The subsystem classes don't change, and they don't know about the facade. Code that needs more control can still use them directly.",[27,47,49],{"id":48},"when-to-use-it","When to use it",[51,52,53,57,60,63],"ul",{},[54,55,56],"li",{},"A subsystem has many classes, and most callers only need a few common tasks from it.",[54,58,59],{},"You want to give a library or an old module a simpler entry point.",[54,61,62],{},"You want to reduce how many classes your code depends on, so changes inside the subsystem don't spread.",[54,64,65],{},"You are organising a system in layers, and want each layer to have one entry point.",[27,67,69],{"id":68},"pros-and-cons","Pros and cons",[71,72,74],"h3",{"id":73},"pros","Pros",[51,76,77,80,83],{},[54,78,79],{},"It separates concerns: callers deal with tasks, and the subsystem deals with the details.",[54,81,82],{},"It gives a simpler, more convenient interface to a complex system.",[54,84,85],{},"Callers depend on one class instead of many, so the subsystem can change behind it.",[71,87,89],{"id":88},"cons","Cons",[51,91,92,95],{},[54,93,94],{},"A facade can grow into a large class that knows too much and is coupled to every part of the system.",[54,96,97],{},"If callers need options the facade doesn't offer, they go around it, and you end up with two ways of doing the same thing.",[27,99,101],{"id":100},"the-example","The example",[32,103,104],{},"The example is a small financial system. Buying and selling shares touches both the bank account and the stock portfolio, and checking credit is a separate service.",[51,106,107,128,143],{},[54,108,109,115,116,121,122,127],{},[110,111,112],"strong",{},[39,113,114],{},"BankAccount",", ",[110,117,118],{},[39,119,120],{},"StockPortfolio"," and ",[110,123,124],{},[39,125,126],{},"CreditRating",": the subsystem classes. Each does one job and knows nothing of the others.",[54,129,130,135,136,138,139,142],{},[110,131,132],{},[39,133,134],{},"FinancialSystemFacade",": the facade. It creates the three subsystem objects. ",[39,137,41],{}," withdraws the cost from the account, then buys the shares. ",[39,140,141],{},"sellStock"," sells the shares, then deposits the money. The prices are fixed in the code: 100 a share to buy and 110 a share to sell.",[54,144,145,150],{},[110,146,147],{},[39,148,149],{},"Demo",": the client. It only ever uses the facade.",[27,152,154],{"id":153},"the-code","The code",[156,157,163],"pre",{"className":158,"code":159,"filename":160,"language":161,"meta":162,"style":162},"language-java shiki shiki-themes haytham-dark haytham-light","public class BankAccount {\n    public void deposit(double amount) {\n        System.out.println(\"Deposit \" + amount + \" to bank account\");\n    }\n\n    public void withdraw(double amount) {\n        System.out.println(\"Withdraw \" + amount + \" from bank account\");\n    }\n}\n","BankAccount.java","java","",[39,164,165,185,213,252,258,265,285,314,319],{"__ignoreMap":162},[166,167,170,174,177,181],"span",{"class":168,"line":169},"line",1,[166,171,173],{"class":172},"sVsk0","public",[166,175,176],{"class":172}," class",[166,178,180],{"class":179},"sNwy7"," BankAccount",[166,182,184],{"class":183},"sVG6I"," {\n",[166,186,188,191,194,198,201,204,208,211],{"class":168,"line":187},2,[166,189,190],{"class":172},"    public",[166,192,193],{"class":172}," void",[166,195,197],{"class":196},"sVz7n"," deposit",[166,199,200],{"class":183},"(",[166,202,203],{"class":172},"double",[166,205,207],{"class":206},"sWFPS"," amount",[166,209,210],{"class":183},")",[166,212,184],{"class":183},[166,214,216,219,222,226,228,231,233,237,240,243,246,249],{"class":168,"line":215},3,[166,217,218],{"class":206},"        System",[166,220,221],{"class":183},".",[166,223,225],{"class":224},"s4Awn","out",[166,227,221],{"class":183},[166,229,230],{"class":196},"println",[166,232,200],{"class":183},[166,234,236],{"class":235},"sUOzB","\"Deposit \"",[166,238,239],{"class":183}," +",[166,241,242],{"class":206}," amount ",[166,244,245],{"class":183},"+",[166,247,248],{"class":235}," \" to bank account\"",[166,250,251],{"class":183},");\n",[166,253,255],{"class":168,"line":254},4,[166,256,257],{"class":183},"    }\n",[166,259,261],{"class":168,"line":260},5,[166,262,264],{"emptyLinePlaceholder":263},true,"\n",[166,266,268,270,272,275,277,279,281,283],{"class":168,"line":267},6,[166,269,190],{"class":172},[166,271,193],{"class":172},[166,273,274],{"class":196}," withdraw",[166,276,200],{"class":183},[166,278,203],{"class":172},[166,280,207],{"class":206},[166,282,210],{"class":183},[166,284,184],{"class":183},[166,286,288,290,292,294,296,298,300,303,305,307,309,312],{"class":168,"line":287},7,[166,289,218],{"class":206},[166,291,221],{"class":183},[166,293,225],{"class":224},[166,295,221],{"class":183},[166,297,230],{"class":196},[166,299,200],{"class":183},[166,301,302],{"class":235},"\"Withdraw \"",[166,304,239],{"class":183},[166,306,242],{"class":206},[166,308,245],{"class":183},[166,310,311],{"class":235}," \" from bank account\"",[166,313,251],{"class":183},[166,315,317],{"class":168,"line":316},8,[166,318,257],{"class":183},[166,320,322],{"class":168,"line":321},9,[166,323,324],{"class":183},"}\n",[156,326,329],{"className":158,"code":327,"filename":328,"language":161,"meta":162,"style":162},"public class StockPortfolio {\n    public void buy(String stock, int shares) {\n        System.out.println(\"Buy \" + shares + \" shares of \" + stock + \" stock\");\n    }\n\n    public void sell(String stock, int shares) {\n        System.out.println(\"Sell \" + shares + \" shares of \" + stock + \" stock\");\n    }\n}\n","StockPortfolio.java",[39,330,331,342,372,411,415,419,444,479,483],{"__ignoreMap":162},[166,332,333,335,337,340],{"class":168,"line":169},[166,334,173],{"class":172},[166,336,176],{"class":172},[166,338,339],{"class":179}," StockPortfolio",[166,341,184],{"class":183},[166,343,344,346,348,351,353,356,359,362,365,368,370],{"class":168,"line":187},[166,345,190],{"class":172},[166,347,193],{"class":172},[166,349,350],{"class":196}," buy",[166,352,200],{"class":183},[166,354,355],{"class":172},"String",[166,357,358],{"class":206}," stock",[166,360,361],{"class":183},",",[166,363,364],{"class":172}," int",[166,366,367],{"class":206}," shares",[166,369,210],{"class":183},[166,371,184],{"class":183},[166,373,374,376,378,380,382,384,386,389,391,394,396,399,401,404,406,409],{"class":168,"line":215},[166,375,218],{"class":206},[166,377,221],{"class":183},[166,379,225],{"class":224},[166,381,221],{"class":183},[166,383,230],{"class":196},[166,385,200],{"class":183},[166,387,388],{"class":235},"\"Buy \"",[166,390,239],{"class":183},[166,392,393],{"class":206}," shares ",[166,395,245],{"class":183},[166,397,398],{"class":235}," \" shares of \"",[166,400,239],{"class":183},[166,402,403],{"class":206}," stock ",[166,405,245],{"class":183},[166,407,408],{"class":235}," \" stock\"",[166,410,251],{"class":183},[166,412,413],{"class":168,"line":254},[166,414,257],{"class":183},[166,416,417],{"class":168,"line":260},[166,418,264],{"emptyLinePlaceholder":263},[166,420,421,423,425,428,430,432,434,436,438,440,442],{"class":168,"line":267},[166,422,190],{"class":172},[166,424,193],{"class":172},[166,426,427],{"class":196}," sell",[166,429,200],{"class":183},[166,431,355],{"class":172},[166,433,358],{"class":206},[166,435,361],{"class":183},[166,437,364],{"class":172},[166,439,367],{"class":206},[166,441,210],{"class":183},[166,443,184],{"class":183},[166,445,446,448,450,452,454,456,458,461,463,465,467,469,471,473,475,477],{"class":168,"line":287},[166,447,218],{"class":206},[166,449,221],{"class":183},[166,451,225],{"class":224},[166,453,221],{"class":183},[166,455,230],{"class":196},[166,457,200],{"class":183},[166,459,460],{"class":235},"\"Sell \"",[166,462,239],{"class":183},[166,464,393],{"class":206},[166,466,245],{"class":183},[166,468,398],{"class":235},[166,470,239],{"class":183},[166,472,403],{"class":206},[166,474,245],{"class":183},[166,476,408],{"class":235},[166,478,251],{"class":183},[166,480,481],{"class":168,"line":316},[166,482,257],{"class":183},[166,484,485],{"class":168,"line":321},[166,486,324],{"class":183},[156,488,491],{"className":158,"code":489,"filename":490,"language":161,"meta":162,"style":162},"public class CreditRating {\n    public void check() {\n        System.out.println(\"check credit rating\");\n    }\n}\n","CreditRating.java",[39,492,493,504,518,537,541],{"__ignoreMap":162},[166,494,495,497,499,502],{"class":168,"line":169},[166,496,173],{"class":172},[166,498,176],{"class":172},[166,500,501],{"class":179}," CreditRating",[166,503,184],{"class":183},[166,505,506,508,510,513,516],{"class":168,"line":187},[166,507,190],{"class":172},[166,509,193],{"class":172},[166,511,512],{"class":196}," check",[166,514,515],{"class":183},"()",[166,517,184],{"class":183},[166,519,520,522,524,526,528,530,532,535],{"class":168,"line":215},[166,521,218],{"class":206},[166,523,221],{"class":183},[166,525,225],{"class":224},[166,527,221],{"class":183},[166,529,230],{"class":196},[166,531,200],{"class":183},[166,533,534],{"class":235},"\"check credit rating\"",[166,536,251],{"class":183},[166,538,539],{"class":168,"line":254},[166,540,257],{"class":183},[166,542,543],{"class":168,"line":260},[166,544,324],{"class":183},[156,546,549],{"className":158,"code":547,"filename":548,"language":161,"meta":162,"style":162},"public class FinancialSystemFacade {\n    private final BankAccount bankAccount;\n    private final StockPortfolio stockPortfolio;\n    private final CreditRating creditRating;\n\n    public FinancialSystemFacade() {\n        bankAccount = new BankAccount();\n        stockPortfolio = new StockPortfolio();\n        creditRating = new CreditRating();\n    }\n\n    public void checkCredit() {\n        creditRating.check();\n    }\n\n    public void buyStock(String stock, int shares) {\n        bankAccount.withdraw(shares * 100);\n\n        stockPortfolio.buy(stock, shares);\n    }\n\n    public void sellStock(String stock, int shares) {\n        stockPortfolio.sell(stock, shares);\n\n        bankAccount.deposit(shares * 110);\n    }\n}\n","FinancialSystemFacade.java",[39,550,551,562,578,591,604,608,618,634,647,660,665,670,684,697,702,707,733,758,763,785,790,795,821,841,845,866,871],{"__ignoreMap":162},[166,552,553,555,557,560],{"class":168,"line":169},[166,554,173],{"class":172},[166,556,176],{"class":172},[166,558,559],{"class":179}," FinancialSystemFacade",[166,561,184],{"class":183},[166,563,564,567,570,572,575],{"class":168,"line":187},[166,565,566],{"class":172},"    private",[166,568,569],{"class":172}," final",[166,571,180],{"class":172},[166,573,574],{"class":206}," bankAccount",[166,576,577],{"class":183},";\n",[166,579,580,582,584,586,589],{"class":168,"line":215},[166,581,566],{"class":172},[166,583,569],{"class":172},[166,585,339],{"class":172},[166,587,588],{"class":206}," stockPortfolio",[166,590,577],{"class":183},[166,592,593,595,597,599,602],{"class":168,"line":254},[166,594,566],{"class":172},[166,596,569],{"class":172},[166,598,501],{"class":172},[166,600,601],{"class":206}," creditRating",[166,603,577],{"class":183},[166,605,606],{"class":168,"line":260},[166,607,264],{"emptyLinePlaceholder":263},[166,609,610,612,614,616],{"class":168,"line":267},[166,611,190],{"class":172},[166,613,559],{"class":196},[166,615,515],{"class":183},[166,617,184],{"class":183},[166,619,620,623,626,629,631],{"class":168,"line":287},[166,621,622],{"class":206},"        bankAccount ",[166,624,625],{"class":183},"=",[166,627,628],{"class":172}," new",[166,630,180],{"class":196},[166,632,633],{"class":183},"();\n",[166,635,636,639,641,643,645],{"class":168,"line":316},[166,637,638],{"class":206},"        stockPortfolio ",[166,640,625],{"class":183},[166,642,628],{"class":172},[166,644,339],{"class":196},[166,646,633],{"class":183},[166,648,649,652,654,656,658],{"class":168,"line":321},[166,650,651],{"class":206},"        creditRating ",[166,653,625],{"class":183},[166,655,628],{"class":172},[166,657,501],{"class":196},[166,659,633],{"class":183},[166,661,663],{"class":168,"line":662},10,[166,664,257],{"class":183},[166,666,668],{"class":168,"line":667},11,[166,669,264],{"emptyLinePlaceholder":263},[166,671,673,675,677,680,682],{"class":168,"line":672},12,[166,674,190],{"class":172},[166,676,193],{"class":172},[166,678,679],{"class":196}," checkCredit",[166,681,515],{"class":183},[166,683,184],{"class":183},[166,685,687,690,692,695],{"class":168,"line":686},13,[166,688,689],{"class":206},"        creditRating",[166,691,221],{"class":183},[166,693,694],{"class":196},"check",[166,696,633],{"class":183},[166,698,700],{"class":168,"line":699},14,[166,701,257],{"class":183},[166,703,705],{"class":168,"line":704},15,[166,706,264],{"emptyLinePlaceholder":263},[166,708,710,712,714,717,719,721,723,725,727,729,731],{"class":168,"line":709},16,[166,711,190],{"class":172},[166,713,193],{"class":172},[166,715,716],{"class":196}," buyStock",[166,718,200],{"class":183},[166,720,355],{"class":172},[166,722,358],{"class":206},[166,724,361],{"class":183},[166,726,364],{"class":172},[166,728,367],{"class":206},[166,730,210],{"class":183},[166,732,184],{"class":183},[166,734,736,739,741,744,746,749,752,756],{"class":168,"line":735},17,[166,737,738],{"class":206},"        bankAccount",[166,740,221],{"class":183},[166,742,743],{"class":196},"withdraw",[166,745,200],{"class":183},[166,747,748],{"class":206},"shares ",[166,750,751],{"class":183},"*",[166,753,755],{"class":754},"sheII"," 100",[166,757,251],{"class":183},[166,759,761],{"class":168,"line":760},18,[166,762,264],{"emptyLinePlaceholder":263},[166,764,766,769,771,774,776,779,781,783],{"class":168,"line":765},19,[166,767,768],{"class":206},"        stockPortfolio",[166,770,221],{"class":183},[166,772,773],{"class":196},"buy",[166,775,200],{"class":183},[166,777,778],{"class":206},"stock",[166,780,361],{"class":183},[166,782,367],{"class":206},[166,784,251],{"class":183},[166,786,788],{"class":168,"line":787},20,[166,789,257],{"class":183},[166,791,793],{"class":168,"line":792},21,[166,794,264],{"emptyLinePlaceholder":263},[166,796,798,800,802,805,807,809,811,813,815,817,819],{"class":168,"line":797},22,[166,799,190],{"class":172},[166,801,193],{"class":172},[166,803,804],{"class":196}," sellStock",[166,806,200],{"class":183},[166,808,355],{"class":172},[166,810,358],{"class":206},[166,812,361],{"class":183},[166,814,364],{"class":172},[166,816,367],{"class":206},[166,818,210],{"class":183},[166,820,184],{"class":183},[166,822,824,826,828,831,833,835,837,839],{"class":168,"line":823},23,[166,825,768],{"class":206},[166,827,221],{"class":183},[166,829,830],{"class":196},"sell",[166,832,200],{"class":183},[166,834,778],{"class":206},[166,836,361],{"class":183},[166,838,367],{"class":206},[166,840,251],{"class":183},[166,842,843],{"class":168,"line":5},[166,844,264],{"emptyLinePlaceholder":263},[166,846,848,850,852,855,857,859,861,864],{"class":168,"line":847},25,[166,849,738],{"class":206},[166,851,221],{"class":183},[166,853,854],{"class":196},"deposit",[166,856,200],{"class":183},[166,858,748],{"class":206},[166,860,751],{"class":183},[166,862,863],{"class":754}," 110",[166,865,251],{"class":183},[166,867,869],{"class":168,"line":868},26,[166,870,257],{"class":183},[166,872,874],{"class":168,"line":873},27,[166,875,324],{"class":183},[156,877,880],{"className":158,"code":878,"filename":879,"language":161,"meta":162,"style":162},"public class Demo {\n    public static void main(String[] args) {\n        FinancialSystemFacade facade = new FinancialSystemFacade();\n\n        facade.checkCredit();\n        facade.buyStock(\"AAPL\", 10);\n        facade.sellStock(\"AAPL\", 5);\n    }\n}\n","Demo.java",[39,881,882,893,919,935,939,951,971,990,994],{"__ignoreMap":162},[166,883,884,886,888,891],{"class":168,"line":169},[166,885,173],{"class":172},[166,887,176],{"class":172},[166,889,890],{"class":179}," Demo",[166,892,184],{"class":183},[166,894,895,897,900,902,905,907,909,912,915,917],{"class":168,"line":187},[166,896,190],{"class":172},[166,898,899],{"class":172}," static",[166,901,193],{"class":172},[166,903,904],{"class":196}," main",[166,906,200],{"class":183},[166,908,355],{"class":172},[166,910,911],{"class":183},"[]",[166,913,914],{"class":206}," args",[166,916,210],{"class":183},[166,918,184],{"class":183},[166,920,921,924,927,929,931,933],{"class":168,"line":215},[166,922,923],{"class":172},"        FinancialSystemFacade",[166,925,926],{"class":206}," facade ",[166,928,625],{"class":183},[166,930,628],{"class":172},[166,932,559],{"class":196},[166,934,633],{"class":183},[166,936,937],{"class":168,"line":254},[166,938,264],{"emptyLinePlaceholder":263},[166,940,941,944,946,949],{"class":168,"line":260},[166,942,943],{"class":206},"        facade",[166,945,221],{"class":183},[166,947,948],{"class":196},"checkCredit",[166,950,633],{"class":183},[166,952,953,955,957,959,961,964,966,969],{"class":168,"line":267},[166,954,943],{"class":206},[166,956,221],{"class":183},[166,958,41],{"class":196},[166,960,200],{"class":183},[166,962,963],{"class":235},"\"AAPL\"",[166,965,361],{"class":183},[166,967,968],{"class":754}," 10",[166,970,251],{"class":183},[166,972,973,975,977,979,981,983,985,988],{"class":168,"line":287},[166,974,943],{"class":206},[166,976,221],{"class":183},[166,978,141],{"class":196},[166,980,200],{"class":183},[166,982,963],{"class":235},[166,984,361],{"class":183},[166,986,987],{"class":754}," 5",[166,989,251],{"class":183},[166,991,992],{"class":168,"line":316},[166,993,257],{"class":183},[166,995,996],{"class":168,"line":321},[166,997,324],{"class":183},[32,999,1000,1001,1003],{},"Running ",[39,1002,149],{}," prints:",[156,1005,1010],{"className":1006,"code":1008,"language":1009,"meta":162},[1007],"language-text","check credit rating\nWithdraw 1000.0 from bank account\nBuy 10 shares of AAPL stock\nSell 5 shares of AAPL stock\nDeposit 550.0 to bank account\n","text",[39,1011,1008],{"__ignoreMap":162},[1013,1014,1015],"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 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 .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}",{"title":162,"searchDepth":187,"depth":187,"links":1017},[1018,1019,1020,1024,1025],{"id":29,"depth":187,"text":30},{"id":48,"depth":187,"text":49},{"id":68,"depth":187,"text":69,"children":1021},[1022,1023],{"id":73,"depth":215,"text":74},{"id":88,"depth":215,"text":89},{"id":100,"depth":187,"text":101},{"id":153,"depth":187,"text":154},"The Facade pattern puts one simple class in front of a complex subsystem, so callers make one call instead of many and need not know how the parts fit. Explained with a worked Java example of trading shares.","Buying and selling shares through one class","md","Structural","lucide:door-open",{},"\u002Flearn\u002Fdesign-patterns\u002Ffacade","---\ntitle: Facade\ngroup: Structural\nicon: lucide:door-open\nsummary: 'One simple class in front of a complex system, so callers talk to it instead of to every part.'\ndescription: 'The Facade pattern puts one simple class in front of a complex subsystem, so callers make one call instead of many and need not know how the parts fit. Explained with a worked Java example of trading shares.'\nexample: 'Buying and selling shares through one class'\norder: 5\nsource: https:\u002F\u002Fgithub.com\u002FHaythamasalama\u002FdesignPattern\u002Ftree\u002Fmaster\u002Fsrc\u002FStructural\u002FFacade\n---\n\n## The idea\n\nA task often needs several classes to work together in the right order. Buying shares, for example, means taking money from a bank account and then adding the shares to a portfolio. If every caller does this itself, every caller has to know about both classes and the order of the steps.\n\nA facade is a class that does this work for them. It offers a few simple methods, such as `buyStock`, and inside it calls the subsystem classes in the right order. Callers talk to the facade and don't need to know what is behind it.\n\nThe subsystem classes don't change, and they don't know about the facade. Code that needs more control can still use them directly.\n\n## When to use it\n\n- A subsystem has many classes, and most callers only need a few common tasks from it.\n- You want to give a library or an old module a simpler entry point.\n- You want to reduce how many classes your code depends on, so changes inside the subsystem don't spread.\n- You are organising a system in layers, and want each layer to have one entry point.\n\n## Pros and cons\n\n### Pros\n\n- It separates concerns: callers deal with tasks, and the subsystem deals with the details.\n- It gives a simpler, more convenient interface to a complex system.\n- Callers depend on one class instead of many, so the subsystem can change behind it.\n\n### Cons\n\n- A facade can grow into a large class that knows too much and is coupled to every part of the system.\n- If callers need options the facade doesn't offer, they go around it, and you end up with two ways of doing the same thing.\n\n## The example\n\nThe example is a small financial system. Buying and selling shares touches both the bank account and the stock portfolio, and checking credit is a separate service.\n\n- **`BankAccount`**, **`StockPortfolio`** and **`CreditRating`**: the subsystem classes. Each does one job and knows nothing of the others.\n- **`FinancialSystemFacade`**: the facade. It creates the three subsystem objects. `buyStock` withdraws the cost from the account, then buys the shares. `sellStock` sells the shares, then deposits the money. The prices are fixed in the code: 100 a share to buy and 110 a share to sell.\n- **`Demo`**: the client. It only ever uses the facade.\n\n## The code\n\n```java [BankAccount.java]\npublic class BankAccount {\n    public void deposit(double amount) {\n        System.out.println(\"Deposit \" + amount + \" to bank account\");\n    }\n\n    public void withdraw(double amount) {\n        System.out.println(\"Withdraw \" + amount + \" from bank account\");\n    }\n}\n```\n\n```java [StockPortfolio.java]\npublic class StockPortfolio {\n    public void buy(String stock, int shares) {\n        System.out.println(\"Buy \" + shares + \" shares of \" + stock + \" stock\");\n    }\n\n    public void sell(String stock, int shares) {\n        System.out.println(\"Sell \" + shares + \" shares of \" + stock + \" stock\");\n    }\n}\n```\n\n```java [CreditRating.java]\npublic class CreditRating {\n    public void check() {\n        System.out.println(\"check credit rating\");\n    }\n}\n```\n\n```java [FinancialSystemFacade.java]\npublic class FinancialSystemFacade {\n    private final BankAccount bankAccount;\n    private final StockPortfolio stockPortfolio;\n    private final CreditRating creditRating;\n\n    public FinancialSystemFacade() {\n        bankAccount = new BankAccount();\n        stockPortfolio = new StockPortfolio();\n        creditRating = new CreditRating();\n    }\n\n    public void checkCredit() {\n        creditRating.check();\n    }\n\n    public void buyStock(String stock, int shares) {\n        bankAccount.withdraw(shares * 100);\n\n        stockPortfolio.buy(stock, shares);\n    }\n\n    public void sellStock(String stock, int shares) {\n        stockPortfolio.sell(stock, shares);\n\n        bankAccount.deposit(shares * 110);\n    }\n}\n```\n\n```java [Demo.java]\npublic class Demo {\n    public static void main(String[] args) {\n        FinancialSystemFacade facade = new FinancialSystemFacade();\n\n        facade.checkCredit();\n        facade.buyStock(\"AAPL\", 10);\n        facade.sellStock(\"AAPL\", 5);\n    }\n}\n```\n\nRunning `Demo` prints:\n\n```text\ncheck credit rating\nWithdraw 1000.0 from bank account\nBuy 10 shares of AAPL stock\nSell 5 shares of AAPL stock\nDeposit 550.0 to bank account\n```\n",{"title":22,"description":1026},{"loc":1032},"https:\u002F\u002Fgithub.com\u002FHaythamasalama\u002FdesignPattern\u002Ftree\u002Fmaster\u002Fsrc\u002FStructural\u002FFacade","learn\u002Fdesign-patterns\u002Ffacade","One simple class in front of a complex system, so callers talk to it instead of to every part.","Wh6Rx-Ak4MFNayR-wP0PlOjxSQmQwSJLdsIlcmFWjEw",[1041,1046,1051,1056,1061,1062,1067],{"path":1042,"title":1043,"icon":1044,"summary":1045,"order":169},"\u002Flearn\u002Fdesign-patterns\u002Fadapter","Adapter","lucide:plug","A thin wrapper that lets a class work with code that expects a different interface.",{"path":1047,"title":1048,"icon":1049,"summary":1050,"order":187},"\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":1052,"title":1053,"icon":1054,"summary":1055,"order":215},"\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":1057,"title":1058,"icon":1059,"summary":1060,"order":254},"\u002Flearn\u002Fdesign-patterns\u002Fdecorator","Decorator","lucide:gift","Wrap an object in another object that adds behaviour, without changing the original class.",{"path":1032,"title":22,"icon":1030,"summary":1038,"order":260},{"path":1063,"title":1064,"icon":1065,"summary":1066,"order":267},"\u002Flearn\u002Fdesign-patterns\u002Fflyweight","Flyweight","lucide:feather","Share the common part of many similar objects, so large numbers of them cost little memory.",{"path":1068,"title":1069,"icon":1070,"summary":1071,"order":287},"\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":1073},"\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":1075},"\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":1077},"\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":1079},"\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":1081},"\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":1083},"\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":1085},"\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":1087},"\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":1089},"\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":1091},"\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>",1793131416628]