开发者

Creating method filters

In my code I have a List<Person>. Attributes to the objects in this list may include something along the lines of:

  • ID
  • First Name
  • Last Name

In a part of my application, I will be allowing the user to search for a specific person by using any combination of those three values. At the moment, I have a switch sta开发者_如何学JAVAtement simply checking which fields are filled out, and calling the method designated for that combination of values.

i.e.:

switch typeOfSearch    
if 0, lookById()    
if 1, lookByIdAndName()  
if 2, lookByFirstName()

and so on. There are actually 7 different types.

This makes me have one method for each statement. Is this a 'good' way to do this? Is there a way that I should use a parameter or some sort of 'filter'? It may not make a difference, but I'm coding this in Java.


You can do something more elgant with maps and interfaces. Try this for example,

interface LookUp{
    lookUpBy(HttpRequest req);
}

Map<Integer, LookUp> map = new HashMap<Integer, LookUp>();

map.put(0, new LookUpById());
map.put(1, new LookUpByIdAndName());

...

in your controller then you can do

int type = Integer.parseInt(request.getParameter(type));
Person person = map.get(type).lookUpBy(request);

This way you can quickly look up the method with a map. Of course you can also use a long switch but I feel this is more manageable.


If good means "the language does it for me", no.

If good means 'readable', I would define in Person a method match() that returns true if the object matches your search criteria. Also, probably is a good way to create a method Criteria where you can encapsulate the criteria of search (which fields are you looking for and which value) and pass it to match(Criteria criteria).


This way of doing quickly becomes unmanageable, since the number of combinations quickly becomes huge. Create a PersonFilter class having all the possible query parameters, and visit each person of the list :

private class PersonFilter {
    private String id;
    private String firstName;
    private String lastName;

    // constructor omitted

    public boolean accept(Person p) {
        if (this.id != null && !this.id.equals(p.getId()) {
            return false;
        }
        if (this.firstName != null && !this.firstName.equals(p.getFirstName()) {
            return false;
        }
        if (this.lastName != null && !this.lastName.equals(p.getLastName()) {
            return false;
        }

        return true;
    }
}

The filtering is now implemented by

public List<Person> filter(List<Person> list, PersonFilter filter) {
    List<Person> result = new ArrayList<Person>();
    for (Person p : list) {
        if (filter.accept(p) {
            result.add(p);
        }
    }
    return result;
}


At some point you should take a look at something like Lucene which will give you the best scalability, manageability and performance for this type of searching. Not knowing the amount of data your dealing with I only recommend this for a longer term solution with a larger set of objects to search with. It's an amazing tool!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