views:

40

answers:

2

I have next code:

PhotoFactory factory = PhotoFactory.getFactory (PhotoResource.PICASA);
PhotoSession session = factory.openSession (login, password);
PhotoAlbum album = factory.createAlbum ();
Photo photo = factory.createPhoto ();
album.addPhoto (photo);
if (session.canUpload ()) {
   session.uploadAlbum (album);
}
session.close ();

I'm not sure that I've chosen correct name. It's not so important but I'm just curious what had you chosen in my case. Another version is manager:

PhotoManager manager = PhotoManager.getManager (PhotoResource.PICASA);
PhotoSession session = manager.openSession (login, password);
PhotoAlbum album = manager.createAlbum ();
Photo photo = manager.createPhoto ();
album.addPhoto (photo);
if (session.canUpload ()) {
   session.uploadAlbum (album);
}
session.close ();

UPD: I've just found next example at hibernate javadocs:

 Session sess = factory.openSession();
 Transaction tx;
 try {
     tx = sess.beginTransaction();
     //do some work
     ...
     tx.commit();
 }

Is that a naming mistake?

+1  A: 

At a very high level, I'd call it a Factory if it's only responsible for creating instances of classes; and a Manager if it needs to oversee the ongoing existence of objects, and how they relate to other objects, etc.

In the code snippets you've posted you're only creating objects and thus, in my opinion, Factory is an appropriate name. Though you should bear in mind what the conceptual responsibilities of the class are and whether they might expand in future.

That said, I would classically expect a factory to not have to worry about creating sessions itself but rather have sessions passed into their createFoo calls as required, so there's definitely some fudge factor as things are set up. I think personally I would have some other abstract entity responsible for creating sessions, and then pass these into the PhotoFactory.

Andrzej Doyle
A: 

I would choose the second solution (manager), since objects ending with 'Factory' are generally considered implementations of the factory pattern, which essentially creates new instances of objects. This is not the case in your example, since it manages a session for a particular service.

What's nice in this example, is that in fact your first static method call getManager is in fact a factory, since it creates instances of Manager objects.

Martin Sturm