假设有一个Java建造的电子杂志系统,定期地向用户的电子邮件信箱发送电子杂志。用户可以通过网页订阅电子杂志,也可以通过网页停止订阅。当用户开始订阅时,系统发送一个电子邮件表示欢迎,当用户结束订阅时,系统发送一个电子邮件表示欢送。本例子就是这个系统负责发送“欢迎”和“欢送”邮件的模块。
在本例中,产品类就是发送给客户的“欢送”和“欢迎”的邮件。由于本例的特殊性,两个产品可以抽象出一个共同的接口,即邮件信息抽象类。下面是该抽象类的源代码
import java.util.Date; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeUtility; /* * 抽象的邮件信息类 * */ public abstract class AutoMessage { protected String smtpHost; protected String subject=""; protected String body=""; protected String from=""; protected String fromPassword=""; protected String to=""; protected Date senDate=null; public AutoMessage(){} public void send() { Properties properties = new Properties(); properties.put("mail.smtp.host", "smtp.qq.com"); properties.put("mail.sender.username", from); properties.put("mail.sender.password", fromPassword); Session session = Session.getInstance(properties); MimeMessage message = new MimeMessage(session); Transport transport=null; try { // 发件人 // 下面这个是设置发送人的Nick name InternetAddress from = new InternetAddress(MimeUtility.encodeWord("麦芽糖")+" <"+this.from+">"); message.setFrom(from); // 收件人 InternetAddress to = new InternetAddress(this.to); message.setRecipient(Message.RecipientType.TO, to); // 邮件主题 message.setSubject(this.subject); // 邮件内容,也可以使纯文本"text/plain" message.setContent(this.body, "text/html;charset=UTF-8"); //邮件的发送日期 message.setSentDate(this.senDate); // 保存邮件 message.saveChanges(); transport = session.getTransport("smtp"); // smtp验证,就是你用来发邮件的邮箱用户名密码 transport.connect(this.smtpHost, this.from, this.fromPassword); // 发送 transport.sendMessage(message, message.getAllRecipients()); //System.out.println("send success!"); } catch (Exception e) { e.printStackTrace(); }finally { if(transport!=null){ try { transport.close(); } catch (MessagingException e) { e.printStackTrace(); } } } } public String getSmtpHost() { return smtpHost; } public void setSmtpHost(String smtpHost) { this.smtpHost = smtpHost; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public String getFromPassword() { return fromPassword; } public void setFromPassword(String fromPassword) { this.fromPassword = fromPassword; } public String getTo() { return to; } public void setTo(String to) { this.to = to; } public Date getSenDate() { return senDate; } public void setSenDate(Date senDate) { this.senDate = senDate; } }
具体的两个邮件信息,源代码
public class GoodbyMessage extends AutoMessage{ public GoodbyMessage() { System.out.println("Enter goodby message"); } public void sayGoodby() { System.out.println("Goodby."); } } public class WelcomeMessage extends AutoMessage{ public WelcomeMessage() { System.out.println("Enter welcome message"); } public void sayWelcome() { System.out.println("welcome."); } }
由于电子邮件可以划分为发件人地址、收件人地址、主题、内容等部分(或零件)。因此电子邮件的建造过程可以分为几个零件的建造过程,包括buildTo(),buildFrom(),buildSendDate(),buildSubject(),buildBody()等。我们应该看出,邮件是最终产品,而To,from,date,subject以及邮件内容,则是产品的零件,再添加一个导演者角色,就是一个标准的建造者模式了。
这个系统包含客户端(Client),导演角色(Director),抽象建造者(Builder),具体建造者(WelcomeBuilder,GoodbyeBuilder),产品(WelcomeMessage,GoodbyeMessage)等角色。
/* * 导演角色源代码 * */ public class Director { private Builder builder=null; public Director(Builder builder) { this.builder=builder; } public void construct(String smtpHost,String from,String fromPassword,String to) { builder.buildSmtpHost(smtpHost); builder.buildFrom(from); builder.buildFromPassword(fromPassword); builder.buildBody(); builder.buildObject(); builder.buildTo(to); builder.buildDate(); builder.sendMail(); } }
/* * 抽象构建者源代码 * */ import java.util.Date; public abstract class Builder { protected AutoMessage msg; public Builder(){} /* * 发送邮件的主机零件构建方法 * */ public void buildSmtpHost(String smtpHost) { msg.setSmtpHost(smtpHost); } /* * 发件人地址的零件构建方法 * */ public void buildFrom(String from){ msg.setFrom(from); } /* * 发件人密码零件构建方法 * */ public void buildFromPassword(String fromPassword) { msg.setFromPassword(fromPassword); } /* * 收件人地址零件构建方法 * */ public void buildTo(String to){ msg.setTo(to); } /* * 发送时间零件的构建方法 * */ public void buildDate() { msg.setSenDate(new Date()); } /* * 邮件主题零件的构造方法 * */ public abstract void buildObject(); /* * 邮件内容零件的构造方法 * */ public abstract void buildBody(); /* * 产品完成以后,用此方法发送邮件 * */ public void sendMail() { msg.send(); } }
/* * 具体构建者源代码 * */ public class WelcomeBuilder extends Builder{ public WelcomeBuilder() { msg=new WelcomeMessage(); } @Override public void buildObject() { // TODO Auto-generated method stub String object="welcome"; msg.setSubject(object); } @Override public void buildBody() { // TODO Auto-generated method stub String body="welcome"; msg.setBody(body); } } public class GoodbyeBuilder extends Builder { public GoodbyeBuilder() { msg=new GoodbyMessage(); } @Override public void buildObject() { // TODO Auto-generated method stub String object="ByeBye"; msg.setSubject(object); } @Override public void buildBody() { // TODO Auto-generated method stub String body="ByeBye"; msg.setBody(body); } }
public class Client { public static void main(String[] args) { Builder welcomeBuilder=new WelcomeBuilder(); Director director=new Director(welcomeBuilder); //参数依次为,邮件服务器地址,发送邮件的地址,发送邮件的密码,接收邮件的地址 director.construct("smtp.qq.com", "[email protected]", "************", "[email protected]"); } }
时间: 2024-10-02 16:20:26