1

In My search page requirement , I need to perform following operation.

  1. Text of user performed search
    (example 20k+ mobile phones + samsung)

  2. Ordering of search refinements
    (for a particular user : refinement is in order of 'price,company,delivery' and for some other it can be 'delivery,price,company'. This class will handle this )

  3. Relaxation of some parameters when results are less.
    ( If results are less by some logic (say change slightly in price range) will be applied. to increase no. of results)

  4. Search Results caching
    (caching search results)

for these operation i created a separate class. i want to know is it correct to create class for this or create a function in some common search function class say "seacrhUtility" should be sufficient.

2 Answers2

2

Since one of the tags of your questions is OOP and one of the SOLID principles of OOP is the Single responsibility principle it might be a good idea to try to follow SRP.

The key point from SRP is that a "responsibility" is defined as "a reason to change".

From what you describe the four points sound like they could all change independently so you could have a class implementing each.

Depending on the exact details you might want to do the actual searching and ordering in just one class since this two probably can be considered just one "responsibility".

The relaxation of the parameters sounds like something that is not directly related to the search operation and that could change and evolve into something quite complex. That should probably be designed as a separate class, perhaps even as a Strategy class.

Now caching should definitely be implemented as a separate class as it is a totally different responsibility. Consider that you may have to change the way you cache or where you cache results and you don't want to have to touch the search algorithm for that.

On the other side, if your application is relatively simple, and will not need to change and if you can implement all the four points in four functions that are less than 4-5 lines of code each - you probably should. Also if the rest of your project is built using only functions or using monolithic objects, it might be better to stick to that approach. Use common sense.

Iulian Margarintescu
  • 2,656
  • 21
  • 22
1

Using a class for this is cool too, But personally I would just use a function, all the needings you have could be passed as a few function parameters and the function will return you the final selected records after a few static operations, that's all.

I would use classes when I need multiple actions in different times (different parts of my code) be done on my input or when there are lots of codes or functions that are going to be used ...

Night2
  • 1,168
  • 9
  • 18