When implementing some custom directives in Spray to handle pagination and sorting, it occured to me that the handling of the pagination and sorting might be applied nicely to a Chain of Responsibility design pattern as this would allow me to encapsulate different operations and allow them to be used together or in isolation, or with other, newer operations.
So I put together a prototype of the code—without the actual Spray cruft. The Spray code handles extracting the URL parameters and results in two case classes; Sort and Paginate which holds these parameters. This code is then the next stage; applying those parameters to a collection. This code has the obvious caveat that the collection is held in memory. Still, for a nominal number of items and when you need to separate the pagination and sorting; it’s a clean and elegant solution.
I’m using an abstract class to represent my parent command as that allows me to handle the successor matching in one place, thus making the code DRY. However, the calls to the superclass handleData could be refactored out to make it a little cleaner to implement a command.