1)桥梁模式的用意与应用场景:
桥梁模式的用意是"将抽象化(Abstraction)与实现化(Implementation)脱耦,使得二者可以独立地变化"。这句话有三个关键词,也就是抽象化、实现化和脱耦。
桥梁模式则是解决在不破坏封装的情况下如何抽取出它的抽象部分和实现部分,它的前提是不破坏封装,让抽象部分和实现部分都可以独立地变化,在例子中,我们的邮件服务器和邮件模版是不是都可以独立地变化?不管是邮件服务器还是邮件模板,只要继承了抽象类就可以继续扩展,它的主旨是建立一个不破坏封装性的可扩展架构。
2)UML类图:
3)代码:
package test7.bridge; abstract class MailTemplate { //邮件发件人 private String from; //收件人 private String to; //邮件标题 private String subject; //邮件内容 private String context; //通过构造函数传递邮件信息 public MailTemplate(String _from,String _to,String _subject,String _context){ this.from = _from; this.to = _to; this.subject = _subject; this.context = _context; } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public String getTo() { return to; } public void setTo(String to) { this.to = to; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public void setContext(String context){ this.context = context; } //邮件都有内容 public String getContext(){ return context; } //允许增加邮件发送标志 public void add(String sendInfo){ context = sendInfo + context; } } class TextMail extends MailTemplate { public TextMail(String _from, String _to, String _subject, String _context) { super(_from, _to, _subject, _context); } public String getContext() { //文本类型则设置邮件的格式为:text/plain String context = "\nContent-Type: text/plain;charset=GB2312\n" +super.getContext(); //同时对邮件进行base64编码处理,这里用一句话代替 context = context + "\n邮件格式为:文本格式"; return context; } } class HtmlMail extends MailTemplate { public HtmlMail(String _from, String _to, String _subject, String _context) { super(_from, _to, _subject, _context); } public String getContext(){ //超文本类型则设置邮件的格式为:multipart/mixed String context = "\nContent-Type: multipart/mixed;charset=GB2312\n"+super.getContext(); //同时对邮件进行HTML检查,是否有类似未关闭的标签 context = context + "\n邮件格式为:超文本格式"; return context; } } abstract class MailServer { //发送的是哪封邮件 protected final MailTemplate m; public MailServer(MailTemplate _m){ this.m = _m; } //发送邮件 public void sendMail(){ System.out.println("====正在发送的邮件信息===="); //发件人 System.out.println("发件人:" + m.getFrom()); //收件人 System.out.println("收件人:" + m.getTo()); //标题 System.out.println("邮件标题:" + m.getSubject() ); //邮件内容 System.out.println("邮件内容:" + m.getContext()); } } class Postfix extends MailServer { public Postfix(MailTemplate _m) { super(_m); } //修正邮件发送程序 public void sendMail(){ //增加邮件服务器信息 String context ="Received: from XXXX (unknown [xxx.xxx.xxx.xxx]) by aaa.aaa.com (Postfix) with ESMTP id 8DBCD172B8\n" ; super.m.add(context); super.sendMail(); } } class SendMail extends MailServer { //传递一份邮件 public SendMail(MailTemplate _m) { super(_m); } //修正邮件发送程序 @Override public void sendMail(){ //增加邮件服务器信息 super.m.add("Received: (sendmail); 7 Nov 2009 04:14:44 +0100"); super.sendMail(); } } public class Application { public static void main(String[] args) { //创建一封TEXT格式的邮件 MailTemplate m = new HtmlMail("[email protected]","[email protected]","外星人攻击地球了","结局是外星人被中国人熬汤炖着吃了!"); //使用postfix发送邮件 MailServer mail = new Postfix(m); //发送邮件 mail.sendMail(); } }