Clever use of enum

Original address: Original address
When we receive some data and need to process it, because they come from different channels (such as Tencent, Toutiao), the processing methods required by different channels are different
. Add attributes to match the corresponding rule in the enumeration. Processing logic

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;
    }
}

Guess you like

Origin blog.csdn.net/My_Way666/article/details/108279408