// demo/src/main/java/com/yrunz/designpattern/monitor/config/InputConfig.javapublic abstract class InputConfig implements Config {protected String name;protected InputType type;protected Context ctx;// 子类实现具体加载逻辑,支持yaml和json的加载方式@Overridepublic abstract void load(String conf);...}// demo/src/main/java/com/yrunz/designpattern/monitor/config/FilterConfig.javapublic abstract class FilterConfig implements Config {protected List
最后再实现具体的基于json和yaml的子类:
// json方式加载Config子类目录:src/main/java/com/yrunz/designpattern/monitor/config/jsonpublic class JsonInputConfig extends InputConfig{...}public class JsonFilterConfig extends FilterConfig{...}public class JsonOutputConfig extends OutputConfig{...}public class JsonPipelineConfig extends PipelineConfig{...}// yaml方式加载Config子类目录:src/main/java/com/yrunz/designpattern/monitor/config/yamlpublic class YamlInputConfig extends InputConfig{...}public class YamlFilterConfig extends FilterConfig{...}public class YamlOutputConfig extends OutputConfig{...}public class YamlPipelineConfig extends PipelineConfig{...}
因为涉及到从配置到对象的实例化过程,自然会想到使用***工厂模式***来创建对象 。另外因为、、和都实现了接口,我们也很容易想到定义一个接口来表示“插件工厂”这一抽象,具体的插件工厂再实现该接口:
// 插件工厂接口,根据配置实例化插件public interface PluginFactory {Plugin create(Config config);}// input插件工厂public class InputPluginFactory implements PluginFactory {...@Overridepublic InputPlugin create(Config config) {InputConfig conf = (InputConfig) config;try {Class> inputClass = Class.forName(conf.type().classPath());InputPlugin input = (InputPlugin) inputClass.getConstructor().newInstance();input.setContext(conf.context());return input;} ...}}// filter插件工厂public class FilterPluginFactory implements PluginFactory {...@Overridepublic FilterPlugin create(Config config) {FilterConfig conf = (FilterConfig) config;FilterChain filterChain = FilterChain.empty();String name = "";try {for (FilterConfig.Item item : conf.items()) {name = item.name();Class> filterClass = Class.forName(item.type().classPath());FilterPlugin filter = (FilterPlugin) filterClass.getConstructor().newInstance();filterChain.add(filter);}} ...}}// output插件工厂public class OutputPluginFactory implements PluginFactory {...@Overridepublic OutputPlugin create(Config config) {OutputConfig conf = (OutputConfig) config;try {Class> outputClass = Class.forName(conf.type().classPath());OutputPlugin output = (OutputPlugin) outputClass.getConstructor().newInstance();output.setContext(conf.context());return output;} ...}}// pipeline插件工厂public class PipelineFactory implements PluginFactory {...@Overridepublic Pipeline create(Config config) {PipelineConfig conf = (PipelineConfig) config;InputPlugin input = InputPluginFactory.newInstance().create(conf.input());FilterPlugin filter = FilterPluginFactory.newInstance().create(conf.filter());OutputPlugin output = OutputPluginFactory.newInstance().create(conf.output());...}}
- 【Go实现】实践GoF的23种设计模式:SOLID原则
- 我的哔哩哔哩视频小工具
- 上海12月份的温度大概是多少 上海12月份冷不冷
- 冬天气温一般多少度重庆 重庆过冬的方法
- 亚运会的简单介绍 亚运会几年一次
- 梅子青 梅子青的寓意
- 今天给大家介绍几个基于三维重建的开放云平台Altizure
- 12月份上海的天气穿什么衣服 12月上海天气冷不冷
- win7计算机策略可以重置,win7组策略恢复初始设置的方法
- 华凌冰箱和美的冰箱有什么区别 美的冰箱好还是华凌冰箱好