Skip to content

Applying the Open-Closed Principle with C# and Broadcast Media Domain examples

Last updated on May 2, 2024

In the dynamic world of broadcast media, where new channels, programs, and formats emerge regularly, the ability to adapt and extend systems without modifying existing code is crucial. This is where the Open-Closed Principle (OCP) comes into play, offering a guiding principle for designing flexible and maintainable software solutions. In this blog post, we’ll explore how the OCP can be applied in the broadcast media domain using C# examples.

Understanding the Open-Closed Principle (OCP)

The Open-Closed Principle states that software entities should be open for extension but closed for modification. This means that the behavior of a module or class can be extended without altering its source code. In the context of broadcast media, this principle is particularly relevant for systems managing channels, programs, schedules, and their various interactions.

Scenario: Managing Programs and Schedules

Let’s consider a simplified scenario where we have a system responsible for managing programs and schedules for a television network. Initially, we might have a straightforward implementation like this:

public class Program
{
    public string Title { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
}

public class ScheduleManager
{
    private List<Program> _programs = new List<Program>();

    public void AddProgram(Program program)
    {
        _programs.Add(program);
    }

    public List<Program> GetPrograms(DateTime date)
    {
        return _programs.Where(p => p.StartTime.Date == date.Date).ToList();
    }
}

Extending without Modification

Now, suppose we want to introduce different types of programs, such as News, Sports, and Entertainment, each with its own way of displaying information in the schedule. Instead of modifying the existing Program class, we can extend it using inheritance and keep the existing functionality intact.

public abstract class Program
{
    public string Title { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
    public abstract string DisplayInfo();
}

public class NewsProgram : Program
{
    public override string DisplayInfo()
    {
        return $"[News] {Title} - {StartTime} to {EndTime}";
    }
}

public class SportsProgram : Program
{
    public override string DisplayInfo()
    {
        return $"[Sports] {Title} - {StartTime} to {EndTime}";
    }
}

public class EntertainmentProgram : Program
{
    public override string DisplayInfo()
    {
        return $"[Entertainment] {Title} - {StartTime} to {EndTime}";
    }
}

Adhering to OCP in Schedule Management

With the extended Program classes, we can now enhance the ScheduleManager to support the new program types without modifying its existing code.

public class ScheduleManager
{
    private List<Program> _programs = new List<Program>();

    public void AddProgram(Program program)
    {
        _programs.Add(program);
    }

    public List<Program> GetPrograms(DateTime date)
    {
        return _programs.Where(p => p.StartTime.Date == date.Date).ToList();
    }

    public void DisplayPrograms(DateTime date)
    {
        var programs = GetPrograms(date);
        foreach (var program in programs)
        {
            Console.WriteLine(program.DisplayInfo());
        }
    }
}

Benefits of OCP in Broadcast Media Domain:

  1. Flexibility: New program types can be introduced without modifying existing code, allowing for easy adaptation to changing requirements.
  2. Maintainability: Existing code remains unchanged, reducing the risk of introducing errors and making maintenance easier.
  3. Scalability: The system can grow to accommodate new program types and features without disrupting the existing functionality.

Conclusion:

In the broadcast media domain, adherence to the Open-Closed Principle is essential for building software systems that can evolve and adapt to changing needs without constant modification. By designing classes and modules that are open for extension but closed for modification, we can create robust, flexible, and maintainable solutions. In C#, the OCP enables us to develop scalable and adaptable broadcast media management systems that can thrive in a rapidly evolving industry landscape.

Published inBroadcast MediaS.O.L.I.D
LinkedIn
Share
WhatsApp