コード例 #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");
            }
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: 22rishuranjan/C_Sharp
        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.");
            }
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: horiunovaSasha/patterns
        // 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");
            }
        }
コード例 #5
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");
            }
        }
コード例 #6
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();
        }