对于我的情况,哪个名称更正确:经理或工厂(或其他名称)?

发布于 2024-08-12 19:28:55 字数 1001 浏览 5 评论 0原文

我有下一个代码:

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 ();

我不确定我是否选择了正确的名称。这并不重要,但我只是好奇你在我的例子中选择了什么。另一个版本是经理:

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:我刚刚在 hibernate javadocs 中找到了下一个示例:

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

这是命名错误吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆光下的微笑 2024-08-19 19:28:55

在非常高的层面上,如果它只负责创建类的实例,我会将其称为工厂;和一个 Manager,如果它需要监督对象的持续存在,以及它们与其他对象的关系等。

在您发布的代码片段中,您只是创建对象,因此,在我看来,Factory 是一个合适的名称。但您应该记住该类的概念职责是什么以及它们将来是否可能扩展。

也就是说,我通常希望工厂不必担心创建会话本身,而是根据需要将会话传递到其 createFoo 调用中,因此在设置过程中肯定存在一些模糊因素。我个人认为我会拥有一些其他抽象实体负责创建会话,然后将它们传递到 PhotoFactory 中。

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.

不知在何时 2024-08-19 19:28:55

我会选择第二个解决方案(管理器),因为以“Factory”结尾的对象通常被认为是工厂模式的实现,它本质上创建了对象的新实例。您的示例中并非如此,因为它管理特定服务的会话。

这个例子的好处在于,实际上您的第一个静态方法调用 getManager 实际上是一个工厂,因为它创建了 Manager 对象的实例。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文