Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Different Packages based on input

 
Rudra Kumar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,
I have an application which needs to use two different sets of packages, based on the version of the input data. Both packages have files with same names and same function names, only the code inside the functions are different. Each package is required for parsing one version of input data.
How do I do this?
Thanks
Rudra
 
Sayed Ibrahim Hashimi
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure how to exactly do this but it sounds like you are going to have to use the java.lang.reflect package. You should look into that.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can easily avoid the need for reflection (why use it if you don't have to?) by defining an interface that defines the common characteristics that both (or all) of those classes will implement.
Then once you decide which class you need to instantiate based upon your run-time criteria, you can use Class.forName().newInstance() to create the instance and assign it to a variable declared as the interface.
The code referencing this variable won't care which class it's actually dealing with since they will all obey the contract of the interface.
hth,
bear
 
Rudra Kumar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,
Thanks for the reply.
Both the packages I am referring to, have more than 40 classes in each of them & they are generated by an automated tool based on the format of the user data.
The latest version of data has more functionality, hence more classes are there with more functions than the old classes. So, these packages have some classes in common with some functions inside them common(not the whole class is common).
How do i go about dealing with this?
Thanks
Rudra.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hard to tell without knowing more about the project, but you should probably take a look at the different creational Design Patterns (at http://c2.com/cgi/wiki?CreationalPatterns for example).
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rudra, regardless of the scale, the general solution to the problem of having multiple classes represent the same set of functionality is through the uses of interfaces.
Taking Ilja's advice on researching creation patterns would be a great thing for you to do as you might find descriptions of solutions that match the needs of your project.
hth,
bear
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic