Created
July 16, 2012 13:44
-
-
Save nelopauselli/3122807 to your computer and use it in GitHub Desktop.
Implementando SOLID - OCP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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