为什么JavaMail Transport.send()是一个静态方法? [英] Why is JavaMail Transport.send() a static method?

查看:140
本文介绍了为什么JavaMail Transport.send()是一个静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修改我没写过的使用JavaMail的代码,并且在理解为什么JavaMail API的设计方式有点麻烦。我觉得,如果我理解,我可以做得更好。

I'm revising code I did not write that uses JavaMail, and having a little trouble understanding why the JavaMail API is designed the way it is. I have the feeling that if I understood, I could be doing a better job.

我们打电话:

transport = session.getTransport("smtp");
transport.connect(hostName, port, user, password);

那么为什么Eclipse会警告我:

So why is Eclipse warning me that this:

transport.send(message, message.getAllRecipients());

是对静态方法的调用吗?

is a call to a static method?

如果我无法使用该对象发送消息,为什么我会获取Transport对象并提供特定于它的设置? Transport类如何知道用于发送消息的服务器和其他设置?它工作正常,很难相信。如果我为两个不同的服务器实例化了Transport对象,该怎么办?怎么会知道使用哪一个?

Why am I getting a Transport object and providing settings that are specific to it if I can't use that object to send the message? How does the Transport class even know what server and other settings to use to send the message? It's working fine, which is hard to believe. What if I had instantiated Transport objects for two different servers; how would it know which one to use?

在写这个问题的过程中,我发现我应该真正打电话:

In the course of writing this question, I've discovered that I should really be calling:

transport.sendMessage(message, message.getAllRecipients());

那么静态Transport.send()方法的目的是什么?这只是糟糕的设计,还是有这样的原因?

So what is the purpose of the static Transport.send() method? Is this just poor design, or is there a reason it is this way?

推荐答案

Transport.send ()基本上是一种方便的方法。是的,如果您管理自己的 Transport 实例,请调用 sendMessage()

Transport.send() is basically a convenience method. Yes, if you're managing your own Transport instance, call sendMessage().

我不确定我认为它设计不好,因为经常你不关心管理传输和监控传输的细节。破解 send()方法,看看它为你做了什么。其他命名或放置此方法的方法可能稍微好一点。

I'm not sure I consider it bad design, since frequently you don't care to manage the details of sending and monitoring transport. Crack open the send() method to see what it does for you. Other ways of naming or placing this method could be marginally better.

这篇关于为什么JavaMail Transport.send()是一个静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