Skip to content

Latest commit

 

History

History
40 lines (24 loc) · 1.55 KB

File metadata and controls

40 lines (24 loc) · 1.55 KB

Intend

Implement boolean logic to check whether objects meet certain requirements.

Use this in the following cases :

  • You want to test objects by multiple criteria using boolean logic.
  • You need to be able to combine the criteria.
  • You do not have access to the implementation of the objects you want to test.

Note : In these situations, you cannot implement the criteria by hand because of the combination factor that would create an explosion of classes.

How it's done

UML

Participants

  • ISpecification : Templated Interface that declares the isSatisfiedBy(T) method.
  • CompositeSpecification : Composite nodes for the AND, OR and NOT operators.
  • ConcreteSpecification : Leaf elements for the various kind of tests that can be composed.

Note : UML class diagram taken from here

Pros & cons

Pros

  • Opened/Closed principle: It is easy to introduce new conditions and tests without duplicating code.
  • Very flexible : Possibility of combining criterias to perform various tests without many specialized classes.
  • Logic : Supports logical operations, which produces easy-to-read code.

Cons

  • Complex : Must invest in complex framework.

Notes