🎉 Hooray! After 3 years of work, I've finally released the ebook on design patterns! Check it out »

C#: Facade

Facade

Facade is a structural design pattern that provides a simplified (but limited) interface to a complex system of classes, library or framework.

While Facade decreases the overall complexity of the application, it also helps to move unwanted dependencies to one place.

Learn more about Facade

Application of the pattern in C#

Complexity:

Popularity:

Usage examples: The Facade pattern is commonly used in apps written in C#. It’s especially handy when working with complex libraries and APIs.

Identification: Facade can be recognized in a class that has a simple interface, but delegates most of the work to other classes. Usually, facades manage full life cycle of objects they use.

Example: Structure of the Pattern

This example illustrates the structure of the Facade design pattern. It focuses on answering these questions:

  • What classes does it consists of?
  • What roles do these classes play?
  • In what way the elements of the pattern are related?

Program.cs: Structural Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RefactoringGuru.DesignPatterns.Facade.Structural
{
    class Program
    {
        static void Main(string[] args)
        {
            Client client = new Client();

            Subsystem1 s1 = new Subsystem1();
            Subsystem2 s2 = new Subsystem2();
            Facade facade = new Facade(s1, s2);
            client.ClientCode(facade);
        }
    }

    class Client
    {
        internal void ClientCode(Facade facade)
        {
            Console.WriteLine(facade.Operation());
        }
    }

    public class Subsystem1
    {
        public string operation1()
        {
            return "Subsystem1: Ready!\n";
        }

        public string operationN()
        {
            return "Subsystem1: Go!\n";
        }
    }
	
    public class Subsystem2
    {
        public string operation1()
        {
            return "Subsystem2: Get ready!\n";
        }

        public string operationZ()
        {
            return "Subsystem2: Fire!\n";
        }
    }

    public class Facade
    {
        Subsystem1 Subsystem1;
		
        Subsystem2 Subsystem2;

        public Facade(Subsystem1 s1, Subsystem2 s2)
        {
            this.Subsystem1 = s1;
            this.Subsystem2 = s2;
        }
		
        public string Operation()
        {
            string result = "Facade initializes subsystems:\n";
            result += Subsystem1.operation1();
            result += Subsystem2.operation1();
            result += "Facade orders subsystems to perform the action:\n";
            result += Subsystem1.operationN();
            result += Subsystem2.operationZ();
            return result;
        }
    }
}

Output.txt: Output

Facade initializes subsystems:
Subsystem1: Ready!
Subsystem2: Get ready!
Facade orders subsystems to perform the action:
Subsystem1: Go!
Subsystem2: Fire!