Пример #1
0
        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 };

            //b4 priciple
            var pf = new ProductFilter();

            Console.WriteLine("Green Products (old):");

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

            //after applying principles
            var bf = new BetterFilter();

            Console.WriteLine("Green Products (new):");

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

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

            Console.WriteLine("Large Blue Products (new):");
            foreach (var p in bf.Filter(products, new AndSpecification <Product>(new ColorSpecification(Color.Blue), new SizeSpecification(Size.Large))))
            {
                Console.WriteLine($" - {p.Name} is large and blue");
            }


            Console.ReadLine();
        }
Пример #2
0
        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 Product (Old)");
            foreach (var p in pf.FilterByColor(products, Color.Green))
            {
                WriteLine($" - { p.Name} is Green");
            }

            //
            //  AFTER



            var bf = new BetterFilter();

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

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

            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");
            }
        }
        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();

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

            foreach (var c in pf.Filter(products, new AndSpecification <Product>(new ColorSpecification(Color.Blue), new SizeSpecification(Size.Large))))
            {
                Console.WriteLine($" - {c.Name } is big and {c.Size} ");
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("OpenClosedPrinciple Demo!");

            var apple = new Product("Apple", Color.Green, Size.small);
            var mango = new Product("Mango", Color.Yellow, Size.medium);
            var House = new Product("House", Color.Red, Size.Large);

            Product[] products = { apple, mango, 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.");
            }

            BetterFilter 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 red item");

            foreach (var p in bf.Filter
                         (products,
                         new AndSpecification <Product>
                             (new ColorSpecification(Color.Red),
                             new SizeSpecification(Size.Large)
                             )))

            {
                Console.WriteLine($"-{p.Name} is large and red.");
            }
        }
Пример #5
0
        // The class should be opened for extension but should be closed for modification (inerfaces and inheritance)
        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 };

            // old one
            var pf = new ProductFilter();

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

            // new one
            //created ISpecification(condidion) and IFilter(checking condition)
            var bf = new BetterFilter();

            Console.WriteLine("Green products new :");

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

            //using combined specification
            Console.WriteLine("Large blue items: ");
            foreach (var item in bf.Filter(products, new AndSpecification <Product>(
                                               new ColorSpecification(Color.Blue),
                                               new SizeSpecification(Size.Large)
                                               )))
            {
                Console.WriteLine($" - {item.Name} is large and blue");
            }
        }
Пример #6
0
        static void Main(string[] args)
        {
            Product apple = new Product("Apple", Color.Green, Size.Small);
            Product tree  = new Product("Tree", Color.Green, Size.Small);
            Product house = new Product("House", Color.Blue, Size.Large);

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

            // old
            ProductFilter pf = new ProductFilter();

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

            // new
            BetterFilter bf = new BetterFilter();

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

            // combinator
            WriteLine("\nLarge blue items");
            foreach (Product p in bf.Filter(
                         products,
                         new AndSpecification <Product>(
                             new ColorSpecification(Color.Blue),
                             new SizeSpecification(Size.Large)
                             )))
            {
                WriteLine($" - {p.Name} is large and blue");
            }
        }
Пример #7
0
        public static void Main(string[] args)
        {
            Product        house    = new Product("Tree", Color.Green, Size.Large);
            Product        tree     = new Product("Tree", Color.Green, Size.Medium);
            Product        mouse    = new Product("Mouse", Color.Yellow, Size.Small);
            Product        ball     = new Product("ball", Color.Yellow, Size.Large);
            List <Product> products = new List <Product>
            {
                house,
                tree,
                mouse,
                ball
            };
            var           specYellow    = new ColorSpecification(Color.Yellow);
            var           specLarge     = new SizeSpecification(Size.Small);
            var           specMultiple  = new AndSpecification <Product>(new ColorSpecification(Color.Yellow), specLarge);
            ProductFilter productFilter = new ProductFilter();

            foreach (Product yellowItem in productFilter.Filter(products, specMultiple))
            {
                Console.WriteLine(yellowItem.Name);
            }
        }
Пример #8
0
        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 productFilter = new ProductFilter();

            Console.WriteLine("Green products (old):");
            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 product in betterFilter.Filter(products, new ColorSpecification(Color.Green)))
            {
                Console.WriteLine($"- {product.Name} is green");
            }

            // -----------------------------------------------------------------------------

            Console.WriteLine("Large blue items");
            foreach (var product in betterFilter.Filter(products, new AndSpecification <Product>(new ColorSpecification(Color.Blue), new SizeSpecification(Size.Large))))
            {
                Console.WriteLine($"- {product.Name} is big and blue.");
            }

            Console.ReadLine();
        }