Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            // separation of concerns
            //     different "concerns" or "considerations" in code shouldn't be tangled together
            // single responsibility principle (S in SOLID)
            //     a unit of code (e.g. class, method) should have just one responsibility.
            // DRY principle
            //     don't repeat yourself
            // KISS
            //     keep it simple stupid (unless it's on codesignal)

            // have a list of products
            IEnumerable <Product> catalog = GetProducts();

            // allow for some customization of that display to the user
            string input = null;

            while (input != "s" && input != "d")
            {
                Console.WriteLine("Enter s for simple, d for detailed: ");
                input = Console.ReadLine();
            }
            string input2 = null;

            while (input2 != "y" && input2 != "n")
            {
                Console.WriteLine("Sort? y/n: ");
                input2 = Console.ReadLine();
            }

            ISorter sorter;

            if (input2 == "y")
            {
                sorter = new PriceSorter();
            }
            else
            {
                sorter = new NonSorter();
            }

            IWriter writer;

            // display them to the user
            if (input == "s")
            {
                writer = new SimpleWriter(sorter);
            }
            else
            {
                writer = new DetailedWriter(sorter);
            }

            writer.FormatAndDisplay(catalog);

            // dependency inversion principle (D in SOLID)
            // - don't have classes depend on each other directly
            //   instead, have them depend on interfaces
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            // separation of concerns
            // different concerns in code shouldn't be tangled together

            // single responsibility principle (S in SOLID)
            // a unit of code like class method should have just one responsibility

            // DRY principle do not repeat yourself
            // KISS Keep it simple stupid

            // functionalities
            // have a list of products
            List <Product> catalog = GetProducts();

            // user's choice of sort and display
            string userInput = null;

            while (userInput != "s" && userInput != "d")
            {
                Console.WriteLine("Enter s for simple, d for detailed");
                userInput = Console.ReadLine();
            }

            // dependency inversion principle (D in SOLID)
            // dont have classes depend on each other directly
            // instead, have them depend on
            //  A(B)  ,  A(Interface B)
            // polymorphism
            string userInput2 = null;

            while (userInput2 != "y" && userInput2 != "n")
            {
                Console.WriteLine("Have the list sorted? y/n ");
                userInput2 = Console.ReadLine();
            }

            ISorter sorter;

            if (userInput2 == "y")
            {
                sorter = new PriceSorter();
            }
            else
            {
                sorter = new NonSorter();
            }

            // sorted catalog goes in to writer
            IWriter writer;

            if (userInput == "s")
            {
                writer = new SimpleWriter(sorter);
            }
            else
            {
                writer = new DetailedWriter(sorter);
            }

            writer.FormatAndDisplay(catalog);
        }