• 设计模式02SOLID(O)开闭原则


    Open Closed Principle:开闭原则

    • OpenClosed: 开闭原则。对拓展开放,对修改关闭,增加新需求时可以轻松拓展而不用修改已有代码。

    • 以下例子是对于属于的过滤(零件名称,零件颜色,零件尺寸分别过滤筛选)
    •            enum Color { Yellow,Red,Green }
                  enum Size
                  {
                      Small,Medium,Large,Yuge
                  }
      
                  class Product
                  {
                      public string Name;
                      public Color Color;
                      public Size Size;
      
                      public Product(string name, Color color, Size size)
                      {
                          Name = name;
                          Color = color;
                          Size = size;
                      }
                      public override string ToString()
                      {
                          return $"   -I'm Has a {Size} {Name} ,I't {Color}";
                      }
                  }
                  class ProductFilter
                  {
                      public static IEnumerable<Product> FilterBySize(IEnumerable<Product> products,Size size) 
                      {
                          foreach (var item in products)
                              if (item.Size == size)
                                  yield return item;
                      }
                  }
                  interface ISpecification<T>
                  {
                      bool IsSatified(T t);
                  }
                  interface IFilter<T>
                  {
                      IEnumerable<T> Filter(IEnumerable<T> items, ISpecification<T> spec);
                      //IEnumerable<T> FilterMore(IEnumerable<T> items, ISpecification<T> spec1, ISpecification<T> spec2);
                  }
      
                  class ColorSpecification : ISpecification<Product>
                  {
                      private Color Color;
      
                      public ColorSpecification(Color color)
                      {
                          Color = color;
                      }
      
                      public bool IsSatified(Product t)
                      {
                          return t.Color == Color;
                      }
                  }
                  class SizeSpecification : ISpecification<Product>
                  {
                      private Size size;
      
                      public SizeSpecification(Size size)
                      {
                          this.size = size;
                      }
      
                      public bool IsSatified(Product t)
                      {
                          return t.Size == size;
                      }
                  }
                  class AndSpecification<T> : ISpecification<T>
                  {
                      ISpecification<T> specification1, specification2;
      
                      public AndSpecification(ISpecification<T> specification1, ISpecification<T> specification2)
                      {
                          this.specification1 = specification1;
                          this.specification2 = specification2;
                      }
      
                      public bool IsSatified(T t)
                      {
                          return specification1.IsSatified(t) && specification2.IsSatified(t);
                      }
                  }
                  class BetterFilter : IFilter<Product>
                  {
                      public IEnumerable<Product> Filter(IEnumerable<Product> items, ISpecification<Product> spec)
                      {
                          foreach (var item in items)
                          {
                              if (spec.IsSatified(item))
                              {
                                  yield return item;
                              }
                          }
                      }
      
                      //public IEnumerable<Product> FilterMore(IEnumerable<Product> items, ISpecification<Product> spec1, ISpecification<Product> spec2)
                      //{
                      //    foreach (var item in items)
                      //    {
                      //        if (spec1.IsSatified(item)&& spec2.IsSatified(item))
                      //        {
                      //            yield return item;
                      //        }
                      //    }
                      //}
                  }
      
                  internal class Program
                  {
                      static void Main(string[] args)
                      {
                          Product apple = new Product("Apple", Color.Red, Size.Small);
                          Product banana = new Product("Banana", Color.Yellow, Size.Medium);
                          Product home = new Product("Home", Color.Green, Size.Large);
                          Product tree = new Product("Tree", Color.Green, Size.Large);
                          Product greenApple = new Product("GreenApple", Color.Green, Size.Small);
      
                          Product[] products = { apple, banana, home, tree,greenApple };
                          Console.WriteLine("Get Large (new):");
                          foreach (var item in ProductFilter.FilterBySize(products, Size.Large))
                          {
                              Console.WriteLine(item);
                          }
                          BetterFilter better = new BetterFilter();
                          Console.WriteLine("Get Large (new):");
                          foreach (var item in better.Filter(products, new ColorSpecification(Color.Green)))
                          {
                              Console.WriteLine(item);
                          }
                          Console.WriteLine("Get Green & Small (new):");
                          foreach (var item in better.Filter(products,
                              new AndSpecification<Product>(
                              new ColorSpecification(Color.Green),
                              new SizeSpecification(Size.Small)))
                              )
                          {
                              Console.WriteLine($"    -{item.Name} is big & Green");
                          }
                      }
                  }
  • 相关阅读:
    这样设计是否更好些~仓储接口是否应该设计成基础操作接口和扩展操作接口
    爱上MVC3~MVC+ZTree实现对树的CURD及拖拽操作
    Android自定义图形,图形的拼接、叠加、相容
    CSDN问答频道“华章杯”7月排行榜活动开始,丰厚奖品等你拿
    修改MyEclipse内存-------OutOfMemoryError错误
    MySQL中MySQL X.X Command Line Client一闪而过的问题
    C++组合问题
    Android编程之仿微信显示更多文字的View
    理解MapReduce哲学
    mysql 备份
  • 原文地址:https://www.cnblogs.com/Zingu/p/16256988.html
Copyright © 2020-2023  润新知