Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Sunday, November 2, 2025

🧩 Façade Design Pattern in C# – Simplifying Complex Systems

🔍 What is the Façade Design Pattern?

The Façade Design Pattern is a structural design pattern that provides a simplified interface to a complex subsystem of classes, libraries, or frameworks.
In simple terms, it hides the complexity of multiple interdependent systems behind a single, easy-to-use interface.

Think of a hotel receptionist — you don’t directly talk to housekeeping, room service, or maintenance. The receptionist (Façade) takes your request and communicates with the right departments internally.


🧠 Intent of the Façade Pattern

  • Simplify interaction between client and complex subsystems.

  • Reduce dependencies between client and internal components.

  • Make the system easier to use and maintain.


🧩 Structure (UML Conceptually)

Client → Facade → SubsystemA → SubsystemB → SubsystemC

The Client interacts with the Facade, which delegates calls to one or more Subsystems.


💻 C# Example: Home Theater System

Let’s say you’re building a Home Theater Application that involves multiple components:

  • DVD Player

  • Projector

  • Sound System

  • Lights

Instead of the client calling all these subsystems directly, we can use a Facade class to control them with a single method call.

Step 1: Subsystems

public class DVDPlayer { public void On() => Console.WriteLine("DVD Player On"); public void Play(string movie) => Console.WriteLine($"Playing '{movie}'"); public void Off() => Console.WriteLine("DVD Player Off"); } public class Projector { public void On() => Console.WriteLine("Projector On"); public void SetInput(string source) => Console.WriteLine($"Projector input set to {source}"); public void Off() => Console.WriteLine("Projector Off"); } public class SoundSystem { public void On() => Console.WriteLine("Sound System On"); public void SetVolume(int level) => Console.WriteLine($"Volume set to {level}"); public void Off() => Console.WriteLine("Sound System Off"); } public class Lights { public void Dim(int level) => Console.WriteLine($"Lights dimmed to {level}%"); }

Step 2: Façade Class

public class HomeTheaterFacade { private readonly DVDPlayer dvd; private readonly Projector projector; private readonly SoundSystem sound; private readonly Lights lights; public HomeTheaterFacade(DVDPlayer dvd, Projector projector, SoundSystem sound, Lights lights) { this.dvd = dvd; this.projector = projector; this.sound = sound; this.lights = lights; } public void WatchMovie(string movie) { Console.WriteLine("Get ready to watch a movie..."); lights.Dim(10); projector.On(); projector.SetInput("DVD Player"); sound.On(); sound.SetVolume(5); dvd.On(); dvd.Play(movie); } public void EndMovie() { Console.WriteLine("Shutting down the home theater..."); dvd.Off(); sound.Off(); projector.Off(); lights.Dim(100); } }

Step 3: Client Code

class Program { static void Main() { var dvd = new DVDPlayer(); var projector = new Projector(); var sound = new SoundSystem(); var lights = new Lights(); var homeTheater = new HomeTheaterFacade(dvd, projector, sound, lights); homeTheater.WatchMovie("Avengers: Endgame"); Console.WriteLine("\n--- Movie Finished ---\n"); homeTheater.EndMovie(); } }

🧾 Output:

Get ready to watch a movie... Lights dimmed to 10% Projector On Projector input set to DVD Player Sound System On Volume set to 5 DVD Player On Playing 'Avengers: Endgame' --- Movie Finished --- Shutting down the home theater... DVD Player Off Sound System Off Projector Off Lights dimmed to 100%

🚀 Real-Time Use Cases of Façade Pattern

ScenarioHow Façade Helps
Banking SystemsSimplifies complex operations like fund transfers by combining multiple services (accounts, validation, notification) into one interface.
E-commerce CheckoutCombines inventory, payment, and order services into one checkout process.
Hotel Booking APIsWraps flight, hotel, and transport systems behind a single booking interface.
Logging or Notification SystemsProvides one class to log to multiple targets (database, file, email).
Azure / AWS SDK WrappersDevelopers use simplified API wrappers to avoid dealing with multiple low-level SDK services.

⚖️ Advantages of the Façade Pattern

✅ Simplifies complex systems for clients
✅ Reduces coupling between client and subsystems
✅ Improves code readability and maintenance
✅ Makes the system more modular


⚠️ Disadvantages

❌ Overuse may hide useful functionality from the client
❌ Can become a "God Object" if it grows too large
❌ Difficult to maintain if subsystems frequently change


