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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply 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: 485

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: 1272
    
    7

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: 485

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: 485

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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Need to create only one object
 
Similar Threads
JScrollpane problem
How to size jsplitpane?
Action Listener for Enter Key pressing on CheckBox
How to set paddings?
The "Next" is not working