aspose file tools*
The moose likes Java in General and the fly likes patterns for filtering data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "patterns for filtering data" Watch "patterns for filtering data" New topic
Author

patterns for filtering data

Tim Lovern
Greenhorn

Joined: Dec 11, 2002
Posts: 11
Are there any published patterns for data filtering?
What I need is a way to provide a generic filtering mechanism that can be applied to Value Objects.
A user could fill in a table on a form with selection criteria for selecting sales orders, for example.
In the simplest case, this information could be used to generate a query. However, because of the complexity of our environment, the simplest case is rare.
What would happen, is that some of the criteria entered can be used to generate the query. Other aspects would need to be applied to the returned data Value Objects to filter the data. (Value Objects VO's are used to isolate the business layer from the database layer)
one thought is a filter interface with some sort of factory object supplying to needed filters to the application.
The underlying goal, is to have a generic way of doing this so that we don't have to re-invent the wheel for each application that requires the ability to do filtering of data. We also want to make sure that whatever we doesn't require the users to know the underlying structure of the data either.
Hope this made sense, and if so any ideas???
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
This is a message I posted in the UML forum some times ago. Looks like a Decorator:
Let's say you want to filter a Vector of Toto objects on some criteria.
You have FilterA which returns a filtered Vector of Toto objects matching the A criteria.
You have FilterB which returns a filtered Vector of Toto objects matching the B criteria.
You have FilterC which returns a filtered Vector of Toto objects matching the C criteria.

All of these classes are inherited from a common abstract class Filter.


Now you can combine Filter like you want:
You want a sort according to the A and B criteria. Alright let's do it:


You want a sort according to the A and C criteria. Alright let's do it:


You want a sort according to the A, B and C criteria. Alright let's do it:


Now if we did it with overloading you would have had a lot of classes:
class FilterAB,
class FilterAC,
class FilterABC,
class FilterBC,
along the FilterA, FilterB, Filter C. So 4 more classes. And this is a simple example and we were quite lucky that FilterAB does the same sort as FilterBA. Do the exercise with the java.io package!
Moreover, for the client, it does not matter what kind of concrete filter he is using. All he has to know is that he has a Filter, and that he can invoke sort on it.
Hope this helps.
W.
[ December 12, 2002: Message edited by: Wilfried LAURENT ]
Tim Lovern
Greenhorn

Joined: Dec 11, 2002
Posts: 11
Very helpful, thanks! I'll need to chew on this a while and apply it to my situation. At a glance, looks like the concept is what I need.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: patterns for filtering data