实践GoF的23种设计模式:SOLID原则( 七 )


// 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 items;// 子类实现具体加载逻辑,支持yaml和json的加载方式@Overridepublic abstract void load(String conf);...}// demo/src/main/java/com/yrunz/designpattern/monitor/config/OutputConfig.javapublic abstract class OutputConfig implements Config {protected String name;protected OutputType type;protected Context ctx;// 子类实现具体加载逻辑,支持yaml和json的加载方式@Overrideabstract public void load(String conf);...}// demo/src/main/java/com/yrunz/designpattern/monitor/config/PipelineConfig.javapublic abstract class PipelineConfig implements Config {protected String name;protected PipelineType type;protected final InputConfig inputConfig;protected final FilterConfig filterConfig;protected final OutputConfig outputConfig;// 子类实现具体加载逻辑,支持yaml和json的加载方式@Overridepublic abstract void load(String conf);}
最后再实现具体的基于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());...}}