Пример #1
0
        static void Mainn(string[] args)
        {
            Console.WriteLine("\n\n**********  Open Closed Principle  **********");
            var apple = new Product("Apple", Color.Green, Size.Small);
            var tree  = new Product("Tree", Color.Green, Size.Large);
            var house = new Product("House", Color.Blue, Size.Large);

            Product[] products = { apple, tree, house };
            var       pf       = new ProductFilter();

            Console.WriteLine("Green products (old):");
            foreach (var prod in pf.FilterByColor(products, Color.Green))
            {
                Console.WriteLine(prod);
            }

            var bf = new BetterFilter();

            Console.WriteLine("Green products (new):");
            foreach (var prod in bf.Filter(products, new ColorSpecification(Color.Green)))
            {
                Console.WriteLine(prod);
            }


            Console.WriteLine("Large blue items:");
            foreach (var prod in bf.Filter(products, new AndSpecification <Product>(new ColorSpecification(Color.Blue), new SizeSpecification(Size.Large))))
            {
                Console.WriteLine(prod);
            }
        }
Пример #2
0
        private static void Main(string[] args)
        {
            Product apple = new Product("Apple", Color.Blue, Size.Small);
            Product tree  = new Product("Tree", Color.Red, Size.Large);
            Product house = new Product("House", Color.Yellow, Size.Large);

            Product[]     products      = { apple, tree, house };
            ProductFilter productFilter = new ProductFilter();

            Console.WriteLine("Red products (old method): ");
            foreach (Product product in productFilter.FilterByColor(products, Color.Red))
            {
                Console.WriteLine($" - {product.Name} is red");
            }

            BetterFilter betterFilter = new BetterFilter();

            Console.WriteLine("Green products (new): ");
            foreach (Product product in betterFilter.Filter(products, new ColorSpecification(Color.Red)))
            {
                Console.WriteLine($" - {product.Name} is red");
            }

            Console.WriteLine("Large yellow items: ");
            foreach (Product product in betterFilter.Filter(products, new AndSpecification <Product>(new ColorSpecification(Color.Yellow), new SizeSpecification(Size.Large))))
            {
                Console.WriteLine($" - {product.Name} is large and yellow");
            }
        }
Пример #3
0
        /// <summary>
        /// Open Closed Principle: Parts of System should be open for extension, but closed for modification. Code updates should result in new code, no re-written code.
        /// </summary>
        private static void ExecuteOpenClosedPrinciple()
        {
            var apple = new Product("Apple", Color.Green, Size.Small);
            var tree  = new Product("Tree", Color.Green, Size.Large);
            var house = new Product("House", Color.Blue, Size.Large);

            Product[] products = { apple, tree, house };

            foreach (var product in ProductFilter.FilterByColor(products, Color.Green))
            {
                Console.WriteLine($" - {product.Name} is Green");
            }

            var betterFilter = new BetterFilter();

            Console.WriteLine("Green products (new):");
            foreach (var p in betterFilter.Filter(products, new ColorSpecification(Color.Green)))
            {
                Console.WriteLine($" - {p.Name} is green");
            }
            Console.WriteLine("Large products:");
            foreach (var p in betterFilter.Filter(products, new SizeSpecification(Size.Large)))
            {
                Console.WriteLine($" - {p.Name} is large");
            }
            Console.WriteLine("Large blue items");
            foreach (var p in betterFilter.Filter(products, new MultipleSpecification <Product>(new ColorSpecification(Color.Blue), new SizeSpecification(Size.Large))))
            {
                Console.WriteLine($" - {p.Name} is big and blue");
            }
        }
