自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

策略模式簡潔的實現(xiàn)方式,你知道嗎?

開發(fā) 前端
策略模式還挺簡單的,就是定義一個接口,然后有多個實現(xiàn)類,每種實現(xiàn)類封裝了一種行為。然后根據(jù)條件的不同選擇不同的實現(xiàn)類。

if else 太多了

最近跟著公司的大佬開發(fā)了一款IM系統(tǒng),類似QQ和微信哈,就是聊天軟件。我們有一部分業(yè)務邏輯是這樣的。

if (msgType = "文本") {
// dosomething
} else if(msgType = "圖片") {
// doshomething
} else if(msgType = "視頻") {
// doshomething
} else {
// doshomething
}

就是根據(jù)消息的不同類型有不同的處理策略,每種消息的處理策略代碼都很長,如果都放在這種if else代碼快中,代碼很難維護也很丑,所以我們一開始就用了策略模式來處理這種情況。

策略模式還挺簡單的,就是定義一個接口,然后有多個實現(xiàn)類,每種實現(xiàn)類封裝了一種行為。然后根據(jù)條件的不同選擇不同的實現(xiàn)類。

實現(xiàn)過程

消息對象,當然真實的對象沒有這么簡單,省略了很多屬性。

@Data
@AllArgsConstructor
public class MessageInfo {

// 消息類型
private MsgTypeEnum type;
// 消息內(nèi)容
private String content;

}

消息類型是一個枚舉類。

public enum MsgTypeEnum {

TEXT(1, "文本"),
IMAGE(2, "圖片"),
VIDEO(3, "視頻");

public final int code;
public final String name;

MsgTypeEnum(int code, String name) {
this.code = code;
this.name = name;
}
}

定義一個消息處理策略接口。

public interface MessageStrategy {

void handleMessage(MessageInfo messageInfo);
}

有2個消息處理接口,分別處理不同的消息。

處理文本消息

@Service
@MsgTypeHandler(value = MsgTypeEnum.TEXT)
public class TextMessageStrategy implements MessageStrategy {

@Override
public void handleMessage(MessageInfo messageInfo) {
System.out.println("處理文本消息 " + messageInfo.getContent());
}
}

處理圖片消息;

@Service
@MsgTypeHandler(value = MsgTypeEnum.IMAGE)
public class ImageMessageStrategy implements MessageStrategy {

@Override
public void handleMessage(MessageInfo messageInfo) {
System.out.println("處理圖片消息 " + messageInfo.getContent());
}
}

可以看到上面我們用了一個MsgTypeHandler注解來表明策略類是用來處理哪種消息的?

@Documented
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MsgTypeHandler {

MsgTypeEnum value();
}

至此,所有代碼就已經(jīng)寫完了,來跑一下測試代碼;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

private Map<MsgTypeEnum, MessageStrategy> messageStrategyMap;

@Resource
private void setMessageStrategyMap(List<MessageStrategy> messageStrategies) {
messageStrategyMap = messageStrategies.stream().collect(
Collectors.toMap(item -> AnnotationUtils.findAnnotation(item.getClass(), MsgTypeHandler.class).value(), v -> v));
}

@Test
public void contextLoads() {
MessageInfo messageInfo = new MessageInfo(MsgTypeEnum.TEXT, "這是一個文本消息");
MessageStrategy messageStrategy = messageStrategyMap.get(messageInfo.getType());
// 處理文本消息 這是一個文本消息
messageStrategy.handleMessage(messageInfo);
}
}

在setMessageStrategyMap方法上加@Resource注解,將消息類型及其對應的策略類保存到map中,當消息來臨的時候就能從map中獲取到對應的策略類,然后處理消息。

責任編輯:武曉燕 來源: Java識堂
相關推薦

2024-02-05 12:08:07

線程方式管理

2023-02-13 08:10:40

Gateway網(wǎng)關Spring

2023-04-28 12:37:59

Spring@Bean使用方式

2023-04-23 09:50:50

@BeanSpring

2024-11-26 00:45:29

free區(qū)域字段

2024-11-26 14:29:48

2024-05-20 10:37:08

Rust模式通信

2019-02-12 11:15:15

Spring設計模式Java

2024-06-12 08:05:06

2023-02-27 07:56:55

IngressKubernetes

2023-12-12 08:41:01

2021-10-14 06:52:47

算法校驗碼結構

2024-09-18 07:00:00

消息隊列中間件消息隊列

2022-09-29 15:32:58

云計算計算模式

2018-07-04 11:02:23

無線傳輸模式

2019-12-02 10:16:46

架構設計模式

2024-07-03 08:33:08

2024-04-07 00:00:00

ESlint命令變量

2024-05-28 09:12:10

2022-01-19 13:57:22

ymlSpringSnakeYml
點贊
收藏

51CTO技術棧公眾號