This article is a continuation of my first article on the Specification Pattern in C#. I recommend you to read it for a better understanding of background concepts.
In the first part of this article, I introduced the specification pattern and how it can help to avoid violating the ‘O’ of SOLID principles of programming. Here, I’ll basically show how two or more specifications can be combined together to achieve more interesting results.
The simplest form of composite specifications are,
- AND Specification (Returns true when all conditions are met)
- OR Specification (Returns true when one or more conditions are met)
- NOT Specification (Returns true when a condition is not met)
- NOR Specification (Returns true when all conditions are NOT met)
- NAND Specification (Returns true when one or more conditions are NOT met)
There are actually other ways in which the conditions can be combined and evaluated but these are the easiest to comprehend. For a deeper understanding, you can read about Logic gates. However, I have put together a summary of their truth tables below (for a simple case of two conditions).
Condition 1 | Condition 2 | AND | OR | NOT | NOR | NAND |
T | T | T | T | F | F | F |
T | F | F | T | F | F | T |
F | T | F | T | T | F | T |
F | F | F | F | T | T | T |
Now, let’s apply them to our specification pattern by combining conditions.
First, we create an abstract CompositeSpecification class that implements the IEmployeeSpecification interface. This serves as the base class from which all our composite specifications inherit. It has a constructor that takes in two parameters of type IEmployeeSpecification (which, of course, can also be a CompositeSpecification and this can be very useful especially when we need to cascade composite specifications or create a composite specification with more than two parameters).
Take a look at the code listing below. I have excluded some details such as the Employee class (as seen in part one of the article - Specification Pattern in C#) for the purpose of brevity.
- publicinterfaceIEmployeeSpecification {
- bool IsSatisfiedBy(Employee employee);
- }
- publicclassEmployeeDepartmentSpecification: IEmployeeSpecification {
- privatereadonlystring _department;
- public EmployeeDepartmentSpecification(string depatrment) {
- _department = depatrment;
- }
- publicbool IsSatisfiedBy(Employee employee) {
- return employee.Department.Equals(_department);
- }
- }
- publicclassEmployeeYearSpecification: IEmployeeSpecification {
- privatereadonlyint _year;
- public EmployeeYearSpecification(int year) {
- _year = year;
- }
- publicbool IsSatisfiedBy(Employee employee) {
- return employee.YearOfResumption.Equals(_year);
- }
- }
- publicclassGetEmployee {
- publicstaticList < Employee > GetEmployeeBy(IEmployeeSpecification specification, Employee[] employees) {
- List < Employee > NeededEmployees = newList < Employee > ();
- foreach(Employee employee in employees) {
- if (specification.IsSatisfiedBy(employee)) {
- NeededEmployees.Add(employee);
- }
- }
- return NeededEmployees;
- }
- }
-
- publicabstractclassCompositeSpecification: IEmployeeSpecification {
- protectedIEmployeeSpecification specification1;
- protectedIEmployeeSpecification specification2;
- public CompositeSpecification(IEmployeeSpecification spec1, IEmployeeSpecification spec2) {
- specification1 = spec1;
- specification2 = spec2;
- }
- publicabstractbool IsSatisfiedBy(Employee employee);
- }
- publicclassAndSpecification: CompositeSpecification {
- public AndSpecification(IEmployeeSpecification spec1, IEmployeeSpecification spec2): base(spec1, spec2) {}
- publicoverridebool IsSatisfiedBy(Employee employee) {
- return specification1.IsSatisfiedBy(employee) && specification2.IsSatisfiedBy(employee);
- }
- }
- publicclassOrSpecification: CompositeSpecification {
- public OrSpecification(IEmployeeSpecification spec1, IEmployeeSpecification spec2): base(spec1, spec2) {}
- publicoverridebool IsSatisfiedBy(Employee employee) {
- return specification1.IsSatisfiedBy(employee) || specification2.IsSatisfiedBy(employee);
- }
- }
- publicclassNORSpecification: CompositeSpecification {
- public NORSpecification(IEmployeeSpecification spec1, IEmployeeSpecification spec2): base(spec1, spec2) {}
- publicoverridebool IsSatisfiedBy(Employee employee) {
- return !(specification1.IsSatisfiedBy(employee) || specification2.IsSatisfiedBy(employee));
- }
- }
- publicclassNANDSpecification: CompositeSpecification {
- public NANDSpecification(IEmployeeSpecification spec1, IEmployeeSpecification spec2): base(spec1, spec2) {}
- publicoverridebool IsSatisfiedBy(Employee employee) {
- return !(specification1.IsSatisfiedBy(employee) && specification2.IsSatisfiedBy(employee));
- }
- }
- publicclassNotSpecification: IEmployeeSpecification {
- privateIEmployeeSpecification specification;
- public NotSpecification(IEmployeeSpecification spec) {
- specification = spec;
- }
- publicbool IsSatisfiedBy(Employee employee) {
- return !specification.IsSatisfiedBy(employee);
- }
- }
Below is a sample program to test the workings of the code.
Note
You can also download and run the original source code from the top of this article.
- classProgram {
- staticvoid Main(string[] args) {
- Employee employee1 = newEmployee {
- FirstName = "Fidel", Department = "Maths", YearOfResumption = 2017
- };
- Employee employee2 = newEmployee {
- FirstName = "Francis", Department = "Software", YearOfResumption = 2016
- };
- Employee employee3 = newEmployee {
- FirstName = "Ahmed", Department = "Maths", YearOfResumption = 2016
- };
- Employee employee4 = newEmployee {
- FirstName = "Ebuka", Department = "Software", YearOfResumption = 2017
- };
- Employee[] employees = newEmployee[] {
- employee1,
- employee2,
- employee3,
- employee4
- };
- Console.WriteLine("Software Department");
- List < Employee > SoftwareEmployees = GetEmployee.GetEmployeeBy(newEmployeeDepartmentSpecification("Software"), employees);
- foreach(var employee in SoftwareEmployees) {
- Console.WriteLine(employee.FirstName);
- }
- Console.WriteLine("\nEmployed in 2017");
- List < Employee > EmployedIn2017 = GetEmployee.GetEmployeeBy(newEmployeeYearSpecification(2017), employees);
- foreach(var employee in EmployedIn2017) {
- Console.WriteLine(employee.FirstName);
- }
- AndSpecification YearAndDepartment = newAndSpecification(newEmployeeDepartmentSpecification("Software"), newEmployeeYearSpecification(2016));
- Console.WriteLine();
- Console.WriteLine("\nList of Software Staff Employed in 2016");
- List < Employee > Software2016Employees = GetEmployee.GetEmployeeBy(YearAndDepartment, employees);
- foreach(var employee in Software2016Employees) {
- Console.WriteLine(employee.FirstName);
- }
- Console.WriteLine("\nList of Staff either in Software or Employed in 2016");
- OrSpecification YearOrDepartment = newOrSpecification(newEmployeeDepartmentSpecification("Software"), newEmployeeYearSpecification(2016));
- List < Employee > SoftwareOr2016Employees = GetEmployee.GetEmployeeBy(YearOrDepartment, employees);
- foreach(var employee in SoftwareOr2016Employees) {
- Console.WriteLine(employee.FirstName);
- }
- Console.WriteLine("\nList of Staff neither in Software nor Employed in 2016");
- NORSpecification YearNorDepartment = newNORSpecification(newEmployeeDepartmentSpecification("Software"), newEmployeeYearSpecification(2016));
- List < Employee > SoftwareNor2016Employees = GetEmployee.GetEmployeeBy(YearNorDepartment, employees);
- foreach(var employee in SoftwareNor2016Employees) {
- Console.WriteLine(employee.FirstName);
- }
- Console.WriteLine("\nList of Staff who are not in Software or not Employed in 2016 or both");
- NANDSpecification YearNandDepartment = newNANDSpecification(newEmployeeDepartmentSpecification("Software"), newEmployeeYearSpecification(2016));
- List < Employee > SoftwareNand2016Employees = GetEmployee.GetEmployeeBy(YearNandDepartment, employees);
- foreach(var employee in SoftwareNand2016Employees) {
- Console.WriteLine(employee.FirstName);
- }
- Console.WriteLine("\nList of Staff who are not in Software");
- NotSpecification NotDepartment = newNotSpecification(newEmployeeDepartmentSpecification("Software"));
- List < Employee > NotSoftwareEmployees = GetEmployee.GetEmployeeBy(NotDepartment, employees);
- foreach(var employee in NotSoftwareEmployees) {
- Console.WriteLine(employee.FirstName);
- }
- Console.ReadKey();
- }
- }
The picture below shows the expected output.
I hope this article has been helpful. Please drop comments and feedback in the Comments section below. Also, follow me on C# Corner to know when my next article comes up.
References
- https://en.wikipedia.org/wiki/Specification_pattern
- https://matt.berther.io/2005/03/25/the-specification-pattern-a-primer/