项目中要实现一个功能,功能具体为,选择不同的邮件名称,产生不同的邮件模板。
功能图例
实现思路上,想到了通过简单工厂模式,简单工厂模式只关心产出的产品不关心实现过程,只需要传入一个类型吗,就能得到想要的结果,十分符合开闭原则。
下面是该功能的UML类图
首先,定义一个抽象的父类 IShMessageEmailService
然后定义实现类 ShMessageEmailEtaService 及 ShMessageEmailEtdService
最后定义工厂类 ShipMessageEmailFactory 工厂类接受一个类型码参数,用来判定产生哪个产品。
代码如下
public interface IShMessageEmailService {
public String getMessageText(ShDynamic dynamic, SailingSchedule sailingSchedule, String messageType);
}
@Service
public class ShMessageEmailEtaService extends BusinessService implements IShMessageEmailService {
@Autowired
private ShManifestCargoService shManifestCargoService;
@Autowired
private ShipPortService shipPortService;
@Override
public String getMessageText(ShDynamic dynamic, SailingSchedule sailingSchedule, String messageType) {
ShManifestCargo shManifestCargo = shManifestCargoService.findImpCargoAndBlNo(sailingSchedule.getId());
String loadPortCode = "";
String loadPortName = "";
String dischargePortCode = "";
String dischargePortName = "";
String loadPortCnName = "";
String dischargePortCnName = "";
String cargoDesc = "";
String blNo = "";
BigDecimal grossWeight = new BigDecimal(0);
if (shManifestCargo != null) {
if (shManifestCargo.getCargoDesc() != null) {
cargoDesc = shManifestCargo.getCargoDesc();
}
}
//装货港
if (shManifestCargo != null) {
blNo = shManifestCargo.getBlNo();
if (shManifestCargo.getGrossWeight() != null) {
grossWeight = shManifestCargo.getGrossWeight();
}
loadPortCode = shManifestCargo.getLoadPortCode();
dischargePortCode = shManifestCargo.getDischargePortCode();
if (loadPortCode != null) {
ShipPort shipPort = shipPortService.findByPortCode(loadPortCode);
loadPortName = shipPort.getName();
if (shipPort.getCountryCode() != null) {
loadPortCnName = shipPort.getCountryCode();
}
}
if (dischargePortCode != null) {
ShipPort shipPort = shipPortService.findByPortCode(dischargePortCode);
dischargePortName = shipPort.getName();
if (shipPort.getCountryCode() != null) {
dischargePortCnName = shipPort.getCountryCode();
}
}
}
StringBuilder sb = new StringBuilder();
sb.append("Dear " + (sailingSchedule.getFirstClientName() == null ? "" : sailingSchedule.getFirstClientName()) + "\n");
sb.append("\n");
sb.append("Mt. Morning Calm " + sailingSchedule.getShipEnName() + "\n");
sb.append("=== Cargo: " + cargoDesc + "=== \n");
if ("".equals(loadPortCode) || "".equals(loadPortCnName)) {
sb.append("Port of Loading: " + loadPortName + loadPortCnName + "\n");
} else {
sb.append("Port of Loading: " + loadPortName + "," + loadPortCnName + "\n");
}
if ("".equals(dischargePortCode) || "".equals(dischargePortCnName)) {
sb.append("Port of Discharging: " + dischargePortName + dischargePortCnName + "\n");
} else {
sb.append("Port of Discharging: " + dischargePortName + "," + dischargePortCnName + "\n");
}
if ("".equals(blNo)) {
if (grossWeight.compareTo(new BigDecimal(0)) == 0) {
sb.append("B/L Quantity: \n");
} else {
sb.append("B/L Quantity:" + grossWeight + "KGS\n");
}
} else {
if (grossWeight.compareTo(new BigDecimal(0)) == 0) {
sb.append("B/L Quantity:" + blNo + "\n");
} else {
sb.append("B/L Quantity:" + blNo + "," + grossWeight + "KGS\n");
}
}
sb.append("\n");
sb.append("--/-/- Arrived Guishan p/stn\n");
sb.append("--/-/- Dropped anchor & NOR tendered\n");
sb.append("--/-/- POB\n");
// 靠泊时间
sb.append("--/-/- First line ashored\n");
// ALL FAST
sb.append("--/-/- All line fasted at wharf No.4\n");
// 开始联检时间
sb.append("--/-/- Free pratique granted\n");
// 开工时间
sb.append("--/-/- Estimated time of discharging commence\n");
// 预计完工时间
sb.append("--/-/- Estimated time of discharging complete\n");
// 预计离泊时间
sb.append("--/-/- ETD (Subject to discharging & Main Channel traffic control) \n");
sb.append("\n");
sb.append("Arrival conditions:\n");
// 抵港油水FO DO FW
BigDecimal arrvFo = dynamic.getArrvFo();
BigDecimal arrvDo = dynamic.getArrvDo();
BigDecimal arrvFw = dynamic.getArrvFw();
sb.append("Bunker - FO:" + (arrvFo == null ? "" : arrvFo) + "MTS/DO:" + (arrvDo == null ? "" : arrvDo) + "MTS/FW:" + (arrvFw == null ? "" : arrvFw) + "MTS \n");
// 抵港吃水F A
BigDecimal arrvDraughtf = dynamic.getArrvDraughtf();
BigDecimal arrvDraughta = dynamic.getArrvDraughta();
sb.append("Draft - F:" + (arrvDraughtf == null ? "" : arrvDraughtf) + "M/A:" + (arrvDraughta == null ? "" : arrvDraughta) + "M \n");
sb.append("\n");
sb.append("Remark:\n");
sb.append("1.Vessel can (un)berth at Xiaohu terminal only in daytime.\n");
sb.append("2.Vessel can (un)berth only against ebb tide.\n");
sb.append("3.Two tugboats assist berthing. \n");
sb.append("\n");
sb.append("Thank you for your kind agency appointment.If any inquries pls be free let me know. \n");
sb.append("We respectfully remind you to also email the relevant information to the operator in charge of this voyage and do not reply this system email. \n");
return sb.toString();
}
}
@Service
public class ShMessageEmailEtdService extends BusinessService implements IShMessageEmailService {
@Autowired
private ShManifestCargoService shManifestCargoService;
@Autowired
private ShipPortService shipPortService;
@Override
public String getMessageText(ShDynamic dynamic, SailingSchedule sailingSchedule, String messageType) {
ShManifestCargo shManifestCargo = shManifestCargoService.findExpCargoAndBlNo(sailingSchedule.getId());
String loadPortCode = "";
String loadPortName = "";
String dischargePortCode = "";
String dischargePortName = "";
String loadPortCnName = "";
String dischargePortCnName = "";
String cargoDesc = "";
String blNo = "";
BigDecimal grossWeight = new BigDecimal(0);
if(shManifestCargo!=null){
if(shManifestCargo.getCargoDesc()!=null){
cargoDesc = shManifestCargo.getCargoDesc();
}
}
//装货港
if (shManifestCargo!=null) {
blNo = shManifestCargo.getBlNo();
if(shManifestCargo.getGrossWeight()!=null){
grossWeight = shManifestCargo.getGrossWeight();
}
loadPortCode = shManifestCargo.getLoadPortCode();
dischargePortCode = shManifestCargo.getDischargePortCode();
if (loadPortCode != null) {
ShipPort shipPort = shipPortService.findByPortCode(loadPortCode);
loadPortName = shipPort.getName();
if(shipPort.getCountryCode()!=null){
loadPortCnName = shipPort.getCountryCode();
}
}
if (dischargePortCode != null) {
ShipPort shipPort = shipPortService.findByPortCode(dischargePortCode);
dischargePortName = shipPort.getName();
if(shipPort.getCountryCode()!=null){
dischargePortCnName = shipPort.getCountryCode();
}
}
}
StringBuilder sb = new StringBuilder();
sb.append("Dear " + (sailingSchedule.getFirstClientName() == null ? "" : sailingSchedule.getFirstClientName()) + "\n");
sb.append("\n");
sb.append("Mt. Morning Calm " + sailingSchedule.getShipEnName() + "\n");
sb.append("=== Cargo: " + cargoDesc + "=== \n");
if ("".equals(loadPortCode) || "".equals(loadPortCnName)) {
sb.append("Port of Loading: " + loadPortName + loadPortCnName + "\n");
} else {
sb.append("Port of Loading: " + loadPortName + "," + loadPortCnName + "\n");
}
if ("".equals(dischargePortCode) || "".equals(dischargePortCnName)) {
sb.append("Port of Discharging: " + dischargePortName + dischargePortCnName + "\n");
} else {
sb.append("Port of Discharging: " + dischargePortName + "," + dischargePortCnName + "\n");
}
// sb.append("Pls B Advd Abv Vsl's Arriving Conditon Asfs:\n");
if ("".equals(blNo)) {
if(grossWeight.compareTo(new BigDecimal(0))==0) {
sb.append("B/L Quantity: \n");
}else{
sb.append("B/L Quantity:"+ grossWeight + "KGS\n");
}
}else{
if(grossWeight.compareTo(new BigDecimal(0))==0) {
sb.append("B/L Quantity:" + blNo + "\n");
}else{
sb.append("B/L Quantity:" + blNo + "," + grossWeight + "KGS\n");
}
}
sb.append("\n");
sb.append("--/-/- Arrived Guishan p/stn\n");
sb.append("--/-/- Dropped anchor & NOR tendered\n");
sb.append("--/-/- POB\n");
// 靠泊时间
sb.append("--/-/- First line ashored\n");
// ALL FAST
sb.append("--/-/- All line fasted\n");
// 开始联检时间
sb.append("--/-/- Free pratique granted\n");
// 开工时间
sb.append("--/-/- Hose on\n");
// 预计完工时间
sb.append("--/-/- Discharging commenced\n");
// 预计离泊时间
sb.append("--/-/- Discharging completed \n");
sb.append("--/-/- Vapour pushing completed \n");
sb.append("--/-/- Hose off \n");
sb.append("--/-/- Outbound pilot on board \n");
sb.append("--/-/- Sailed from Nansha \n");
sb.append("--/-/- ETA Tokuyama \n");
sb.append("\n");
sb.append("Arrival conditions:\n");
// 抵港油水FO DO FW
BigDecimal arrvFo = dynamic.getArrvFo();
BigDecimal arrvDo = dynamic.getArrvDo();
BigDecimal arrvFw = dynamic.getArrvFw();
sb.append("Bunker - FO:" + (arrvFo == null ? "" : arrvFo) + "MTS/DO:" + (arrvDo == null ? "" : arrvDo) + "MTS/FW:" + (arrvFw == null ? "" : arrvFw) + "MTS\n");
// 抵港吃水F A
BigDecimal arrvDraughtf = dynamic.getArrvDraughtf();
BigDecimal arrvDraughta = dynamic.getArrvDraughta();
sb.append("Draft - F:" + (arrvDraughtf == null ? "" : arrvDraughtf) + "M/A:" + (arrvDraughta == null ? "" : arrvDraughta) + "M\n");
sb.append("\n");
sb.append("Departure conditions:\n");
// 离港油水FO DO FW
BigDecimal deptFo = dynamic.getDeptFo();
BigDecimal deptDo = dynamic.getDeptDo();
BigDecimal deptFw = dynamic.getDeptFw();
sb.append("Bunker - FO:" + (deptFo == null ? "" : deptFo) + "MTS/DO:" + (deptDo == null ? "" : deptDo) + "MTS/FW:" + (deptFw == null ? "" : deptFw) + "MTS \n");
// 离港吃水F A
BigDecimal deptDraughtf = dynamic.getDeptDraughtf();
BigDecimal deptDraughta = dynamic.getDeptDraughta();
sb.append("Draft - F:" + (deptDraughtf == null ? "" : deptDraughtf) + "M/A:" + (deptDraughta == null ? "" : deptDraughta) + "M \n");
sb.append("\n");
sb.append("Remark:\n");
sb.append("1.Vessel can (un)berth at Xiaohu terminal only in daytime.\n");
sb.append("2.Vessel can (un)berth only against ebb tide.\n");
sb.append("3.Two tugboats assist berthing. \n");
sb.append("4.One tugboat assist unberthing. \n");
sb.append("\n");
sb.append("Thanks again for your kind agency appointment.If any inquries pls be free let me know.\n");
sb.append("We respectfully remind you to also email the relevant information to the operator in charge of this voyage and do not reply this system email. \n");
return sb.toString();
}
}
@Service
public class ShipMessageEmailFactory {
public static final String ETA_MESSAGE = "etaMessage";
public static final String ETD_MESSAGE = "etdMessage";
public static final String PROGRESS_MESSAGE = "progressMessage";
@Autowired
private ShMessageEmailEtaService shMessageEmailEtaService;
@Autowired
private ShMessageEmailEtdService shMessageEmailEtdService;
public IShMessageEmailService createProduct(String messageType) {
if (messageType.equals(ETA_MESSAGE)) {
return shMessageEmailEtaService;
} else if (messageType.equals(ETD_MESSAGE)) {
return shMessageEmailEtdService;
}
return null;
}
}
//调用该方法
@RequestMapping(value = "query_email_model", method = {RequestMethod.GET})
public SailingQueryEntity queryEmailModel(@RequestParam("id") String id, @RequestParam("messageType") String messageType) {
SailingQueryEntity entity = new SailingQueryEntity();
SailingSchedule sailingSchedule = service.find(SailingSchedule.class, id);
List dynamic = shipDynamicService.findBySailingScheduleId(id);
ShDynamic dynamicEntity = dynamic.get(0);
String text = "";
if(!messageType.equals(shipMessageEmailFactory.PROGRESS_MESSAGE)){
IShMessageEmailService shMessageEmailService = shipMessageEmailFactory.createProduct(messageType);
text = shMessageEmailService.getMessageText(dynamicEntity, sailingSchedule, messageType);
}
entity.setArrivedText(text);
return entity;
}
需要注意的是,一般简单工厂模式的工厂类都是使用static进行修饰,即静态方法。但是我在使用static进行修饰时,却报错,原因是,通过spring管理的service不是静态的,而静态方法,要求返回值必须是静态的才行,将static去掉可以了。简单工厂模式很简单,不属于23中设计模式,但是对于软件的拓展性,确是十分重要。