Two Laptop Bag*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Combination of Factory and Singleton ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Combination of Factory and Singleton ?" Watch "Combination of Factory and Singleton ?" New topic
Author

Combination of Factory and Singleton ?

Daniel Breitner
Ranch Hand

Joined: Nov 13, 2008
Posts: 70
Hello everybody,

I am just trying to get on with my Data Access Layer on SCJD.

After talking about the synchronisation stuff here in the forum yesterday I thought it might be a good idea to have the Data Acces Class (the one working with the RandomAccessFile) as a Singleton. So all Threads would get the same DAO which then has the same RAF, Lock, etc.

One question to that is:

Is it ok to create a mixture between Factory and Singleton pattern ?

I would like to have the benefit of the Factory, that the calling class doesn´t care about the implementation type of the interface it gets. But I would also like to use a Singleton to support synchronisation.

So what I want to have is a Factory that returns a Singleton.


Visit me at http://liferay-blogging.blogspot.com
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Hey, Daniel!

Is it ok to create a mixture between Factory and Singleton pattern ?


You know, funny thing, I did exactly what you are saying. I created a static factory that returns the Data object. Here's its code:



DB is the interface that extends the interface provided by Sun. Looking at it today, I think that maybe it would also be interesting to have these 2 methods, but also returning the interface provided by Sun.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

Hi Daniel,

I used the DAO Factory pattern, maybe a bit overhead for this assignment, I know. But it allows you to easy extend or switch to another database system (instead of a flat file on the file system). And in my factory I am (like in Roberto's code) also calling Data.getInstance().

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Daniel Breitner
Ranch Hand

Joined: Nov 13, 2008
Posts: 70
oh - thats cool

I think dao factory is best when it comes to switching from database to datafile ...
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hi Roberto, Roel,


Do you use the same Factory object for your Local and Remote implementation of your business layer that exposes SearchRooms() and BookRoom()?


Thanks,


Carlos.


SCSA, OCA, SCJP 5.0, SCJD http://www.linkedin.com/in/carlosamorillo
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5139
    
  12

Hi Carlos,

If you mean if i had another DAOFactory for local or business implementation, I didn't have. In both implementation I use something like DAOFactory.getDAO(). If this was not your question, I'm sorry

Kind regards,
Roel
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Howdy, Carlos!

Do you use the same Factory object for your Local and Remote implementation of your business layer that exposes SearchRooms() and BookRoom()?


Oh, certainly, champion. This is what makes flexible the whole thing!
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hi Roberto, Roel,


That's what I thought.

I am just asking because I am about to start the RMI part of my assignment.
and because of Andrew Monkhouse example with Denny's DVDs
where he has a DvDDatabaseFactory interface extending Remote
and then he has DvdDatabaseFactoryImpl extending UnicastRemoteObject and implementing DvdDatabaseFactory

and then he has a DvdDatabaseRemote interface extending Remote and DBClient and
DvdDatabaseImpl extending UnicastRemoteObject and implementing DvdDatabaseRemote.
The DvdDatabaseImpl() constructor using composition has a private reference to his database object (kind of
the equivalent of my Data singleton) that implements his DBClient interface.

I am just trying to get things clear in my mind before I start coding my network server that will export only through
RMI my SearchRooms() and BookRoom() methods of my BusinessService object using a thin client approach.

I already kind of have working my Local BusinessService object connected to my Data object and an early prototype
of my GUI that's going to be how my assignment will work in "alone" mode.


Thanks,


Carlos.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Howdy, Daniel!

You know, there's another discussion going on here, and I think it might be helpful to you. Please take a look at that.

The approach taken by Roel and the approach addressed by me in this thread are pretty similar, with few variations. When using something similar to the approach addressed by me, it isn't really necessary to create a factory just for the Data object. This object and the business component are prepared according to the way the application is started, and are injected in the main window's class. So the business component always has a reference to the Data object, and thus it doesn't have to be retrieved throughout the application, as the business component already has a reference to it.
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hi John Lennon,

Howdy, Daniel!

You know, there's another discussion going on here, and I think it might be helpful to you. Please take a look at that.

I guess and I hope you meant Carlos instead.


Thanks,

Carlos.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Howdy, Carlos!

Oh, sorry for that. It's just that this thread was started by Daniel... sorry for the mistake, champion!
 
 
subject: Combination of Factory and Singleton ?
 
Similar Threads
RMI: factory design pattern
If singletons are bad, how does a factory help?
B&S Design question
NX: Physical locking
Is there any benefit in the RMI Factory solution - when you have a lock cookie in your interface?