Is Singleton enough for a EMailUtil or there is any better implementation? [duplicate]

I am developing an application, where I have to send lots of emails for user account creations, validation, invites etc.

I have implemented a static class EMailUtil which has different methods like

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public static void sendInvite(from, to, Subject){...}
public static void sendVerificationCode(from, to, subject, code){...}
</code>
<code>public static void sendInvite(from, to, Subject){...} public static void sendVerificationCode(from, to, subject, code){...} </code>
public static void sendInvite(from, to, Subject){...}
public static void sendVerificationCode(from, to, subject, code){...}

Afterwards, I converted that class to a singleton. I was wondering if there is a way I can improve my approach here. What other pattern I could use here and what is the best approach to implement this utility class?

4

Ok, here’s one suggestion.

First of all, you should get rid of repeating parameters (from, to, subject etc) and methods (sendInvite, sendVerificationCode etc).

To achieve that you could start with defining an EMail-class holding the parameters as attributes. This is quite a standard approach when facing repeating parameters. Now all invitations and verifications and such would be just instances of EMail and all EMails could be send using only one send-method. Thus you would also get rid of the repeating methods.

EMail could be subclassed or it could provide factory methods for creating all kinds of messages. So either

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>EMail verificationMessage = new VerificationEMail(recipient, verificationCode);
</code>
<code>EMail verificationMessage = new VerificationEMail(recipient, verificationCode); </code>
EMail verificationMessage = new VerificationEMail(recipient, verificationCode);

or

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>EMail verificationMessage = EMail.createVerificationMessage(recipient, verificationCode);
</code>
<code>EMail verificationMessage = EMail.createVerificationMessage(recipient, verificationCode); </code>
EMail verificationMessage = EMail.createVerificationMessage(recipient, verificationCode);

I’d choose the latter approach if it does not lead to an overly bloated EMail class.

What goes into from and subject fields of a message should probably be decided in the method that’s creating a message of a certain type. All verification messages should probably have a similar title etc.

After defining the structure of the messages you only need some kind of dispatcher or such to send them:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>EMailDispatcher dispatcher = new EMailDispatcher();
dispatcher.send(someMessage);
</code>
<code>EMailDispatcher dispatcher = new EMailDispatcher(); dispatcher.send(someMessage); </code>
EMailDispatcher dispatcher = new EMailDispatcher();
dispatcher.send(someMessage);

Dispatcher could have only one method to send messages:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public void send(EMail mail);
</code>
<code>public void send(EMail mail); </code>
public void send(EMail mail);

I’d try to avoid having a static send method like your EMailUtil has. Static methods are hard to replace for unit tests and otherwise. You don’t want to get thousands of actual emails sent every time automated tests run.

Now, if you’re using JavaMail-API, then it already has a Message-class. However it could prove useful to have your own message class to define the contents and types of messages your application needs and have the dispatcher deal with the more technical Java-API.

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Is Singleton enough for a EMailUtil or there is any better implementation? [duplicate]

I am developing an application, where I have to send lots of emails for user account creations, validation, invites etc.

I have implemented a static class EMailUtil which has different methods like

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public static void sendInvite(from, to, Subject){...}
public static void sendVerificationCode(from, to, subject, code){...}
</code>
<code>public static void sendInvite(from, to, Subject){...} public static void sendVerificationCode(from, to, subject, code){...} </code>
public static void sendInvite(from, to, Subject){...}
public static void sendVerificationCode(from, to, subject, code){...}

Afterwards, I converted that class to a singleton. I was wondering if there is a way I can improve my approach here. What other pattern I could use here and what is the best approach to implement this utility class?

4

Ok, here’s one suggestion.

First of all, you should get rid of repeating parameters (from, to, subject etc) and methods (sendInvite, sendVerificationCode etc).

To achieve that you could start with defining an EMail-class holding the parameters as attributes. This is quite a standard approach when facing repeating parameters. Now all invitations and verifications and such would be just instances of EMail and all EMails could be send using only one send-method. Thus you would also get rid of the repeating methods.

EMail could be subclassed or it could provide factory methods for creating all kinds of messages. So either

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>EMail verificationMessage = new VerificationEMail(recipient, verificationCode);
</code>
<code>EMail verificationMessage = new VerificationEMail(recipient, verificationCode); </code>
EMail verificationMessage = new VerificationEMail(recipient, verificationCode);

or

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>EMail verificationMessage = EMail.createVerificationMessage(recipient, verificationCode);
</code>
<code>EMail verificationMessage = EMail.createVerificationMessage(recipient, verificationCode); </code>
EMail verificationMessage = EMail.createVerificationMessage(recipient, verificationCode);

I’d choose the latter approach if it does not lead to an overly bloated EMail class.

What goes into from and subject fields of a message should probably be decided in the method that’s creating a message of a certain type. All verification messages should probably have a similar title etc.

After defining the structure of the messages you only need some kind of dispatcher or such to send them:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>EMailDispatcher dispatcher = new EMailDispatcher();
dispatcher.send(someMessage);
</code>
<code>EMailDispatcher dispatcher = new EMailDispatcher(); dispatcher.send(someMessage); </code>
EMailDispatcher dispatcher = new EMailDispatcher();
dispatcher.send(someMessage);

Dispatcher could have only one method to send messages:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public void send(EMail mail);
</code>
<code>public void send(EMail mail); </code>
public void send(EMail mail);

I’d try to avoid having a static send method like your EMailUtil has. Static methods are hard to replace for unit tests and otherwise. You don’t want to get thousands of actual emails sent every time automated tests run.

Now, if you’re using JavaMail-API, then it already has a Message-class. However it could prove useful to have your own message class to define the contents and types of messages your application needs and have the dispatcher deal with the more technical Java-API.

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật