English 中文(简体)
Which name is more correct in my case: manager or factory (or something else)?
原标题:

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?

最佳回答

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.

问题回答

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.





相关问题
Naming: solution, projects, namespaces and assemblies

I m working on naming guidelines for solutions, projects, their default namespaces and assemblies (Visual Studio). Now it looks like that: For example, we have a company named "Company" and a project ...

Good name for the opposite of the "canary" metaphor [closed]

The "canary" is a common metaphor (for example in buffer overflow checks) for detecting mis-behaving operations by setting a flag before and verifying that it is still set after. Is there a common ...

Package name for CurrentDate class

I m about to write a class called CurrentDate and need a solid package name. The obvious answers are com.destroytoday.date or com.destroytoday.time, but I m not sure if I ll have more date-related ...

Naming form fields in a web page

How do you name your field names in a web page without revealing the structure of your database tables?

database, table and column naming conventions [closed]

Do you know how to use the naming conventions in mysql database? I ve downloaded a mysql sample database. Here it is: CREATE DATABASE IF NOT EXISTS classicmodels DEFAULT CHARACTER SET latin1; USE ...

热门标签