CTO 写的代码,真是绝了
解决思路
public abstract class GeneralChannelRule {
public abstract void process();
}
public class TencentChannelRule extends GeneralChannelRule
@Override
public void process() {
// Tencent处理逻辑
}
}
public class TouTiaoChannelRule extends GeneralChannelRule
@Override
public void process() {
// TouTiao处理逻辑
}
}
public enum ChannelRuleEnum {
/**
* 头条
*/
TOUTIAO("TOUTIAO"),
/**
* 腾讯
*/
TENCENT("TENCENT"),
;
....
}
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();
}
新的思路
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;
}
}
public static void main(String[] args) {
String sign = "TOUTIAO";
ChannelRuleEnum channelRule = ChannelRuleEnum.match(sign);
GeneralChannelRule rule = channelRule.channel;
rule.process(sign);
}
后
- END -
相关文章