3

Scenario: When we receive some data and need to process it, because they come from different channels (such as Tencent, Toutiao), different channels require different processing methods. Let's write a simple Demo to realize this scenario. .

Solutions

1. First, construct an abstract class of GeneralChannelRule basic rules, and define an abstract method process(). Different channels need to implement this abstract method.

`public abstract class GeneralChannelRule {`
 `public abstract void process();`
`}`

2. Write a Tencent rule class and define specific processing logic for Tencent channel data

`public class TencentChannelRule extends GeneralChannelRule`
 `@Override`
 `public void process() {`
 `// Tencent处理逻辑`
 `}`
`}`

3. Write a rule class for headlines and define specific processing logic for headlines data

`public class TouTiaoChannelRule extends GeneralChannelRule`
 `@Override`
 `public void process() {`
 `// TouTiao处理逻辑`
 `}`
`}`

4. Create a simple enumeration class

`public enum ChannelRuleEnum {`
 `/**`
 `* 头条`
 `*/`
 `TOUTIAO("TOUTIAO"),`
 `/**`
 `* 腾讯`
 `*/`
 `TENCENT("TENCENT"),`
 `;`
 `....`
`}`

5. Use rules to process data.

`public static void main(String[] args) {`
 `//这里我们模拟接收到的数据,其渠道为为TOUTIAO,来自头条的数据`
 `String sign = "TOUTIAO";`
 `GeneralChannelRule rule;`
 `//根据对应渠道获取对应的具体规则实现类`
 `if (ChannelRuleEnum.TENCENT.code.equals(sign)) {`
 `rule = new TencentChannelRule();`
 `} else if (ChannelRuleEnum.TOUTIAO.code.equals(sign)) {`
 `rule = new TouTiaoChannelRule();`
 `} else {`
 `//匹配不到`
 `}`
 `//执行`
 `rule.process();`
 `}`

Analysis: If the above method is adopted, there are two shortcomings.

When we need to add new channels, we need to modify the logic in the main method. This violates the open and closed rules in design patterns. The core idea of the open and closed principle is that the software entity can be expanded and cannot be modified.

In other words, the expansion is open, but the modification is closed

After adding channels, modifying the code will generate a lot of if else, which is not very elegant. In order to solve the above two problems, we can use the enumeration class to ingeniously optimize.

New ideas

1. Let's adjust the enumeration class, add a GeneralChannelRule attribute, build the corresponding GeneralChannelRule implementation class for the corresponding channel, and add a match() matching method.

`public enum ChannelRuleEnum {`
 `/**`
 `* 头条`
 `*/`
 `TOUTIAO("TOUTIAO",new TouTiaoChannelRule()),`
 `/**`
 `* 腾讯`
 `*/`
 `TENCENT("TENCENT",new TencentChannelRule()),`
 `;`
 `public String name;`
 `public GeneralChannelRule channel;`
 `ChannelRuleEnum(String name, GeneralChannelRule channel) {`
 `this.name = name;`
 `this.channel = channel;`
 `}`
 `//匹配`
 `public static ChannelRuleEnum match(String name){`
 `ChannelRuleEnum[] values = ChannelRuleEnum.values();`
 `for (ChannelRuleEnum value : values) {`
 `if(value.name.equals(name)){`
 `return value;`
 `}`
 `}`
 `return null;`
 `}`
 `public String getName() {`
 `return name;`
 `}`
 `public GeneralChannelRule getChannel() {`
 `return channel;`
 `}`
`}`

2. Rewrite the program

`public static void main(String[] args) {`
 `String sign = "TOUTIAO";`
 `ChannelRuleEnum channelRule = ChannelRuleEnum.match(sign);`
 `GeneralChannelRule rule = channelRule.channel;`
 `rule.process(sign);`
 `}`

Analysis: By using the enumeration class, the key is bound to the specific implementation of the rule in the enumeration. By changing:

The if-else can be reduced to make the code more elegant. If you need to add channels, we only need to write a specific rule implementation class and inherit the GeneralChannelRule abstract class, and add new enumerations in the enumeration class without changing any of the original ones. Code. This is in line with the development closed principle.

At last

The above is a solution to cleverly kill if-else through enumeration. There are many interesting solutions to reduce if-else (such as: state design patterns, etc.). If you are interested, please check relevant information.


用户bPcHjCF
1.5k 声望1k 粉丝