Coding Schools


 
Python | C Sharp | Azure AI | HTML | JavaScript | CSS | SQL Server
OOPS in C#
C# Data Types
Boxing and Unboxing in C#
Garbage Collection in C#
C# - Conditional Statements
C# - Loops
Interfaces in C#
Generics in C#
Collections in C#
C# 8.0 new features
C# Singleton Design Pattern
C# Factory Design Pattern
LINQ in C#
C# - Program to Find Prime Numbers
C# - Fibonacci Sequence
C# - Factorial of a number
C# - Recursive methods
C# - Anonymous Methods
C# - String Methods
C# TDD - XUnit
C# TDD - NUnit
C# Multiton Design Pattern
C# Facade Design Pattern
C# Abstract Factory Design Pattern
C# Decorator Design Pattern
C# Composite Design Pattern

C# Facade Design Pattern



The Facade Design Pattern is a structural pattern that provides a simplified interface to a complex subsystem. It helps to hide the complexities of the system and provides an easy-to-use interface to the clients.

Here's an example of how you can implement the Facade Design Pattern in C#:

csharp
using System;

namespace FacadePattern
{
    // Subsystem class A
    public class SubsystemA
    {
        public void OperationA()
        {
            Console.WriteLine("SubsystemA: OperationA");
        }
    }

    // Subsystem class B
    public class SubsystemB
    {
        public void OperationB()
        {
            Console.WriteLine("SubsystemB: OperationB");
        }
    }

    // Subsystem class C
    public class SubsystemC
    {
        public void OperationC()
        {
            Console.WriteLine("SubsystemC: OperationC");
        }
    }

    // Facade class
    public class Facade
    {
        private SubsystemA _subsystemA;
        private SubsystemB _subsystemB;
        private SubsystemC _subsystemC;

        public Facade()
        {
            _subsystemA = new SubsystemA();
            _subsystemB = new SubsystemB();
            _subsystemC = new SubsystemC();
        }

        public void Operation1()
        {
            Console.WriteLine("Facade: Operation1");
            _subsystemA.OperationA();
            _subsystemB.OperationB();
        }

        public void Operation2()
        {
            Console.WriteLine("Facade: Operation2");
            _subsystemB.OperationB();
            _subsystemC.OperationC();
        }
    }

    // Client code
    class Program
    {
        static void Main(string[] args)
        {
            Facade facade = new Facade();

            facade.Operation1();
            facade.Operation2();

            // Outputs:
            // Facade: Operation1
            // SubsystemA: OperationA
            // SubsystemB: OperationB
            // Facade: Operation2
            // SubsystemB: OperationB
            // SubsystemC: OperationC
        }
    }
}

Explanation:

  1. Subsystem Classes (SubsystemA, SubsystemB, SubsystemC): These are the classes that perform complex operations.

  2. Facade Class: This class wraps the subsystem classes and provides a simplified interface to the client. It contains methods Operation1 and Operation2, which internally call the necessary operations of the subsystem classes.

  3. Client Code: The client interacts with the Facade class instead of the subsystem classes directly. This simplifies the interaction and hides the complexity.

The Facade Design Pattern is useful when you want to provide a simple interface to a complex subsystem. It decouples the client from the subsystem and makes the system easier to use.

If you have any further questions or need more examples, feel free to ask!




All rights reserved | Privacy Policy | Sitemap