1、定义抽象类
packagecom.polaris.design; /*** @author:shi * @date :Created in 2020/8/18 20:15 * @description: * @modified By: */ public abstract classGeneralChannelRule { public abstract voidprocess(); }
2、实现抽象类方法
packagecom.polaris.design; /*** @author:shi * @date :Created in 2020/8/18 20:18 * @description: * @modified By: */ public class JDChannelRule extendsGeneralChannelRule { @Override public voidprocess() { System.out.println("我们大京东!"); } }
3、枚举
packagecom.polaris.design; /*** @author:shi * @date :Created in 2020/8/18 20:18 * @description: * @modified By: */ public enumChannelRuleEnum { /*** 京东 */JD("JD", newJDChannelRule()), /*** 头条 */TOUTIAO("TOUTIAO", newTouTiaoChannelRule()), /*** 腾讯 */TENCENT("TENCENT", newTencentChannelRule()); publicString name; publicGeneralChannelRule channel; ChannelRuleEnum(String name, GeneralChannelRule channel) { this.name =name; this.channel =channel; } //匹配 public staticChannelRuleEnum match(String name) { ChannelRuleEnum[] values =ChannelRuleEnum.values(); for(ChannelRuleEnum value : values) { if(value.name.equals(name)) { returnvalue; } } return null; } publicString getName() { returnname; } publicGeneralChannelRule getChannel() { returnchannel; } }
4、测试
packagecom.polaris.design; /*** @author:shi * @date :Created in 2020/8/18 20:27 * @description: * @modified By: */ public classTest1 { public static voidmain(String[] args) { String sign = "JD"; ChannelRuleEnum channelRule =ChannelRuleEnum.match(sign); GeneralChannelRule rule =channelRule.channel; rule.process(); } }
转自:https://mp.weixin.qq.com/s/faQ3yWYM0swfDoEmtwa3nQ