Codebase I am working in has several specialized sorting classes I want to reuse to support sorting in an api. For instance, a class called SortByHeight takes a list and sorts it in descending order, implementing custom logic before sorting.
I want clients to specify what direction they want to sort in, ascending or descending. The problem is that the SortBy* classes sort items in different orders by default, because that behavior makes sense in the context of the application. So the logic I am looking for is something like
- Process and sort items with SortBy* class
- Get the default sort order of the class
- If it is different than the order specified in the api, reverse the sorted list
My current solution is store pairs of a SortBy* class, along with it's default sort direction in a static Map field of a different class.
Would extending the interface all SortBy* classes implement, by adding a getDirection
abstract method be a better solution? This would potentially force SortBy* classes to specify the direction they sort in.
Aucun commentaire:
Enregistrer un commentaire