{"id":1654,"date":"2023-04-22T19:05:30","date_gmt":"2023-04-22T12:05:30","guid":{"rendered":"https:\/\/csharptutorial.net\/?page_id=1654"},"modified":"2023-04-22T19:05:31","modified_gmt":"2023-04-22T12:05:31","slug":"csharp-facade-pattern","status":"publish","type":"page","link":"https:\/\/www.csharptutorial.net\/csharp-design-patterns\/csharp-facade-pattern\/","title":{"rendered":"C# Facade Pattern"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the C# Facade pattern to make a simple and clean interface to a larger and more complex subsystem.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the C# Facade pattern<\/h2>\n\n\n\n<p>The Facade pattern is a structural design pattern that allows you to create a simplified <a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-interface\/\">interface<\/a> to a complex subsystem.<\/p>\n\n\n\n<p>The Facade pattern exposes only the necessary functionality of the underlying subsystem while hiding the implementation details and complexities of the subsystem.<\/p>\n\n\n\n<p>The following diagram illustrates the Facade pattern:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/csharptutorial.net\/wp-content\/uploads\/2023\/04\/CSharp-Facade-Design-Pattern.svg\" alt=\"\" class=\"wp-image-1655\"\/><\/figure>\n\n\n\n<p>In this diagram, the client classes interact with a simple interface (Facade) instead of working directly with subsystem classes. This makes the system more flexible so that when the subsystem classes change, you don&#8217;t need to modify the client classes.<\/p>\n\n\n\n<p>To use the Facade pattern in practice, you create a new class or set of classes that serve as an interface to the existing subsystem. <\/p>\n\n\n\n<p>This new interface provides a simplified set of methods that the client classes use to access the functionality of the subsystem. The client classes don&#8217;t need to know the implementation details or interact directly with the subsystem&#8217;s classes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">C# Facade pattern example<\/h2>\n\n\n\n<p>The following program returns the current temperature in Celsius based on a city. It uses three classes <code>GeoLookupService<\/code>, <code>WeatherService<\/code>, and <code>ConverterService<\/code> that provide geolocation lookup, weather, and converter services:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title\">FacacdePattern<\/span>;\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Location<\/span>\n{\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">double<\/span> Latitude\n    {\n        <span class=\"hljs-keyword\">get<\/span>; <span class=\"hljs-keyword\">set<\/span>;\n    }\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">double<\/span> Longitude\n    {\n        <span class=\"hljs-keyword\">get<\/span>; <span class=\"hljs-keyword\">set<\/span>;\n    }\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">Location<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">double<\/span> latitude, <span class=\"hljs-keyword\">double<\/span> longitude<\/span>)<\/span>\n    {\n\n        Latitude = latitude;\n        Longitude = longitude;\n    }\n}\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">GeoLookupService<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Location <span class=\"hljs-title\">GetLocationByCity<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> city<\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> Location(<span class=\"hljs-number\">15<\/span>, <span class=\"hljs-number\">20<\/span>);\n    }\n}\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">WeatherService<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">double<\/span> <span class=\"hljs-title\">GetCurrentTemperature<\/span>(<span class=\"hljs-params\">Location location<\/span>)<\/span> =&gt; <span class=\"hljs-keyword\">new<\/span> Random().Next(<span class=\"hljs-number\">32<\/span>, <span class=\"hljs-number\">115<\/span>);\n}\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ConverterService<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">double<\/span> <span class=\"hljs-title\">ConvertFToC<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">double<\/span> f<\/span>)<\/span> =&gt; (f - <span class=\"hljs-number\">32<\/span>) * <span class=\"hljs-number\">5<\/span> \/ <span class=\"hljs-number\">9<\/span>;\n}\n\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Program<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Main<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] args<\/span>)<\/span>\n    {\n\n        <span class=\"hljs-keyword\">var<\/span> geoLookupService = <span class=\"hljs-keyword\">new<\/span> GeoLookupService();\n        <span class=\"hljs-keyword\">var<\/span> location = geoLookupService.GetLocationByCity(<span class=\"hljs-string\">\"San Jose\"<\/span>);\n\n        <span class=\"hljs-keyword\">var<\/span> weatherService = <span class=\"hljs-keyword\">new<\/span> WeatherService();\n        <span class=\"hljs-keyword\">var<\/span> temperature = weatherService.GetCurrentTemperature(location); \n\n        <span class=\"hljs-keyword\">var<\/span> converterService = <span class=\"hljs-keyword\">new<\/span> ConverterService();\n        <span class=\"hljs-keyword\">var<\/span> temperatureInC = converterService.ConvertFToC(temperature);\n\n        Console.WriteLine(<span class=\"hljs-string\">$\"The current temperature is <span class=\"hljs-subst\">{temperatureInC:F1}<\/span>\u00b0C\"<\/span>);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">The current temperature <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-number\">30.0<\/span>\u00b0C<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, define the <code>Location<\/code> class that represents a geographical location with a latitude and longitude:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Location<\/span>\n{\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">double<\/span> Latitude\n    {\n        <span class=\"hljs-keyword\">get<\/span>; <span class=\"hljs-keyword\">set<\/span>;\n    }\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">double<\/span> Longitude\n    {\n        <span class=\"hljs-keyword\">get<\/span>; <span class=\"hljs-keyword\">set<\/span>;\n    }\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">Location<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">double<\/span> latitude, <span class=\"hljs-keyword\">double<\/span> longitude<\/span>)<\/span>\n    {\n\n        Latitude = latitude;\n        Longitude = longitude;\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, define the <code>GeoLookupService<\/code> class with a method <code>GetLocationByCity()<\/code> that returns a Location object for a given city:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">GeoLookupService<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Location <span class=\"hljs-title\">GetLocationByCity<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> city<\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> Location(<span class=\"hljs-number\">15<\/span>, <span class=\"hljs-number\">20<\/span>);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, define the <code>WeatherService<\/code> class that returns the current temperature in Fahrenheit of a location:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">WeatherService<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">double<\/span> <span class=\"hljs-title\">GetCurrentTemperature<\/span>(<span class=\"hljs-params\">Location location<\/span>)<\/span> =&gt; <span class=\"hljs-keyword\">new<\/span> Random().Next(<span class=\"hljs-number\">32<\/span>, <span class=\"hljs-number\">115<\/span>);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>GetCurrentTemperature()<\/code> method returns a random temperature in Fahrenheit between 32 and 115. In practice, you may call an API to get the current temperature based on the current location of the user.<\/p>\n\n\n\n<p>Fourth, define the <code>ConverterService<\/code> class that converts Fahrenheit to Celsius:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ConverterService<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">double<\/span> <span class=\"hljs-title\">ConvertFToC<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">double<\/span> f<\/span>)<\/span> =&gt; (f - <span class=\"hljs-number\">32<\/span>) * <span class=\"hljs-number\">5<\/span> \/ <span class=\"hljs-number\">9<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, use the above classes in the Client class to display the current temperature in Celsius to the console by using the above service classes:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Client<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Main<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] args<\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">var<\/span> geoLookupService = <span class=\"hljs-keyword\">new<\/span> GeoLookupService();\n        <span class=\"hljs-keyword\">var<\/span> location = geoLookupService.GetLocationByCity(<span class=\"hljs-string\">\"San Jose\"<\/span>);\n\n        <span class=\"hljs-keyword\">var<\/span> weatherService = <span class=\"hljs-keyword\">new<\/span> WeatherService();\n        <span class=\"hljs-keyword\">var<\/span> temperature = weatherService.GetCurrentTemperature(location); \n\n        <span class=\"hljs-keyword\">var<\/span> converterService = <span class=\"hljs-keyword\">new<\/span> ConverterService();\n        <span class=\"hljs-keyword\">var<\/span> temperatureInC = converterService.ConvertFToC(temperature);\n\n        Console.WriteLine(<span class=\"hljs-string\">$\"The current temperature is <span class=\"hljs-subst\">{temperatureInC:F1}<\/span>\u00b0C\"<\/span>);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The program works fine except that it depends on many services of the subsystem. If one of the services changes, you need to modify the Client class accordingly. To make the Client class more flexible, you can apply the Facade pattern.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using the Facade pattern<\/h3>\n\n\n\n<p>First, define an interface <code>IWeatherServiceFacade<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">interface<\/span> <span class=\"hljs-title\">IWeatherServiceFacade<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">double<\/span> <span class=\"hljs-title\">GetCurrentTemperatureByCity<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> city<\/span>)<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>IWeatherServiceFacade<\/code> interface has one method <code>GetCurrentTemperatureByCity()<\/code> that returns the current temperature in Celsius of a city.<\/p>\n\n\n\n<p>Second, define the <code>WeatherServiceFacade<\/code> class that implements the I<code>WeatherServiceFacade<\/code> interface:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">WeatherServiceFacade<\/span> : <span class=\"hljs-title\">IWeatherServiceFacade<\/span>\n{\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">readonly<\/span> GeoLookupService _geoLookupService;\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">readonly<\/span> WeatherService _weatherService;\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">readonly<\/span> ConverterService _converterService;\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">WeatherServiceFacade<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        _geoLookupService = <span class=\"hljs-keyword\">new<\/span> GeoLookupService();\n        _weatherService = <span class=\"hljs-keyword\">new<\/span> WeatherService();\n        _converterService = <span class=\"hljs-keyword\">new<\/span> ConverterService();\n    }\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">double<\/span> <span class=\"hljs-title\">GetCurrentTemperatureByCity<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> city<\/span>)<\/span>\n    {\n\n        <span class=\"hljs-keyword\">var<\/span> location = _geoLookupService.GetLocationByCity(city);\n        <span class=\"hljs-keyword\">var<\/span> temperature = _weatherService.GetCurrentTemperature(location);\n        <span class=\"hljs-keyword\">return<\/span> _converterService.ConvertFToC(temperature);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>GetCurrentTemperatureByCity()<\/code> method of the <code><code>WeatherService<\/code>Facade<\/code> first uses the <code>GeoLookupService<\/code> object to get the location of a city. Then, it uses the <code>WeatherService<\/code> object to get the current temperature in Fahrenheit by location. And finally, it uses the <code>UnitConvertService<\/code> to convert the temperature from Fahrenheit to Celsius.<\/p>\n\n\n\n<p>Now, the <code>Client<\/code> class needs to use the <code>IWeatherServiceFacade<\/code> object to get the current temperature of a city by calling the <code>GetCurrentTemperatureByCity()<\/code> method. It doesn&#8217;t need to know the underlying services:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Client<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Main<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] args<\/span>)<\/span>\n    {\n\n        <span class=\"hljs-keyword\">var<\/span> weatherServiceFacade = <span class=\"hljs-keyword\">new<\/span> WeatherServiceFacade();\n        <span class=\"hljs-keyword\">var<\/span> temperatureInC = weatherServiceFacade.GetCurrentTemperatureByCity(<span class=\"hljs-string\">\"San Jose\"<\/span>);\n\n        Console.WriteLine(<span class=\"hljs-string\">$\"The current temperature is <span class=\"hljs-subst\">{temperatureInC:F1}<\/span>\u00b0C\"<\/span>);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Putting it all together.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">namespace<\/span> <span class=\"hljs-title\">FacacdePattern<\/span>;\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Location<\/span>\n{\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">double<\/span> Latitude\n    {\n        <span class=\"hljs-keyword\">get<\/span>; <span class=\"hljs-keyword\">set<\/span>;\n    }\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">double<\/span> Longitude\n    {\n        <span class=\"hljs-keyword\">get<\/span>; <span class=\"hljs-keyword\">set<\/span>;\n    }\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">Location<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">double<\/span> latitude, <span class=\"hljs-keyword\">double<\/span> longitude<\/span>)<\/span>\n    {\n\n        Latitude = latitude;\n        Longitude = longitude;\n    }\n}\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">GeoLookupService<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> Location <span class=\"hljs-title\">GetLocationByCity<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> city<\/span>)<\/span>\n    {\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> Location(<span class=\"hljs-number\">15<\/span>, <span class=\"hljs-number\">20<\/span>);\n    }\n}\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">WeatherService<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">double<\/span> <span class=\"hljs-title\">GetCurrentTemperature<\/span>(<span class=\"hljs-params\">Location location<\/span>)<\/span> =&gt; <span class=\"hljs-keyword\">new<\/span> Random().Next(<span class=\"hljs-number\">32<\/span>, <span class=\"hljs-number\">115<\/span>);\n}\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ConverterService<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">double<\/span> <span class=\"hljs-title\">ConvertFToC<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">double<\/span> f<\/span>)<\/span> =&gt; (f - <span class=\"hljs-number\">32<\/span>) * <span class=\"hljs-number\">5<\/span> \/ <span class=\"hljs-number\">9<\/span>;\n}\n\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">interface<\/span> <span class=\"hljs-title\">IWeatherServiceFacade<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">double<\/span> <span class=\"hljs-title\">GetCurrentTemperatureByCity<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> city<\/span>)<\/span>;\n}\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">WeatherServiceFacade<\/span> : <span class=\"hljs-title\">IWeatherServiceFacade<\/span>\n{\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">readonly<\/span> GeoLookupService _geoLookupService;\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">readonly<\/span> WeatherService _weatherService;\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">readonly<\/span> ConverterService _converterService;\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">WeatherServiceFacade<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>\n    {\n        _geoLookupService = <span class=\"hljs-keyword\">new<\/span> GeoLookupService();\n        _weatherService = <span class=\"hljs-keyword\">new<\/span> WeatherService();\n        _converterService = <span class=\"hljs-keyword\">new<\/span> ConverterService();\n    }\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">double<\/span> <span class=\"hljs-title\">GetCurrentTemperatureByCity<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> city<\/span>)<\/span>\n    {\n\n        <span class=\"hljs-keyword\">var<\/span> location = _geoLookupService.GetLocationByCity(city);\n        <span class=\"hljs-keyword\">var<\/span> temperature = _weatherService.GetCurrentTemperature(location);\n        <span class=\"hljs-keyword\">return<\/span> _converterService.ConvertFToC(temperature);\n    }\n}\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Client<\/span>\n{\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Main<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span>&#91;] args<\/span>)<\/span>\n    {\n\n        <span class=\"hljs-keyword\">var<\/span> weatherServiceFacade = <span class=\"hljs-keyword\">new<\/span> WeatherServiceFacade();\n        <span class=\"hljs-keyword\">var<\/span> temperatureInC = weatherServiceFacade.GetCurrentTemperatureByCity(<span class=\"hljs-string\">\"San Jose\"<\/span>);\n\n        Console.WriteLine(<span class=\"hljs-string\">$\"The current temperature is <span class=\"hljs-subst\">{temperatureInC:F1}<\/span>\u00b0C\"<\/span>);\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the Facade pattern to create a simple and clean interface to complex subsystems to make your applications more maintainable and flexible.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1654\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-design-patterns\/csharp-facade-pattern\/\"\n\t\t\t\tdata-post-title=\"C# Facade Pattern\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1654\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-design-patterns\/csharp-facade-pattern\/\"\n\t\t\t\tdata-post-title=\"C# Facade Pattern\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the C# Facade pattern to make a simple and clean interface to a larger and more complex subsystem.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1441,"menu_order":17,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1654","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1654","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/comments?post=1654"}],"version-history":[{"count":1,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1654\/revisions"}],"predecessor-version":[{"id":1656,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1654\/revisions\/1656"}],"up":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/1441"}],"wp:attachment":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/media?parent=1654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}