At run time, based on user behavior and history, I need to perform a sorting operation. In my case, SortByDate/SortByDemand/SortByConsumption
will just return the string, or we can say order by clause(which can be complex).
In most of the forums, I have found Strategy pattern should be used for sorting.
I have attached the image for Strategy pattern here. Util class will call the object of one the three classes i.e SortByDate/SortByDemand/SortByConsumption
So every time a new method of sorting is defined, I need to change the util class and define a new Strategy.
If however I implemented it using factory, the util class just need to call the factory and it will take care of which class to call. So I think I should use factory.
However I have read that strategy is best pattern for such needs. Why is strategy pattern is better here?