日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > windows >内容正文

windows

设计模式---策略模式+工厂

發(fā)布時(shí)間:2023/11/16 windows 34 coder
生活随笔 收集整理的這篇文章主要介紹了 设计模式---策略模式+工厂 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

關(guān)鍵詞:設(shè)計(jì)模式,策略模式,工廠模式

概要

現(xiàn)在我需要實(shí)現(xiàn)一個(gè)功能,是添加一路SDI輸出,但是輸出的協(xié)議有不同,有udp、srt等,針對(duì)不同的協(xié)議我要做不同的操作,后面還有可能添加其他的協(xié)議,因此這里面用策略模式不錯(cuò)。

由于單純的策略模式并不能完全消除if...else...,這里我們用了工廠模式再進(jìn)行封裝(其實(shí)就是通過(guò)List或Map,消除if...else...)

這里使用springboot管理bean,如果不是spring,自己去new就行。

代碼概要

策略接口

public interface SDIStrategy {

    /**
     * 創(chuàng)建SDI,先在數(shù)據(jù)庫(kù)中創(chuàng)建此目的地,然后在根據(jù)相關(guān)協(xié)議組裝請(qǐng)求,去SMH創(chuàng)建目的地
     */
    void createSDI(Route route, OpenSDIReq req);
}

策略實(shí)現(xiàn)

@Component
public class SDIUDPStrategy implements SDIStrategy {

    @Override
    public void createSDI(Route route, OpenSDIReq req) {
        // do something udp一般是內(nèi)網(wǎng)訪問(wèn),ip可配
    }
}
@Component
public class SDISRTStrategy implements SDIStrategy {

    @Override
    public void createSDI(Route route, OpenSDIReq req) {
		// do something srt給外網(wǎng)用,可配置端口,延時(shí),加密方式,TTL等
    }
}

工廠

@Component
public class SDIStrategyFactory {

    private static final Map<String, SDIStrategy> strategies = new HashMap<>();

    @Resource
    private SDIUDPStrategy sdiudpStrategy;
    @Resource
    private SDISRTStrategy sdisrtStrategy;

    @PostConstruct
    public void init() {
        strategies.put(SDIProtocolType.UDP.getProtocol(), sdiudpStrategy);
        strategies.put(SDIProtocolType.SRT.getProtocol(), sdisrtStrategy);
    }

    public SDIStrategy getStrategy(String protocol) {
        if (SDIProtocolType.getEnum(protocol) == null) {
            throw new ServiceException("illegal protocol type, please check it");
        }
        return strategies.get(protocol);
    }

}

此enum類(lèi)并不是重點(diǎn),可不看

public enum SDIProtocolType {

    UDP("udp"),
    SRT("srt");

    private String protocol;

    SDIProtocolType(String protocol) {
        this.protocol = protocol;
    }

    public String getProtocol() {
        return protocol;
    }

    public static SDIProtocolType getEnum(String protocol) {
        for (SDIProtocolType value : SDIProtocolType.values()) {
            if (value.getProtocol().equals(protocol)) {
                return value;
            }
        }
        return null;
    }
}

最終的調(diào)用

@Slf4j
@Service
public class SDIServiceImpl implements ISDIService {
    @Resource
    private SDIStrategyFactory sdiStrategyFactory;
    
    private void createSDIDest(Route route, OpenSDIReq req) {
        String protocol = req.getProtocol();
        SDIStrategy strategy = sdiStrategyFactory.getStrategy(protocol);
        strategy.createSDI(route, req);

    }
}

總結(jié)

以上是生活随笔為你收集整理的设计模式---策略模式+工厂的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。