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

solution

1. First build a GeneralChannelRule base rule abstract class and define an abstract method, process(), to be implemented by different channels.

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

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

'public class TencentChannelRule extends GeneralChannelRule @override public void process() {' // Tencent processing logic' '} ' `} `Copy the code

3. Write a headline rule class that defines the specific processing logic for headline data

'public class TouTiaoChannelRule extends GeneralChannelRule' @override 'public void process() {' // TouTiaoChannelRule'} ' `} `Copy the code

Create a simple enumeration class

/ * * ` public enum ChannelRuleEnum {` ` ` ` * headlines ` ` * / ` ` TOUTIAO (" TOUTIAO "), ` ` / * * ` ` * TENCENT ` ` * / ` ` TENCENT (" TENCENT "), ` `; ` `... ` ` `}Copy the code

5. Use rules to process data.

' 'public static void main(String[] args) {'' '; ` `GeneralChannelRule rule; ` ` / / according to the rules of corresponding channels to obtain the corresponding specific implementation class ` ` if (ChannelRuleEnum. TENCENT. Code. Equals (sign)) {` ` rule = new TencentChannelRule (); ` `} else if (ChannelRuleEnum.TOUTIAO.code.equals(sign)) {` `rule = new TouTiaoChannelRule(); ' '} else {' // cannot match ' '} '// executes'' rule-.process (); ` ` `}Copy the code

If the above way, there are two disadvantages.

When we need to add new channels, we need to modify the logic in the main method. This violates the open and closed rule of design patterns. The core idea of open and closed original BAI is that software entities can be expanded, but not modified.

That is, the DAO is open for extensions and closed for modifications

Modifying the code after adding channels produces a lot of if else, which is not very elegant. To solve both of these problems, we can use enumerated classes for clever optimization.

New train of thought

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

/ * * ` public enum ChannelRuleEnum {` ` ` ` * headlines ` ` * / ` ` TOUTIAO (" TOUTIAO, "new TouTiaoChannelRule ()), ` ` / * * ` ` * tencent ` ` * / ` `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; ` ` `} ` `}Copy the code

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); ` ` `}Copy the code

Parsing: Binding keys to rule concrete implementations in enumerations by using enumeration classes. By changing:

If we need new channels, we can just write the concrete rule implementation class and inherit the GeneralChannelRule abstract class, and add the new enumeration in the enumeration class without changing any of the original code. This is in line with the closed development principle.

The last

There are many interesting solutions to reduce if-else (e.g., state design patterns, etc.). If you are interested, check out the relevant information.