File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

feedback on this proposed design

 
k Oyedeji
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am building an application which allows users to supply a number of criteria for a search and then be presented with the results. I am considering the following design for the following reasons:
a 'searchResults' class which has a constructor which requires an object which implements a 'criteria' interface the 'searchResults' class would have methods for sorting the results, searching against the results(for a subset)and returning the amount of results (and perhaps paging through the results)
the reason i have seperated the criteria into another class is so that this class can be
1.used in many applications regardless of the type of criteria the application allows the user to search by.
2. search results could be stored by saving the criteria (and not the set of results). The criteria could also have a date field so search results would only return the records that matched the criteria at a particular time in the past.
Also by creating a constructor which requires an object which implements the interface i can ensure a criteria must be created before a searchResult object can.
I'm new to OO design so i would be grateful if you could point out any flaws in my way of thinking or the application design.
Thanks in advance
Kola
 
Frank Carver
Sheriff
Posts: 6920
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general the approach sounds reasonable, but I would be somewhat wary of requiring your criteria to be passed in to a constructor. This prevents reuse of your search object for different requests, prevents it being used as a "bean" and generally constrains your design.
Can you give us an example code snippet of how you envisage using your design? Writing the code which uses a class before the class itself can often be a very powerful design tool.
 
k Oyedeji
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank
Thanks for your reply.
I envisage something along the lines of:
criteria userSearc = new criteria( usersFirstString, usersSecondString);
searchResults usersSearch = new searchResults(criteria);
system.out.print("your search returned" + searchResults.recordCount + "results" );
I'm considering making searchResults implement a collection interface and a method to return an iterator so that the results can be looped through.
What alternative would you suggest to not passing a criteria object to the searchResults constructor? Perhaps i could make searchResults empty until a search method is called which passes some criteria?
Basically i would like to be able to create a class to perform a search and use it for any application which requires a search of some sort, factoring out the parts that will vary from application to application.
Thanks
 
