Reputation: 1133
I'm trying to work with design patterns and I'm wondering what would be the best thing to do in the following 2 cases:
First case: I want to return and print a Result after calling upon a certain method that executes a list of tests. This list can be sorted, filtered, etc.
The Result should be able to give the results of the tests, the way it was sorted, the filters used, the failures, when it occured, etc etc. All of this can/should be (semi-)optional, for example no filter can be used, or I can not be interested in any of the sorting. Perhaps I only want to know the succeeded tests, perhaps not.
I had 2 design patterns in mind: Factory method or Composite. Factory would require a lot of creator's I'm afraid and Composite would overdo it perhaps. Any suggestions for other patterns, or why one would be preferable above the other?
Second case: The above mentioned sorter. A lot of different sorters can exist (alphabetically, first failure, etc etc) and I'm at a loss of how to organize them. I was thinking of just declaring a lot of static Sorters in some kind of TestSorterCollection class, but that is ugly. However, not sure what design pattern is better.
Upvotes: 0
Views: 1194
Reputation: 17066
First off, this looks to me like a candidate for the MVC architectural pattern.
Regarding design patterns, the sorting algorithms (controller logic) can be implemented as a set of Strategies. Then, passing one or more test result objects to a strategy returns a view which will likely be a subset of those test results.
The view could be implemented any number of ways depending on your needs. It could be as simple as an Iterator that filters out certain test results, or iterates in an order other than the default. It could be a Flyweight if you expect to have a very large number of views for the same test results. It could be a Decorator if you want different kinds of views with different functionalities.
As you can see, there are many possibilities. The key would seem to be keeping the test results, logic, and logic results separated enough that they can vary independently (MVC).
Upvotes: 1