• 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

Elegant way for creating objects depending on information from DB

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

i am writing a small program which shall connect to various webservers. Every website has a slightly different way of taking parameters, encryption etc. That why i have a class called AbstractConnection and various subclasses implementing methods like encryption.
I have to connect to websites depending on an information i get from the database. Let's say, the database says one website has not been read out for a long time. I get the name of the website. Now i'm doing as following:


I do not think that this is very elegant, since i have to save the information of which connection has which id in the database and another time in the code.
Do any of you guys knows a more elegant way to solve this problem?

Thank you!
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not put the details of the connection into the database too?
Can you use the ids as indices for a Connection[] or as keys in a Map?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You say you have an AbstractConnection class, but in the Sample code you use Connection?
Anyways you might want to look at the Factory Pattern, might be helpful.

Otherwise you can have a Map<Integer,AbstractConnection> and then fill the map with the required instances of different types of connections, you can then do a map.get(id). But the downfall here is that you would have to unnecessarily create instances of other connection types while you would want to use only one.
 
M Bryan
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, this is problem logic which is implemented in several subclasses. I have to implement different techniques of encryption.
What i could do is save the name of the class in the database and do something like this:



Is this good style?
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks for me like a good example of where Factory Pattern can be used.

I dont know if using reflection is a good approach because sometime in future when you want to add a new connection type then you would have to change the code which was initializing the "className" variable.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

M Bryan wrote:No, this is problem logic which is implemented in several subclasses. I have to implement different techniques of encryption.
What i could do is save the name of the class in the database and do something like this:



Is this good style?


Moreover there's more to it then just getConstructor.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.

What you are thinking of isThat is good style, but only works if you have an accessible no-arguments constructor.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:No.

What you are thinking of isThat is good style, but only works if you have an accessible no-arguments constructor.



But other than that, just to know the name of the class, we would need to have the if..elseif..else and that is equivalent to the code OP posted, just that the instance creation is done in a different way.
 
M Bryan
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just have read the wikipedia article and in the java example, there is a class and a subclass. in the upper class the method makeRoom(); is called and depending on the instanceation of the object, is it possible to call the method in the subclass.
But this does not solve my problem, because it does not show the code where it is decided which constructor shall be called.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

M Bryan wrote:I just have read the wikipedia article and in the java example, there is a class and a subclass. in the upper class the method makeRoom(); is called and depending on the instanceation of the object, is it possible to call the method in the subclass.
But this does not solve my problem, because it does not show the code where it is decided which constructor shall be called.


So it goes like this:
You have a WebsiteConnector interface which is like:

so your News website connector would be


and so on for other websites.
Once you have this then in your factory you can:


So how would you use these in your code:


If you see above your connectToSomeWebsite does not even care to what kind of website its connecting, you get the condition from the database and then pass it on to the factory. Let the factory take the trouble of creating the right instance for you. You then make use of the instance in your connectToSomeWebsite method. This way in future if you need to connect to some other website, all you have to do is to add a new implementation of WebsiteConnector and then update the factory to return this new instance and thats it, your code works as it was working before the new website was added.

Note: Now if you say that you cannot edit the NewsConnection or SportsConnection classes or add a new interface then the above doesn't make sense. OR if you say you dont know how these classes are instantiated then why would you want a better way to create these instances when you dont know where this instantiation is happening?
 
M Bryan
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your example.
The idea to make an extgra class for creating these object was a good idea, because now i have just one place where i have to change the code when i am adding a new connection type. Before i would have done the code you have written in the getConnector(int someID) method every time i needed a connection object.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

M Bryan wrote:Thank you for your example.
The idea to make an extgra class for creating these object was a good idea, because now i have just one place where i have to change the code when i am adding a new connection type. Before i would have done the code you have written in the getConnector(int someID) method every time i needed a connection object.


Exactly your code which just wants to connect to some website shouldn't be really concerned about what type of website it is and how its obtained (separate use from construction). Also your code which connects to the website shouldn't be concerned/affected when in future you want to support some XYZ Website type.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally I have an XML file that - for each interface (not abstract class) needed - associates a descriptive name with the name of the class plus whatever initialization parameters it needs and some text description to remind myself what is going on.



XML because adding new parameters or whatever a new class needs does not interfere with previously defined ones.

Bill
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohamed Sanaulla wrote:. . . just to know the name of the class, . . . just that the instance creation is done in a different way.

Yes, but the previous post had className in, so the names of the class was already available.
Another problem is that newInstance() can throw several checked Exceptions.
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic