Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
Type
Object Behavioral
Solution
The Strategy Pattern is one of the less complex patterns defined by the Gang of Four. The Strategy Pattern first identifies the behaviors or algorithms that vary and separate them from the system that stays the same. These behaviors or algorithms are encapsulated in classes that implement a common interface. This enables the developer to program to an interface and not an implementation. The different algorithms are encapsulated in a concrete class (ConcreteStrategy). Each of these objects are referenced by classes (Context) through the common interface (Strategy).
Class Diagram Example
Java Sample Code
EmailService.java
package strategy;
public interface EmailService {
public void send(String[] recipients, String subject, String message, String from);
}
GeneralEmailServiceImpl.java
package strategy;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class GeneralEmailServiceImpl implements EmailService {
private String smtpHostName;
private String smtpPort;
private String smtpAuthUser;
private String smtpAuthPwd;
public String getSmtpHostName() {
return smtpHostName;
}
public String getSmtpPort() {
return smtpPort;
}
public String getSmtpAuthUser() {
return smtpAuthUser;
}
public String getSmtpAuthPwd() {
return smtpAuthPwd;
}
public GeneralEmailServiceImpl(String smtpHostName, String smtpPort, String smtpAuthUser, String smtpAuthPwd){
this.smtpHostName = smtpHostName;
this.smtpPort = smtpPort;
this.smtpAuthUser = smtpAuthUser;
this.smtpAuthPwd = smtpAuthPwd;
}
public void send(String[] recipients, String subject, String message, String from) {
boolean debug = false;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", getSmtpHostName());
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", getSmtpPort());
props.setProperty("mail.smtp.quitwait", "false");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(debug);
MimeMessage msg = new MimeMessage(session);
try {
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
msg.setSubject(subject);
msg.setContent(message, "text/html");
Transport.send(msg);
} catch (MessagingException e) {
e.printStackTrace();
return;
}
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = getSmtpAuthUser();
String password = getSmtpAuthPwd();
return new PasswordAuthentication(username, password);
}
}
}
GoogleEmailServiceImpl.java
package strategy;
import java.security.Security;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class GoogleEmailServiceImpl implements EmailService {
private String smtpHostName;
private String smtpPort;
private String smtpAuthUser;
private String smtpAuthPwd;
public String getSmtpHostName() {
return smtpHostName;
}
public String getSmtpPort() {
return smtpPort;
}
public String getSmtpAuthUser() {
return smtpAuthUser;
}
public String getSmtpAuthPwd() {
return smtpAuthPwd;
}
public GoogleEmailServiceImpl(String smtpHostName, String smtpPort, String smtpAuthUser, String smtpAuthPwd){
this.smtpHostName = smtpHostName;
this.smtpPort = smtpPort;
this.smtpAuthUser = smtpAuthUser;
this.smtpAuthPwd = smtpAuthPwd;
}
public void send(String[] recipients, String subject, String message, String from) {
boolean debug = false;
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", getSmtpHostName());
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", getSmtpPort());
props.setProperty("mail.smtp.quitwait", "false");
props.put("mail.smtp.socketFactory.port", getSmtpPort());
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(debug);
MimeMessage msg = new MimeMessage(session);
try {
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
msg.setSubject(subject);
msg.setContent(message, "text/html");
Transport.send(msg);
} catch (MessagingException e) {
e.printStackTrace();
}
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = getSmtpAuthUser();
String password = getSmtpAuthPwd();
return new PasswordAuthentication(username, password);
}
}
}
EmailServiceSelectorTest.java
package strategy;
public class EmailServiceSelectorTest {
public static void main(String[] args) {
String smtpHostName = "";
String smtpPort = "";
String smtpAuthUser = "";
String smtpAuthPwd = "";
String[] recipients = {""};
String subject = "";
String message = "";
String from = "";
GoogleEmailServiceImpl googleEmailServiceImpl = new GoogleEmailServiceImpl(smtpHostName, smtpPort, smtpAuthUser, smtpAuthPwd);
GeneralEmailServiceImpl generalEmailServiceImpl = new GeneralEmailServiceImpl(smtpHostName, smtpPort, smtpAuthUser, smtpAuthPwd);
googleEmailServiceImpl.send(recipients, subject, message, from);
generalEmailServiceImpl.send(recipients, subject, message, from);
}
}
you may download the source code.
No comments:
Post a Comment