Пример #4
0
        private static void OpenClosedPrinciple()
        {
            Console.WriteLine("Open Closed Principle");
            Product[] products = GetProducts();

            Console.WriteLine("Violates OCP");
            var filter = new ProductFilter();

            Console.WriteLine("Blue Items");
            foreach (var prod in filter.FilterByColor(products, Color.Blue))
            {
                Console.WriteLine("--- {0}", prod);
            }

            Console.WriteLine("Large Items");
            foreach (var prod in filter.FilterBySize(products, Size.Large))
            {
                Console.WriteLine("--- {0}", prod);
            }

            Console.WriteLine("\nDoes not violate OCP\n");
            //არ არღვევს ocp-s

            var bf = new BetterFilter();

            Console.WriteLine("Green Products");
            foreach (var prod in bf.Filter(products, new ColorSpecification(Color.Green)))
            {
                Console.WriteLine("---  {0}", prod);
            }

            Console.WriteLine("---Small Products---");
            foreach (var prod in bf.Filter(products, new SizeSpecification(Size.Small)))
            {
                Console.WriteLine("--- {0}", prod);
            }

            Console.WriteLine("Large And Blue Items");
            foreach (var prod in bf.Filter(products,
                                           new AndSpecification <Product>(
                                               new ColorSpecification(Color.Blue),
                                               new SizeSpecification(Size.Large)
                                               )))
            {
                Console.WriteLine("--- {0}", prod);
            }

            Console.WriteLine("Medium Or Large Items");
            foreach (var prod in bf.Filter(products,
                                           new OrSpecification <Product>(
                                               new SizeSpecification(Size.Medium),
                                               new SizeSpecification(Size.Large))))
            {
                Console.WriteLine("--- {0}", prod);
            }


            Console.WriteLine();
        }
Пример #5
0
        static void RunOpenClosed()
        {
            IEnumerable <Product> products = new List <Product>
            {
                new Product("Apple", Color.Green, Size.Small),
                new Product("Tree", Color.Green, Size.Large),
                new Product("House", Color.Blue, Size.Large),
            };


            ProductFilter         productFilter    = new ProductFilter();
            IEnumerable <Product> filteredProducts = Enumerable.Empty <Product>();

            filteredProducts = productFilter.FilterByColor(products, Color.Green);
            Console.WriteLine("Green Products (old):");
            foreach (var product in filteredProducts)
            {
                Console.WriteLine($"- {product.Name} is {Color.Green.ToString()}");
            }

            filteredProducts = Enumerable.Empty <Product>();

            BetterFilter       betterFilter       = new BetterFilter();
            ColorSpecification colorSpecification = new ColorSpecification(Color.Green);

            Console.WriteLine("Green Products (new):");
            filteredProducts = betterFilter.Filter(products, colorSpecification);
            foreach (var product in filteredProducts)
            {
                Console.WriteLine($"- {product.Name} is {Color.Green.ToString()}");
            }


            filteredProducts = Enumerable.Empty <Product>();
            SizeSpecification sizeSpecification = new SizeSpecification(Size.Large);

            Console.WriteLine("Large Products (new):");
            filteredProducts = betterFilter.Filter(products, sizeSpecification);
            foreach (var product in filteredProducts)
            {
                Console.WriteLine($"- {product.Name} is {Size.Large.ToString()}");
            }

            filteredProducts = Enumerable.Empty <Product>();
            SizeAndColorSpecification <Product> sizeAndCOlorSpecification = new SizeAndColorSpecification <Product>(colorSpecification, sizeSpecification);

            Console.WriteLine("Large Products (new):");
            filteredProducts = betterFilter.Filter(products, sizeAndCOlorSpecification);
            foreach (var product in filteredProducts)
            {
                Console.WriteLine($"- {product.Name} is {Size.Large.ToString()}");
            }
        }
Пример #6
0
        static void Main(string[] args)
        {
            var j = new Journal();

            Console.WriteLine("SolidSRP");
            j.AddEntry("I wake up");
            j.AddEntry("I lunched");
            Console.WriteLine(j.ToString());


            Console.WriteLine("===========\n SolidORP");
            Console.WriteLine("*Filtering Without SOLIDORP*");
            var apple = new Product("Appel", Color.Green, Size.Small);
            var tree  = new Product("Tree", Color.Green, Size.Large);
            var house = new Product("House", Color.Blue, Size.Large);

            Product[] products = { apple, tree, house };
            var       pf       = new ProductFilter();

            Console.WriteLine("Green products (old): ");
            foreach (var p in pf.FilterByColor(products, Color.Green))
            {
                Console.WriteLine($" - {p.Name} is green");
            }

            Console.WriteLine("*Filtering With SOLIDORP*");
            var bf = new BetterFilter();

            Console.WriteLine("Green products (new): ");
            foreach (var p in bf.Filter(products, new ColorSpecification(Color.Green)))
            {
                Console.WriteLine($" - {p.Name} is green");
            }
            Console.WriteLine("Large blue items");
            foreach (var p in bf.Filter(products, new AndSpecification <Product>(new ColorSpecification(Color.Blue), new SizeSpecification(Size.Large))))
            {
                Console.WriteLine($" - {p.Name} is blue and big");
            }
            Console.WriteLine("===========\n SOLID LSP ");


            Rectangle rc = new Rectangle(2, 3);

            Console.WriteLine($"{rc} has area {Area(rc)}");

            Square sq = new Square();

            sq.Width = 4;
            Console.WriteLine($"{sq} has area {Area(sq)}");

            Console.WriteLine("Enter a key");
            Console.ReadKey();
        }
Пример #7
0
        static void Main(string[] args)
        {
            // Refers to first section
            var j = new Journal();

            j.addEntry("I Cried");
            j.addEntry("I know");
            Console.WriteLine(j);

            var p        = new Persistence();
            var filename = @"d:\temp\journals\j1.txt";

            p.SaveToFile(j, filename, true);
            //Process.Start(filename);

            var apple = new Product("Apple", Color.Green, Size.Small);
            var tree  = new Product("Tree", Color.Green, Size.Big);
            var house = new Product("House", Color.Blue, Size.Big);

            Product[] products = { apple, tree, house };

            var pf = new ProductFilter();

            Console.WriteLine("Green products (old):"); // Every time we are adding filter we will need to add another method in  the product filter class
            foreach (var prod in pf.FilterByColor(products, Color.Green))
            {
                Console.WriteLine($" - {prod.Name} is Green");
            }
            // Now we to filter by both. So this is a problem - we will need another function
            // Adding a function with filtering by both color and size.
            // The main concept is that you can always expand the class but without changing the things before
            // We can implement a pattern - specification pattern - we will add interfaces
            // Now using the better filter
            // The second iteration is for both size and color - adding another class AndSpecification for combining any both filters
            var bf = new BetterFilter();

            Console.WriteLine("Green Products (new)");
            foreach (var prod in bf.Filter(products, new ColorSpecification(Color.Green)))
            {
                Console.WriteLine($" - {prod.Name} is Green");
            }

            Console.WriteLine("Big Blue Houses");
            foreach (var prod in bf.Filter
                         (products,
                         new AndSpecification <Product>(new ColorSpecification(Color.Blue),
                                                        new SizeSpecification(Size.Big))))
            {
                Console.WriteLine($" - {prod.Name} is big and blue");
            }
        }
Пример #8
0
        static void Main(string[] args)
        {
            Product[] products = new Product[]
            {
                new Product("Apple", Color.Green, Size.Small),
                new Product("Tree", Color.Green, Size.Large),
                new Product("House", Color.Blue, Size.Large)
            };
            var bf = new BetterFilter();

            foreach (var p in bf.Filter(products,
                                        new AndSpecification <Product>(new ColorSpecification(Color.Green), new SizeSpecification(Size.Large))))
            {
                Console.WriteLine($"name : {p.Name}");
            }
            foreach (var p in bf.Filter(products, new ColorSpecification(Color.Green)))
            {
                Console.WriteLine($"name : {p.Name}");
            }
        }
