aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Need to create only one object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Need to create only one object" Watch "Need to create only one object" New topic
Author

Need to create only one object

Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 497

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..


Are you better than me? Then please show me my mistakes..
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2058
    
  22

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

Joined: Oct 07, 2010
Posts: 497

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

Joined: Oct 07, 2010
Posts: 497

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
 
 
subject: Need to create only one object
 
Similar Threads
JScrollpane problem
Action Listener for Enter Key pressing on CheckBox
How to set paddings?
The "Next" is not working
How to size jsplitpane?