日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

項目中要實現一個功能,功能具體為,選擇不同的郵件名稱,產生不同的郵件模板。
功能圖例


 

設計模式--簡單工廠模式代碼詳解

 

實現思路上,想到了通過簡單工廠模式,簡單工廠模式只關心產出的產品不關心實現過程,只需要傳入一個類型嗎,就能得到想要的結果,十分符合開閉原則。

下面是該功能的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 + "KGSn");
            }
        } else {
            if (grossWeight.compareTo(new BigDecimal(0)) == 0) {
                sb.append("B/L Quantity:" + blNo + "n");

            } else {
                sb.append("B/L Quantity:" + blNo + "," + grossWeight + "KGSn");
            }
        }
        sb.append("n");
        sb.append("--/-/-   Arrived Guishan p/stnn");
        sb.append("--/-/-   Dropped anchor & NOR tenderedn");
        sb.append("--/-/-   POBn");
        // 靠泊時間
        sb.append("--/-/-   First line ashoredn");
        // ALL FAST
        sb.append("--/-/-   All line fasted at wharf No.4n");
        // 開始聯檢時間
        sb.append("--/-/-   Free pratique grantedn");
        // 開工時間
        sb.append("--/-/-   Estimated time of discharging commencen");
        // 預計完工時間
        sb.append("--/-/-   Estimated time of discharging completen");
        // 預計離泊時間
        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 + "KGSn");
            }
        }else{
            if(grossWeight.compareTo(new BigDecimal(0))==0) {
                sb.append("B/L Quantity:" + blNo + "n");

            }else{
                sb.append("B/L Quantity:" + blNo + "," + grossWeight + "KGSn");
            }
        }
        sb.append("n");
        sb.append("--/-/-   Arrived Guishan p/stnn");
        sb.append("--/-/-   Dropped anchor & NOR tenderedn");
        sb.append("--/-/-   POBn");
        // 靠泊時間
        sb.append("--/-/-   First line ashoredn");
        // ALL FAST
        sb.append("--/-/-   All line fastedn");
        // 開始聯檢時間
        sb.append("--/-/-   Free pratique grantedn");
        // 開工時間
        sb.append("--/-/-   Hose onn");
        // 預計完工時間
        sb.append("--/-/-   Discharging commencedn");
        // 預計離泊時間
        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) + "MTSn");
        // 抵港吃水F A
        BigDecimal arrvDraughtf = dynamic.getArrvDraughtf();
        BigDecimal arrvDraughta = dynamic.getArrvDraughta();
        sb.append("Draft -  F:" + (arrvDraughtf == null ? "" : arrvDraughtf) + "M/A:" + (arrvDraughta == null ? "" : arrvDraughta) + "Mn");
        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去掉可以了。簡單工廠模式很簡單,不屬于23中設計模式,但是對于軟件的拓展性,確是十分重要。

分享到:
標簽:模式 設計
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定