Home > Software engineering >  Refactoring code using SOLID principles and Design Patterns
Refactoring code using SOLID principles and Design Patterns

Time:11-18

I am starting to learn C# design SOLID principles and design patterns. I just want to ask what could be the best design pattern to be used to refactor this particular code.

using InvoiceApp.Invoices;

namespace InvoiceApp
{
    internal class InvoiceProcessor
    {
        internal void Process(int client)
        {
            Console.WriteLine("Processing invoice...");
            Invoice invoice;
            switch (client)
            {
                case 0:
                    invoice = new SimpleInvoice();
                    break;
                case 1:
                    invoice = new InvoiceWithHeader();
                    break;
                case 2:
                    invoice = new InvoiceWithFooter();
                    break;
                case 3:
                case 4:
                    invoice = new InvoiceWithHeaderFooter();
                    break;
                default:
                    throw new ArgumentException("Invalid client");
            }

            invoice.CreateInvoice();

            DisplayInvoice(client, invoice);
            SaveInvoice(client, invoice);
        }


        private void DisplayInvoice(int client, Invoice invoice)
        {
            if (client == 4)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(invoice.Data);
            } else
            {
                Console.WriteLine(invoice.Data);
            }
        }
        private void SaveInvoice(int client, Invoice invoice)
        {
            string data = invoice.Data;

            //cipher first
            switch (client)
            {
                case 0:
                case 1:
                    data = CaesarCipher(data);
                    break;
                case 2:
                case 3:
                    data = WeirdCipher(data);
                    break;
            }

            File.WriteAllText("Invoice.txt", data);

            Console.WriteLine("Invoice data saved!");
        }

        private string CaesarCipher(string input)
        {
            const int key = 4;
            string output = string.Empty;

            foreach (char ch in input)
            {
                if (!char.IsLetter(ch))
                {

                    output  = ch;
                }

                char d = char.IsUpper(ch) ? 'A' : 'a';
                output  = (char)((((ch   key) - d) % 26)   d);
            }

            return output;
        }

        private string WeirdCipher(string input)
        {
            return input.Replace('A', '$').Replace('H', '#');
        }
    }
}


I know this class violates SRP and maybe Dependency Injection(not sure), but I am having a hard time what could be the best design pattern to use to make the the implementation dynamic and maintainable.

CodePudding user response:

I think one good option is the Strategy pattern. Please take a look on that.

CodePudding user response:

This class is not doing a lot in the first place. Sure, you can move you initialization to a factory making it more clean, but other than that, the class is processing invoices.

  • Related