Skip to content

Instantly share code, notes, and snippets.

@nelopauselli
Created July 16, 2012 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nelopauselli/3122807 to your computer and use it in GitHub Desktop.
Save nelopauselli/3122807 to your computer and use it in GitHub Desktop.
Implementando SOLID - OCP
using System.Collections.Generic;
using System.Linq;
namespace OCP.Filter.Model
{
class ProductFilter
{
public IEnumerable<Product> ByCriteria(IList<Product> products, ICriteria criteria)
{
foreach (var product in products)
{
if (criteria.Match(product)) yield return product;
}
}
}
interface ICriteria
{
bool Match(Product product);
}
class MultiCriteria : ICriteria
{
private readonly IEnumerable<ICriteria> _criterias;
public MultiCriteria(IEnumerable<ICriteria> criterias)
{
_criterias = criterias;
}
public bool Match(Product product)
{
return _criterias.All(c=>c.Match(product));
}
}
class SizeCriteria : ICriteria
{
private readonly ProductSize _productSize;
public SizeCriteria(ProductSize productSize)
{
_productSize = productSize;
}
public bool Match(Product product)
{
return product.Size == _productSize;
}
}
class ColorCriteria : ICriteria
{
private readonly ProductColor _productColor;
public ColorCriteria(ProductColor productColor)
{
_productColor = productColor;
}
public bool Match(Product product)
{
return product.Color == _productColor;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment