Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Combination of Factory and Singleton ?

 
Daniel Breitner
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roel De Nijs
Sheriff
Posts: 10374
132
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Daniel Breitner
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh - thats cool

I think dao factory is best when it comes to switching from database to datafile ...
 
Carlos Morillo
Ranch Hand
Posts: 221
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roel De Nijs
Sheriff
Posts: 10374
132
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 221
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 221
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, Carlos!

Oh, sorry for that. It's just that this thread was started by Daniel... sorry for the mistake, champion!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic