aspose file tools*
The moose likes Java in General and the fly likes Elegant way for creating objects depending on information from DB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Elegant way for creating objects depending on information from DB" Watch "Elegant way for creating objects depending on information from DB" New topic
Author

Elegant way for creating objects depending on information from DB

M Bryan
Ranch Hand

Joined: Jun 15, 2011
Posts: 64
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!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
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?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3070
    
  33

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.


Mohamed Sanaulla | My Blog
M Bryan
Ranch Hand

Joined: Jun 15, 2011
Posts: 64
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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3070
    
  33

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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3070
    
  33

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
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
No.

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

Joined: Sep 08, 2007
Posts: 3070
    
  33

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

Joined: Jun 15, 2011
Posts: 64
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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3070
    
  33

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

Joined: Jun 15, 2011
Posts: 64
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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3070
    
  33

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.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12781
    
    5
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
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Elegant way for creating objects depending on information from DB