工厂方法模式在软件系统演化中的应用
Posted windpoplar
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了工厂方法模式在软件系统演化中的应用相关的知识,希望对你有一定的参考价值。
一、第一阶段
1、需求
网站报500类错误时,管理员和开发人员并不能实时知道,等查看日志时或用户打电话过来返回问题时,有可能已经造成了极大的不良影响。所以需要开发一个实时通知功能,将网站的报错信息通过 Email 发送给管理员。
2、实现
写一个异常处理器,配置到系统中进行监听,渲染时走 Email 类发出去。具体代码如下。
package com.taoxi.designpattern.factorymethod.v1; import java.util.ArrayList; import java.util.List; //Email类 public class Email { private String hostName; private int port; private String userName; private String password; private String from; private List<String> toAddress = new ArrayList<>(); private String subject; private String message; //发送 public void send(){ System.out.println("send email!"); } public String getHostName() { return hostName; } public void setHostName(String hostName) { this.hostName = hostName; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public List<String> getToAddress() { return toAddress; } public void setToAddress(List<String> toAddress) { this.toAddress = toAddress; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
package com.taoxi.designpattern.factorymethod.v1; import java.util.ArrayList; import java.util.List; //错误异常处理类 public class ErrorHandler { //渲染异常和错误 public void renderException() { String hostName = "SMTP.163.com"; int port = 586; String userName = "taoxi@163.com"; String password = "******";//授权码 String from = "taoxi@163.com"; List<String> toAddress = new ArrayList<String>(); toAddress.add("tx@jd.com"); toAddress.add("lj@jd.com"); String subject = "报错了!"; String message = "具体的报错内容"; Email email = new Email(); email.setHostName(hostName); email.setPort(port); email.setUserName(userName); email.setPassword(password); email.setFrom(from); email.getToAddress().addAll(toAddress); email.setSubject(subject); email.setMessage(message); email.send(); } }
3、问题
ErrorHandler
作为客户端(调用方),想发送邮件出去,却参与了邮件发送对象的初始化工作,不符合单一职责原则
。
二、第二阶段
1、需求
需要让客服跟进一下异常订单的情况,有人退订单了或下单了长时间没有付款的客户,邮件通知到客服。
2、实现
再写一个订单监听器,配置到系统中进行监听,需要的时候走 EMail 类发出去。
package com.taoxi.designpattern.factorymethod.v2; import java.util.ArrayList; import java.util.List; //订单监听器 public class OrderHandler { //通知处理 public void notifyHandler() { String hostName = "SMTP.163.com"; int port = 586; String userName = "taoxi@163.com"; String password = "******";//授权码 String from = "taoxi@163.com"; List<String> toAddress = new ArrayList<String>(); toAddress.add("tx@jd.com"); toAddress.add("lj@jd.com"); String subject = "订单异常!"; String message = "具体的异常内容"; Email email = new Email(); email.setHostName(hostName); email.setPort(port); email.setUserName(userName); email.setPassword(password); email.setFrom(from); email.getToAddress().addAll(toAddress); email.setSubject(subject); email.setMessage(message); email.send(); } }
3、问题
可复用性太差了,更换个邮件配置还得改多处,需要优化一下了。
4、改进
把Email封装到一起。
package com.taoxi.designpattern.factorymethod.v2_1; import java.util.ArrayList; import java.util.List; //Email类 public class Email { private String hostName; private int port; private String userName; private String password; private String from; private List<String> toAddress = new ArrayList<>(); private String subject; private String message; //获取对象 public static Email getInstance() { String hostName = "SMTP.163.com"; int port = 586; String userName = "taoxi@163.com"; String password = "******";//授权码 String from = "taoxi@163.com"; List<String> toAddress = new ArrayList<String>(); toAddress.add("tx@jd.com"); toAddress.add("lj@jd.com"); Email email = new Email(); email.setHostName(hostName); email.setPort(port); email.setUserName(userName); email.setPassword(password); email.setFrom(from); email.getToAddress().addAll(toAddress); return email; } //设置消息体 public void initMessage(String subject, String message) { this.subject = subject; this.message = message; } //发送消息 public void send(){ System.out.println("send email!"); } public String getHostName() { return hostName; } public void setHostName(String hostName) { this.hostName = hostName; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public List<String> getToAddress() { return toAddress; } public void setToAddress(List<String> toAddress) { this.toAddress = toAddress; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
package com.taoxi.designpattern.factorymethod.v2_1; //错误异常处理 public class ErrorHandler { //渲染异常和错误 public void renderException() { String subject = "报错了!"; String message = "具体的报错内容"; Email email = Email.getInstance(); email.initMessage(subject, message); email.send(); } }
package com.taoxi.designpattern.factorymethod.v2_1; //订单监听器 public class OrderHandler { //监听处理 public void notifyHandler() { String subject = "订单异常!"; String message = "具体的异常内容"; Email email = Email.getInstance(); email.initMessage(subject, message); email.send(); } }
以上是关于工厂方法模式在软件系统演化中的应用的主要内容,如果未能解决你的问题,请参考以下文章