Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need to create only one object

 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

Please have a look at the following set of codes

MyPhoneBookMain.java



MainView.java





EditButtonOptions





EditBean.java



DatabaseHandler.java




DatabaseContactable.java




When I run the program and click the "go" button, this is what I get

Connection Created
Connection Created
Connection Closed


As you can see here, it is creating 2 DatabaseHandler objects. MainView.java is creating one object and EditButtonOptions.java created another object. This is a JDBC application, so that means this is definitely creating 2 connections, and only closing one when the program exited, which is definitely not good. So, I need to create only one object, but need to use that in both MainView.java and EditButtonOptions.java files. What I tried is making the constructor of DatabaseHandler.java file "private" but you know, then I lose the access ( I found a way to access private constructors in http://www.java-forums.org/new-java/12221-accessing-private-constructor.html, but since the author says it is a HACKING method, I don't think it is a professional way). How can I create only one object, and use it in any place without any issue? Please help..
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Several ways you can do this:-

a) You could have MainView create a DatabaseHandler, give the reference to it to EditBean and EditBean can give it to EditButtonOptions.
b) You could use a singleton pattern
c) You can use dependency injection pattern. Here a factory creates all objects, and when object A and B needs C, the factory creates instance of A, B and C and gives the instance of C to A and B

 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh A Lalwani wrote:Several ways you can do this:-

a) You could have MainView create a DatabaseHandler, give the reference to it to EditBean and EditBean can give it to EditButtonOptions.
b) You could use a singleton pattern
c) You can use dependency injection pattern. Here a factory creates all objects, and when object A and B needs C, the factory creates instance of A, B and C and gives the instance of C to A and B



Thanks a lot for the reply Jayesh. I will check this tomorrow and let you know the result
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the delay Jayesh. I had some issues with my computer.

I used the first method you have mentioned. It worked!! Thanks a lot for helping me, I really appreciate it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic