Skip to main content
Commonmark migration
Source Link

#A Bit About OOP

A Bit About OOP

#A Bit About OOP

A Bit About OOP

Source Link
RubberDuck
  • 30.9k
  • 6
  • 71
  • 174

#A Bit About OOP

There's a really nasty switch statement in your Designer.

        switch (type)
        {
            case BootstrapType.Success:
                Console.ForegroundColor = ConsoleColor.Green;
                if(style == BootstrapStyle.Alert)
                {
                    Console.BackgroundColor = ConsoleColor.DarkGreen;
                }
                break;
            case BootstrapType.Info:
                Console.ForegroundColor = ConsoleColor.Cyan;
                if(style == BootstrapStyle.Alert)
                {
                    Console.BackgroundColor = ConsoleColor.DarkCyan;
                }
                break;
            case BootstrapType.Warning:
                Console.ForegroundColor = ConsoleColor.Yellow;
                if(style == BootstrapStyle.Alert)
                {
                    Console.BackgroundColor = ConsoleColor.DarkYellow;
                }
                break;
            case BootstrapType.Danger:
                Console.ForegroundColor = ConsoleColor.Red;
                if(style == BootstrapStyle.Alert)
                {
                    Console.BackgroundColor = ConsoleColor.DarkRed;
                }
                break;
            case BootstrapType.Magenta:
                Console.ForegroundColor = ConsoleColor.Magenta;
                if(style == BootstrapStyle.Alert)
                {
                    Console.BackgroundColor = ConsoleColor.DarkMagenta;
                }
                break;
            case BootstrapType.Cobalt:
                Console.ForegroundColor = ConsoleColor.Blue;
                if (style == BootstrapStyle.Alert)
                {
                    Console.BackgroundColor = ConsoleColor.DarkBlue;
                }
                break;
            default:
                Console.ForegroundColor = ConsoleColor.Gray;
                if(style == BootstrapStyle.Alert)
                {
                    Console.BackgroundColor = ConsoleColor.DarkGray;
                }
                break;
        }

In order to add any new styles to your code, you potentially have to make changes to three places in your code. Minimally, you have to add it to your enum and your switch. A little OOP can go about making this much easier to deal with. Particularly because these all do a very similar thing. You could extend this indefinitely and easily by inheriting from your Bootstrap class and overriding Customize.

First, you would need to change the signature of Customize in the base class so that we can over ride the method. Second, change it so that only the default case gets executed here.

static virtual void Customize(BootstrapStyle style)
{
    Console.ForegroundColor = ConsoleColor.Gray;
    if (style == Boostrap.Alert)
    {
        ConsoleColor.BackgroundColor = ConsoleColor.DarkGray;
    }
}

Note that I also removed the Type parameter, as we're replacing it with inherited classes.

Now we can go about implementing child classes like so.

public class SuccessBootstrap : Bootstrap
{
    static override void Customize(BootstrapStyle style)
    {
        Console.ForegroundColor = ConsoleColor.Green;
        if (style == Boostrap.Alert)
        {
            ConsoleColor.BackgroundColor = ConsoleColor.DarkGreen;
        }
    }
}

public class InfoBoostrap : Bootstrap
{
    static override void Customize(BootstrapStyle style)
    {
        Console.ForegroundColor = ConsoleColor.Cyan;
        if (style == Boostrap.Alert)
        {
            ConsoleColor.BackgroundColor = ConsoleColor.DarkCyan;
        }
    }
}

And so on. With this method of creating new types of coloring schemes, you don't even have to open up the Bootstrap file. You just create a new child class and over ride Customize.

There is one more refactoring that should probably happen though. The "If style == alert then set background color" logic gets repeated in each and every one of these overrides. It could be simplified by extracting this logic into a protected method of your base class.

protected void SetBackgroundColor(ConsoleColor backgroundColor, BootstrapStyle style)
{
    if(style == BootstrapStyle.Alert)
    {
        Console.BackgroundColor = backgroundColor;
    }
}

Which simplifies your child class implementations down to two dead simple lines of code.

public class SuccessBootstrap : Bootstrap
{
    static override void Customize(BootstrapStyle style)
    {
        Console.ForegroundColor = ConsoleColor.Green;
        base.SetBackgroundColor(ConsoleColor.DarkGreen, style);
    }
}

If you should decide to add a third style, you only need to update the logic in the base class.