Frank Carver
Sheriff
Posts: 6920
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[I]I envisage something along the lines of:
[/I]
I understand. There are a few things here which might lead to maintainance problems in the future, though.
  • I'm always wary when I see specific clases used in methods like this. Each time your application code makes a call to "new" it is assuming and enforcing which class needs to be constructed, and denying you the ability to use different classes later. Consider passing in pre-constructed objects as parameters, and using interfaces to allow you to use any compliant objects.
  • This may be something you have elided for readability, but it looks as if the context for your search operation is somehow hard-coded into either your "criteria" or "searchResults" classes. This seems unusual to me, my natural tendency would be to allow that to be externally specified (say in a SearchContext class) which you can then run multiple searches against.
  • Also is there any particular reason why you made recordCount a public variable rather than a (much more flexible) method call?


  • Putting these suggestions together; how about something more like:

    Obviously the above is incomplete. I don't know how you plan for your search to work, or where the data to be searched will come from, or what information you might want to get from your criteria to use in your searching, or what types of criterion you might be interested in.
    I hope, though, that it goes a little way to showing one way you can separate your design concerns, and avoid calling new on specific classes inside your code.
    Has this helped at all, or have I missed your point ?
     
    k Oyedeji
    Ranch Hand
    Posts: 96
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Frank
    Thanks for your reply. I will study the code you provided as it looks very much like what i have been trying to acheive.
    To answer your questions:
    "
    This may be something you have elided for readability, but it looks as if the context for your search operation is somehow hard-coded into either your "criteria" or "searchResults" classes. This seems unusual to me, my natural tendency would be to allow that to be externally specified (say in a SearchContext class) which you can then run multiple searches against."
    I'm a bit confused, are you referring to the environment, a class or the calling class
    "
    Also is there any particular reason why you made recordCount a public variable rather than a (much more flexible) method call?"
    I thought about this and being a newbie i wasn't sure which was best. Why is a method call more flexible?
    Thank you
     
    Frank Carver
    Sheriff
    Posts: 6920
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm a bit confused, are you referring to the environment, a class or the calling class
    None of those really. I was talking about whatever it is that you are searching :- a web site? a file system? a database? a zip file? a String ? an array ? something else ?
    You haven't told us anything about what you are searching, so I left a "placeholder" for where you tell the software what to search. I doubt that it could know on its own!
    Why is a method call more flexible?
    The simple answer is because you don't have to precalculate it if it is never going to be used. The more complex answer is that it allows you to write code to "defend" against difficult situations.
    If you notice in the example I gave, I was able to simply delegate the "count" method to the "size" method of the returned Collection. Simple, small, hardly anything which could go wrong.
    If I had used a public variable, I would have had to store it every time a search was performed, make sure that it had a sensible value even if the search hadn't finished, or if the criteria were changed. Complicated, considerably more code, and lots more chance for "bugs".
     
    k Oyedeji
    Ranch Hand
    Posts: 96
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Frank
    Thanks for your explanation of why you would use a method instead. The search will be performed against a database. I'm also wondering if i'm i should make the searchResults class a Collection, perhaps a set as a set has the basic behaviour i would want to implement (for my searchResults) but i just need a small amount of additional functionality like being able to sort, count the records returned etc.
    KOla
     
    Frank Carver
    Sheriff
    Posts: 6920
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Why do you need a "searchResults" class at all? Can you explain what you would lose if you just returned a java.util.List ?
    If you need to be able to sort, then see Collections.sort(List) in the API documentation
     
    k Oyedeji
    Ranch Hand
    Posts: 96
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Frank
    I have just realised this.(i'm still not entirely familiar with the java apis). A list provides all the functionality required so i think thats what i'll use.
    I've decided that i'll have an interface 'criteria' which contains a method search() which returns a list. This should allow any class which implemnts criteria to retreive the search results however it likes as long as it returns the results in an List.
    The class which implements the criteria interface can decide how it wants to accept the user's criteria etc.
    Does this sound like an okay design?
    I cant thank you enough for your patience and the insight you've given me. Thank you.
    Kola
     
    Frank Carver
    Sheriff
    Posts: 6920
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I may be missing your point, but I can't see why the "search" method should belong to the "criteria". I keep imagining that your search criteria is essentially something like a string to look for, or a group of column-name/data-value pairs. To me the "search" operation belongs to the thing being searched,
    not to the thing that specifies what to look for.
    If you do it as you have suggested, how do you expect to tell the "criteria" which database to search?
    I would normally expect some object to have a method like:
    List find(Criteria cc)
    Can you give us a more concrete example of what a "criteria" object would be, and how you would tell it which database to search?
     
    k Oyedeji
    Ranch Hand
    Posts: 96
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Frank Carver:
    I may be missing your point, but I can't see why the "search" method should belong to the "criteria". I keep imagining that your search criteria is essentially something like a string to look for, or a group of column-name/data-value pairs.

    Frank
    You're correct a user will enter a string or a number of strings which will then go off and search the database. As i'm from a web development background i would normally just go of and query the database directly.
    From an OO point of view i'm trying to create a class or set of classes which would allow me to re-use them for multiple applications with the minimum changes, perhaps the only change being the class which goes off and queries the database.
    You are probably right the obejct bieng searched should provide the search operation but how would i create the class so that the amount or type of criteria passed can easily be changed from application?
    Hope i'm making some sense
    Thanks
    ps i've just finished my first read of the design patterns book but now i think i'm even more confused!!
     
    Ilja Preuss
    author
    Sheriff
    Posts: 14112
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by k Oyedeji:
    ... but how would i create the class so that the amount or type of criteria passed can easily be changed from application?
    ...
    ps i've just finished my first read of the design patterns book but now i think i'm even more confused!!

    Then read Strategy and Composite again. Do they give you some ideas?
     
    k Oyedeji
    Ranch Hand
    Posts: 96
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Ilja Preuss:

    Then read Strategy and Composite again. Do they give you some ideas?

    Thanks for the pointer i had a look at them and came up with the following:
    1. The class searchContext defines an interface for an application to perform a search and maintains
    a reference to a searchStratergy.
    2. a searchStratergy (which implements a search interface)is responsible for carrying out the search and returns an arrayList of results.
    This allows client applications to determine how to retreive results by creating a class
    which implements the search interface and the implementation to vary.

    I'm not too sure how the composite pattern would apply here.
    Does this sound okay?
    THanks
    [ December 15, 2002: Message edited by: k Oyedeji ]
     
    Frank Carver
    Sheriff
    Posts: 6920
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm still worried that the problem as described has two "axes" of flexibility - what to search (how to specify the database, or whatever) and what to look for (which text strings, etc.)
    I would really hope that such a fundamental aspect of the problem definition would find its way into the solution model somehow.
    Also, I think you are in danger of heading into "analysis paralysis". I strongly recommend that by now you should really have at least one working "prototype" or "spike solution" which can succesfully process a search for one particular set of criteria on one particular data source. If you choose the simplest criteria and data source you can think of, you should be able to try out how these solutions look and feel in a concrete manner.
    You can't practically expect to jump straight from a vague idea of the problem, to an ideal solution in one go.
     
    Ilja Preuss
    author
    Sheriff
    Posts: 14112
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by k Oyedeji:
    I'm not too sure how the composite pattern would apply here.

    It might make it possible to only have to pass one criteria object, regardless of the number of actual criterias...

    Does this sound okay?

    As Frank suggested: try it!
    And don't forget to post your experiences here...
     
    Ilja Preuss
    author
    Sheriff
    Posts: 14112
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by k Oyedeji:
    I'm not too sure how the composite pattern would apply here.

    It might make it possible to only have to pass one criteria object, regardless of the number of actual criterias...

    Does this sound okay?

    As Frank suggested: try it!
    And don't forget to post your experiences here...
     
    k Oyedeji
    Ranch Hand
    Posts: 96
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Frank Carver:
    I'm still worried that the problem as described has two "axes" of flexibility - what to search (how to specify the database, or whatever) and what to look for (which text strings, etc.)
    I would really hope that such a fundamental aspect of the problem definition would find its way into the solution model somehow.
    Also, I think you are in danger of heading into "analysis paralysis". I strongly recommend that by now you should really have at least one working "prototype" or "spike solution" which can succesfully process a search for one particular set of criteria on one particular data source. If you choose the simplest criteria and data source you can think of, you should be able to try out how these solutions look and feel in a concrete manner.
    You can't practically expect to jump straight from a vague idea of the problem, to an ideal solution in one go.

    Guys
    Thanks for your feedback i will let you know how i get on. I will build a first draft as suggested.
    Thanks
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic