Java 我们可以称以下模式为DI吗?
请原谅我的无知。我对Java还很陌生。我学习过关于春迪的知识。我们提供的任何xml或基于注释的配置都将用于以声明方式注入依赖项。但是,我认为我们不能动态地改变依赖关系 因此,我创建了以下示例代码,它可以通过扩展类路径来提供依赖关系(扩展类路径的代码未显示)。我们能称之为DI吗 标记接口Java 我们可以称以下模式为DI吗?,java,spring,design-patterns,Java,Spring,Design Patterns,请原谅我的无知。我对Java还很陌生。我学习过关于春迪的知识。我们提供的任何xml或基于注释的配置都将用于以声明方式注入依赖项。但是,我认为我们不能动态地改变依赖关系 因此,我创建了以下示例代码,它可以通过扩展类路径来提供依赖关系(扩展类路径的代码未显示)。我们能称之为DI吗 标记接口 public interface IRule { } public interface IFooRule extends IRule { public boolean validate(Strin
public interface IRule {
}
public interface IFooRule extends IRule {
public boolean validate(String json);
}
public class FooRuleImplemetor {
private FooRuleImplemetor implementor;
private FooRuleImplemetor() {
}
public boolean validateRule(String json) {
--Business Logic--
}
public static synchronized FooRuleImplemetor getInstance() {
if (implementor == null) {
implementor = new FooRuleImplemetor();
}
return implementor;
}
}
public class RulesFactory {
public <T extends IRule> T getRuleImplementation(String clazz) {
try {
Class<?> ruleObject = Class.forName(clazz);
Method factoryMethod = ruleObject.getMethod("getInstance");
return (T) factoryMethod.invoke(null);
} catch (ClassNotFoundException e) {
logger.error("ClassNotFoundException", e);
} catch (IllegalAccessException e) {
logger.error("IllegalAccessException", e);
} catch (SecurityException e) {
logger.error("SecurityException", e);
} catch (NoSuchMethodException e) {
logger.error("NoSuchMethodException", e);
} catch (IllegalArgumentException e) {
logger.error("IllegalArgumentException", e);
} catch (InvocationTargetException e) {
logger.error("InvocationTargetException", e);
}
return null;
}
}
扩展IRule的接口
public interface IRule {
}
public interface IFooRule extends IRule {
public boolean validate(String json);
}
public class FooRuleImplemetor {
private FooRuleImplemetor implementor;
private FooRuleImplemetor() {
}
public boolean validateRule(String json) {
--Business Logic--
}
public static synchronized FooRuleImplemetor getInstance() {
if (implementor == null) {
implementor = new FooRuleImplemetor();
}
return implementor;
}
}
public class RulesFactory {
public <T extends IRule> T getRuleImplementation(String clazz) {
try {
Class<?> ruleObject = Class.forName(clazz);
Method factoryMethod = ruleObject.getMethod("getInstance");
return (T) factoryMethod.invoke(null);
} catch (ClassNotFoundException e) {
logger.error("ClassNotFoundException", e);
} catch (IllegalAccessException e) {
logger.error("IllegalAccessException", e);
} catch (SecurityException e) {
logger.error("SecurityException", e);
} catch (NoSuchMethodException e) {
logger.error("NoSuchMethodException", e);
} catch (IllegalArgumentException e) {
logger.error("IllegalArgumentException", e);
} catch (InvocationTargetException e) {
logger.error("InvocationTargetException", e);
}
return null;
}
}
实现IFooRule的类
public interface IRule {
}
public interface IFooRule extends IRule {
public boolean validate(String json);
}
public class FooRuleImplemetor {
private FooRuleImplemetor implementor;
private FooRuleImplemetor() {
}
public boolean validateRule(String json) {
--Business Logic--
}
public static synchronized FooRuleImplemetor getInstance() {
if (implementor == null) {
implementor = new FooRuleImplemetor();
}
return implementor;
}
}
public class RulesFactory {
public <T extends IRule> T getRuleImplementation(String clazz) {
try {
Class<?> ruleObject = Class.forName(clazz);
Method factoryMethod = ruleObject.getMethod("getInstance");
return (T) factoryMethod.invoke(null);
} catch (ClassNotFoundException e) {
logger.error("ClassNotFoundException", e);
} catch (IllegalAccessException e) {
logger.error("IllegalAccessException", e);
} catch (SecurityException e) {
logger.error("SecurityException", e);
} catch (NoSuchMethodException e) {
logger.error("NoSuchMethodException", e);
} catch (IllegalArgumentException e) {
logger.error("IllegalArgumentException", e);
} catch (InvocationTargetException e) {
logger.error("InvocationTargetException", e);
}
return null;
}
}
规则工厂。一个工厂级适用于几乎任何一个病毒级
public interface IRule {
}
public interface IFooRule extends IRule {
public boolean validate(String json);
}
public class FooRuleImplemetor {
private FooRuleImplemetor implementor;
private FooRuleImplemetor() {
}
public boolean validateRule(String json) {
--Business Logic--
}
public static synchronized FooRuleImplemetor getInstance() {
if (implementor == null) {
implementor = new FooRuleImplemetor();
}
return implementor;
}
}
public class RulesFactory {
public <T extends IRule> T getRuleImplementation(String clazz) {
try {
Class<?> ruleObject = Class.forName(clazz);
Method factoryMethod = ruleObject.getMethod("getInstance");
return (T) factoryMethod.invoke(null);
} catch (ClassNotFoundException e) {
logger.error("ClassNotFoundException", e);
} catch (IllegalAccessException e) {
logger.error("IllegalAccessException", e);
} catch (SecurityException e) {
logger.error("SecurityException", e);
} catch (NoSuchMethodException e) {
logger.error("NoSuchMethodException", e);
} catch (IllegalArgumentException e) {
logger.error("IllegalArgumentException", e);
} catch (InvocationTargetException e) {
logger.error("InvocationTargetException", e);
}
return null;
}
}
这种图案叫什么名字?我们能称之为依赖注入吗
更新
public interface IRule {
}
public interface IFooRule extends IRule {
public boolean validate(String json);
}
public class FooRuleImplemetor {
private FooRuleImplemetor implementor;
private FooRuleImplemetor() {
}
public boolean validateRule(String json) {
--Business Logic--
}
public static synchronized FooRuleImplemetor getInstance() {
if (implementor == null) {
implementor = new FooRuleImplemetor();
}
return implementor;
}
}
public class RulesFactory {
public <T extends IRule> T getRuleImplementation(String clazz) {
try {
Class<?> ruleObject = Class.forName(clazz);
Method factoryMethod = ruleObject.getMethod("getInstance");
return (T) factoryMethod.invoke(null);
} catch (ClassNotFoundException e) {
logger.error("ClassNotFoundException", e);
} catch (IllegalAccessException e) {
logger.error("IllegalAccessException", e);
} catch (SecurityException e) {
logger.error("SecurityException", e);
} catch (NoSuchMethodException e) {
logger.error("NoSuchMethodException", e);
} catch (IllegalArgumentException e) {
logger.error("IllegalArgumentException", e);
} catch (InvocationTargetException e) {
logger.error("InvocationTargetException", e);
}
return null;
}
}
我还想知道这种模式的潜在缺点,如果有的话。是否有经过良好测试的替代模式可供我使用?您所描述的更像是一个服务定位器,用于解决依赖关系。但是依赖项本身并没有被注入——没有控制反转,您可以通过
rulesFactory.getRuleImplementation(clazz)
调用来控制依赖项的解析
来自Martin Fowler(我建议您阅读):
基本的选择是在服务定位器和依赖注入之间。第一点是,这两种实现都提供了原始示例中缺少的基本解耦——在这两种情况下,应用程序代码都独立于服务接口的具体实现。这两种模式之间的重要区别在于如何将实现提供给应用程序类。对于服务定位器,应用程序类通过给定位器的消息显式地请求它。使用注入时,没有显式的请求,服务出现在应用程序类中-因此控制反转
因为与大多数模式一样,它取决于您的需求。因此,我将它们称为权衡
在这种情况下,它们是主要优势(解耦)的结果。当设计模块化时,请记住最小化耦合,同时最大化内聚。因此,请确保您没有创建太细粒度的模块
要列出一些权衡:
- 这会使代码不那么清晰
- 使维护更加困难
- 您丢失了一些编译时检查,这意味着潜在的运行时错误