Pattern of the week: Specification Pattern

January 19, 2012 by · Leave a Comment
Filed under: Architecture, Design, Patterns 

This weeks pattern is the Specification Pattern. We will spend this week reviewing this pattern, how it is used and what its intent is for. For some more in-depth definition you can see here and Here are some useful descriptions, but to appreciate this we will review the intent and possible solutions and applications for this pattern in everyday work.

Here is the intent of the Specification pattern: “ to separate the logic for (e.g.) filtering an entity from the entity itself.”

Specification_UML

 

Example application for this pattern has been to support filtering as a pluggable entity “collection” of specifications.  The implementation details as things mature are apparent with generics and expression tree’s and event the IQueryable<> interfaces against OR/M layers.

The main benefits of this pattern are

1 – Loose coupling of the filter logic from the objects being filtered,

2- Single responsibility: Filtering is essentially a first class citizen and also can exist in a decoupled state for better testing and independent improvement

3 – Composition of specifications allows for reuse and complex nested specifications for easier maintenance.

Pattern of the week: Memento

January 14, 2012 by · Leave a Comment
Filed under: Architecture, Design, Patterns, Programming 

This weeks pattern is the Memento Pattern. We will spend this week reviewing this pattern, how it is used and what its intent is for. For some more in-depth definition you can see  here and here are some useful descriptions, but to appreciate this we will review the intent and possible solutions and applications for this pattern in everyday work.

Here is the intent of the Memento pattern: “Without violating encapsulation, capture and externalize an object’s internal state so that the object can be returned to this state later.  A Magic cookie encapsulating check point capability, promote undo or rollback to full object status.”

 

memento

Example application for this pattern has been to support undo or rollback, other common implementations are a finite state machine.  In my own personal development projects I have used memento pattern to create and audit trail with full play-back of the payloads as an encapsulation exercise.