• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pattern for applying algorithm on unrelated properties of unrelated classes?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I've been thinking for days how to solve the problem below, but I need some support of you guys.
I'm sure there exists some technique or a pattern to solve easily my problem.
Before continuing, you should know that for my case it's fobidden to modify the datamodel (otherwise my problem could have been solved by just inheritance and polymorphism.

So, in our model there are several classes in which some properties exist that need to be used for applying a complicated algorithm.
We receive from our backend a unsorted list of objects of a certain class, and the result of that algorithm sorts the objects in a certain way and change some other properties of those classes.

The problem is, those classes are completely unrelated and the property names of those classes are completely different.
The algorithm that has to be applied is exactly the same.

As I don't want to duplicate the algorithm for handling each type of class, I'm looking for the right programming technique to apply.

Do you have any suggestions or hints please?

Thank you sooo much!


simplified Example of what I need to do -
imagine that the same algorithm

For the class User:
- sorts the elements of users, based upon an algorithm applied on the birthDate and marriageDate
- thereafter calculates the expected children, based upon an algorithm applied on the birthDate and marriageDate

For the class Company:
- sorts the elements of planets, based upon an algorithm applied on the startupDate and introductionToStockExchange
- thereafter calculates the expected revue, based upon an algorith applied on the startupDate and the introductionToStockExchange
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the first one, write suitable Comparator objects for each class and then just call the List.sort(Comparator) method for each of the lists. Presumably the two Comparator objects do the same thing, so they can delegate their logic to some static method in your MainClass class.

For the second one, you need some kind of a method which accepts two Date objects and returns an int value. Since it's the same algorithm for both Person and Company (or did you not say that?) then likewise it can delegate to a static method in MainClass.

I don't particularly see a downside to writing "sort list; for each element do something" twice, and I wouldn't turn myself inside out to avoid doing that.
 
Esti Vanderkelen
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your suggestions.
I however mis-explained myself with my example.
The algorithm has to be applied on a dozen of classes, not just on two classes.
Duplicating or x-plicating the code is thus not an option.

I started thinking to use reflexion to pass the name of the class property on which the algorithm should be applied.
I think this is my only option, if I want to keep the code re-usable?
Or any other hints, suggestions?

I can not imagine that i'm the first with this problem.
I'm sure a pattern exist to solve it, but I can not find it.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The pattern that comes to my mind is the Adapter Pattern. Your algorithm code will deal with only one interface. Then you will write an implementation of that interface for each different class that the algorithm needs to work with. Each adapter will map the method in the class being adapted to the methods that the algorithm works with. It could potentially be a lot of work if you have dozens of different classes that your algorithm needs to work with but that's probably the way I'd go if it were me.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems like you could also use lambda expressions/functional interfaces and method references if you were using Java 8. Are all your data classes structured the same way with two dates and an int? Are those the fields that the algorithm has to deal with all the time?
 
Where does a nanny get ground to air missles? Protect this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic