コード例 #1
0
        public static void Start()
        {
            var apple = new Product("Apple", Color.Red, Size.Medium);
            var tree  = new Product("Tree", Color.Green, Size.Large);
            var house = new Product("House", Color.Green, Size.Medium);

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

            Console.WriteLine("Filter by Size {Medium}");
            foreach (var item in productFilter.FilterBySize(products, Size.Medium))
            {
                Console.WriteLine($"- {item.Name}, {item.Color}, {item.Size}");
            }
            Console.WriteLine("Filter by Color {Green}");
            foreach (var item in productFilter.FilterByColor(products, Color.Green))
            {
                Console.WriteLine($"- {item.Name}, {item.Color}, {item.Size}");
            }

            Console.WriteLine("Open Close Better filter");
            var betterFilter = new BetterFilter();

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

            Console.WriteLine("Filter by Size {Medium}");
            foreach (var item in betterFilter.Filter(products, new SizeSpecification(Size.Medium)))
            {
                Console.WriteLine($"- {item.Name}, {item.Color}, {item.Size}");
            }

            Console.WriteLine("Combination");
            var andSpecification = new AndSpecification <Product>(
                new ColorSpecification(Color.Green),
                new SizeSpecification(Size.Medium));

            foreach (var item in betterFilter.Filter(products, andSpecification))
            {
                Console.WriteLine($"- {item.Name}, {item.Color}, {item.Size}");
            }
        }
コード例 #2
0
        static void Main(string[] args)
        {
            //create a journal
            var journal = new Journal();
            //create persistance
            var p = new Persist();

            p.SaveToFile(journal, "");


            //specification pattern for open / close principle
            Product[] products = new Product[3];
            var       product1 = new Product()
            {
                color = Color.blue,
                Name  = "product1",
                Size  = Size.small
            };
            var product2 = new Product()
            {
                color = Color.green,
                Name  = "product2",
                Size  = Size.large
            };
            var product3 = new Product()
            {
                color = Color.red,
                Name  = "Product3",
                Size  = Size.XLarge
            };

            products[0] = product1;
            products[1] = product2;
            products[2] = product3;


            var bf = new BetterFilter();

            bf.Filter(products, new ColorSpecification());
            bf.Filter(products, new AndSpecification <Product>(new ColorSpecification(), new SizeSpecification()));
        }
コード例 #3
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 bf = new BetterFilter();

            Console.WriteLine("Green products");
            foreach (var p in bf.Filter(products, new ColorSpecification(Color.Green)))
            {
                Console.WriteLine($"- {p.Name} is green");
            }
            foreach (var p in bf.Filter(
                         products,
                         new AndSpecification <Product>(new ColorSpecification(Color.Blue), new SizeSpecification(Size.Large))))
            {
                Console.WriteLine($"- {p.Name} is big and blue");
            }
        }
コード例 #4
0
        static void Main(string[] args)
        {
            #region Single Responsability Principle

            var j = new Journal();
            j.AddEntry("I cried today.");
            j.AddEntry("I ate a bug.");
            Console.WriteLine(j);

            var p        = new Persistence();
            var filename = @"c:\temp\journal.txt";
            p.SaveToFile(j, filename);
            Process.Start(filename);

            #endregion

            #region 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 pfp in pf.FilterByColor(products, Color.Green))
            {
                Console.WriteLine($" - {pfp.Name} is green");
            }

            // ^^ BEFORE

            // vv AFTER
            var bf = new BetterFilter();
            Console.WriteLine("Green products (new):");
            foreach (var pfp in bf.Filter(products, new ColorSpecification(Color.Green)))
            {
                Console.WriteLine($" - {pfp.Name} is green");
            }

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

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

            #endregion

            #region Liskov Substitution

            Rectangle rc = new Rectangle(2, 3);
            Console.WriteLine($"{rc} has area {Area(rc)}");

            // should be able to substitute a base type for a subtype
            /*Square*/
            Rectangle sq = new Square();
            sq.Width = 4;

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

            #endregion

            #region Interface Segregation

            #endregion

            #region Dependency Inversion

            var parent = new Person {
                Name = "John"
            };
            var child1 = new Person {
                Name = "Chris"
            };
            var child2 = new Person {
                Name = "Matt"
            };

            // low-level module
            var relationships = new Relationships();
            //relationships.AddParentAndChild(parent, child1);
            //relationships.AddParentAndChild(parent, child2);

            new Research(relationships);

            #endregion

            Console.ReadKey();
        }