实战!通过简单工厂模式实现功能

简介: 实战!通过简单工厂模式实现功能

一 故事背景

项目中要实现一个功能,功能具体为,选择不同的邮件名称,产生不同的邮件模板。
功能图例

image.png

二 设计

实现思路上,想到了通过简单工厂模式,简单工厂模式只关心产出的产品不关心实现过程,只需要传入一个类型吗,就能得到想要的结果,十分符合开闭原则。下面是该功能的UML类图

image.png

首先,定义一个抽象的父类 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<ShDynamic> 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去掉可以了。

相关文章
|
7月前
|
设计模式 Java PHP
php设计模式--简单工厂模式(一)
php设计模式--简单工厂模式(一)
37 0
|
7月前
|
设计模式 PHP
php设计模式--抽象工厂模式(二)
php设计模式--抽象工厂模式(二)
36 0
|
7月前
|
设计模式 Java Go
[设计模式Java实现附plantuml源码~创建型] 产品族的创建——抽象工厂模式
[设计模式Java实现附plantuml源码~创建型] 产品族的创建——抽象工厂模式
|
7月前
|
设计模式 Java Go
[设计模式Java实现附plantuml源码~创建型] 集中式工厂的实现~简单工厂模式
[设计模式Java实现附plantuml源码~创建型] 集中式工厂的实现~简单工厂模式
|
7月前
|
设计模式 存储 JavaScript
[设计模式Java实现附plantuml源码~创建型] 多态工厂的实现——工厂方法模式
[设计模式Java实现附plantuml源码~创建型] 多态工厂的实现——工厂方法模式
|
设计模式 Java
简化复杂系统:深入解析Java设计模式中的外观模式
在软件开发领域,设计模式是一套经过验证的最佳实践方法,用于解决各种常见问题。外观模式是一种结构型设计模式,其目标是为复杂子系统提供一个简单的接口。在本文中,我们将深入探讨外观模式的核心思想,以及它在Java中的实际应用。
88 0
|
前端开发
前端学习案例3-工厂模式3
前端学习案例3-工厂模式3
61 0
前端学习案例3-工厂模式3
|
前端开发
前端学习案例1-工厂模式1
前端学习案例1-工厂模式1
57 0
前端学习案例1-工厂模式1
|
前端开发
前端学习案例2-工厂模式2
前端学习案例2-工厂模式2
67 0
前端学习案例2-工厂模式2
|
设计模式 C++
【设计模式学习笔记】简单工厂模式、工厂模式、抽象工厂模式案例详解(C++实现)
【设计模式学习笔记】简单工厂模式、工厂模式、抽象工厂模式案例详解(C++实现)
345 0
【设计模式学习笔记】简单工厂模式、工厂模式、抽象工厂模式案例详解(C++实现)