💡 Best Practices

  • Use when you have a complex system with multiple dependencies.

  • Keep the Facade thin — it should only simplify, not duplicate logic.

  • Combine with Singleton pattern for global access if needed.

  • Avoid making it responsible for business rules — just coordination.


🧭 Conclusion

The Façade Design Pattern acts like a front desk for your codebase — it hides unnecessary complexity and makes client interactions smooth and simple.
When used properly, it makes large systems more maintainable, readable, and user-friendly.


Monday, September 29, 2025

🏭 Factory Design Pattern in C# with Real-Time Example

📌 What is the Factory Design Pattern?

The Factory Design Pattern is a creational pattern used in object-oriented programming to abstract the process of object creation. Instead of instantiating classes directly using new, the Factory Pattern provides a method that returns instances of different classes based on input parameters.

This pattern is especially useful when:

  • The exact type of object to create is determined at runtime.
  • You want to encapsulate object creation logic.
  • You need to adhere to the Open/Closed Principle (open for extension, closed for modification).

🚗 Real-Time Example: Vehicle Factory in C#

Imagine you're building a transportation management system that handles different types of vehicles: Car, Bike, and Truck. Each vehicle has its own behavior, but the client code shouldn't worry about how these objects are created.

✅ Step-by-Step Implementation

1. Define a Common Interface

public interface IVehicle
{
    void Start();
}

2. Create Concrete Implementations

public class Car : IVehicle
{
    public void Start()
    {
        Console.WriteLine("Car started.");
    }
}

public class Bike : IVehicle
{
    public void Start()
    {
        Console.WriteLine("Bike started.");
    }
}

public class Truck : IVehicle
{
    public void Start()
    {
        Console.WriteLine("Truck started.");
    }
}

3. Implement the Factory Class

public static class VehicleFactory
{
    public static IVehicle GetVehicle(string vehicleType)
    {
        switch (vehicleType.ToLower())
        {
            case "car":
                return new Car();
            case "bike":
                return new Bike();
            case "truck":
                return new Truck();
            default:
                throw new ArgumentException("Invalid vehicle type");
        }
    }
}

4. Client Code

class Program
{
    static void Main(string[] args)
    {
        IVehicle vehicle1 = VehicleFactory.GetVehicle("car");
        vehicle1.Start();

        IVehicle vehicle2 = VehicleFactory.GetVehicle("bike");
        vehicle2.Start();
    }
}

🎯 Benefits of Using Factory Pattern in C#

  • Encapsulation: Object creation logic is hidden from the client.
  • Scalability: Easily add new vehicle types without changing client code.
  • Maintainability: Centralized object creation makes debugging and updates easier.
  • Loose Coupling: Promotes interface-based programming.

📝 Final Thoughts

The Factory Design Pattern is a cornerstone of clean, scalable software architecture. Whether you're building enterprise applications or small utilities, this pattern helps you write flexible and maintainable code.


Thursday, September 25, 2025

Write a program to Generate all possible permutations of the string "ABCDEF" using C#.

 Since "ABCDEF" has 6 characters, the number of permutations = 6! = 720.

Here’s a C# program to generate and print them:

using System; using System.Collections.Generic; class Program { static void Main() { string input = "ABCDEF"; var results = new List<string>(); GetPermutations(input.ToCharArray(), 0, results); Console.WriteLine($"Total Permutations: {results.Count}"); foreach (var str in results) { Console.WriteLine(str); } } static void GetPermutations(char[] arr, int index, List<string> results) { if (index == arr.Length - 1) { results.Add(new string(arr)); } else { for (int i = index; i < arr.Length; i++) { Swap(ref arr[index], ref arr[i]); GetPermutations(arr, index + 1, results); Swap(ref arr[index], ref arr[i]); // backtrack } } } static void Swap(ref char a, ref char b) { if (a == b) return; char temp = a; a = b; b = temp; } }

🔹 How it works:

  1. Uses recursion to generate all permutations.

  2. Swaps characters, calls recursively, then backtracks to restore the original order.

  3. Stores all unique permutations in a List<string>.


🔹 Sample Output:

Total Permutations: 720 ABCDEF ABCDE F ABCDFE ABCEDF ... FEDCBA

👉 This prints all 720 unique permutations of "ABCDEF".

Don't Copy

Protected by Copyscape Online Plagiarism Checker

Pages