Пример #9
0
        static void SOLIDOpenClosed()
        {
            var apple = new Product("Apple", Color.Green, Size.Small);
            var tree  = new Product("Tree", Color.Green, Size.Large);
            var house = new Product("House", Color.Blue, Size.Large);

            Product[] products = { apple, tree, house };

            var pf = new ProductFilter();

            Console.WriteLine("Green products (old):");
            foreach (var p in pf.FilterByColor(products, Color.Green))
            {
                Console.WriteLine($" - {p.Name} is green");
            }

            // ^^ BEFORE

            // vv AFTER
            var bf = new BetterFilter();

            Console.WriteLine("Green products (new):");
            foreach (var p in bf.Filter(products, new ColorSpecification(Color.Green)))
            {
                Console.WriteLine($" - {p.Name} is green");
            }

            Console.WriteLine("Large products");
            foreach (var p in bf.Filter(products, new SizeSpecification(Size.Large)))
            {
                Console.WriteLine($" - {p.Name} is large");
            }

            Console.WriteLine("Large blue items");
            foreach (var p in bf.Filter(products,
                                        new BanchOfSpecifications <Product>(new ColorSpecification(Color.Blue), new SizeSpecification(Size.Large)))
                     )
            {
                Console.WriteLine($" - {p.Name} is big and blue");
            }
        }
Пример #10
0
        public static void Test()
        {
            var apple = new Product("Apple", Color.RED, Size.SMALL);
            var tree  = new Product("Tree", Color.GREEN, Size.LARGE);
            var house = new Product("House", Color.BLUE, Size.LARGE);

            Product[] products = { apple, tree, house };

            // INCORRECT IMPLEMENTATION

            /*
             * var pf = new ProductFilter();
             * WriteLine("Green products (incorrect):");
             *
             * foreach (var p in pf.FilterByColor(products, Color.GREEN)) {
             *  WriteLine($" - {p.Name} is green");
             * }
             */

            // CORRECT IMPLEMENTATION
            var bf = new BetterFilter();

            WriteLine("Green products (correct):");

            foreach (var p in bf.Filter(products, new ColorSpecification(Color.GREEN)))
            {
                WriteLine($" - {p.Name} is green");
            }

            WriteLine("Large blue items");
            foreach (var p in bf.Filter(
                         products,
                         new AndSpecification <Product>(
                             new ColorSpecification(Color.BLUE),
                             new SizeSpecification(Size.LARGE)
                             )))
            {
                WriteLine($" - {p.Name} is big and blue");
            }
        }
Пример #11
0
        static void Filter()
        {
            var apple = new Product("apple", Size.Small, Color.Red);
            var tree  = new Product("tree", Size.Large, Color.Green);
            var house = new Product("house", Size.Large, Color.Blue);

            Product[] products = { apple, tree, house };

            var bf = new BetterFilter();

            foreach (var i in bf.Filter(products, new ColorSpecification(Color.Blue)))
            {
                Console.WriteLine(i);
            }
            Console.WriteLine("Hello World!");
        }
            static void Main(string[] args)
            {
                var apple = new Product("Apple", Color.Green, Size.Small);
                var tree  = new Product("Tree", Color.Green, Size.Large);
                var house = new Product("House", Color.Blue, Size.Large);

                Product[] products = { apple, tree, house };

                var pf = new ProductFilter();

                Console.WriteLine("Green Products (old): ");
                foreach (var p in pf.FilterByColor(products, Color.Green))
                {
                    Console.WriteLine($"- {p.Name} is green");
                }

                var bf = new BetterFilter();

                Console.WriteLine("Green Prodcuts (new) : ");
                foreach (var p in bf.Filter(products, new ColorSpecification(Color.Green)))
                {
                    Console.WriteLine($"- {p.Name} is green");
                }
            }