Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我们可以称以下模式为DI吗?_Java_Spring_Design Patterns - Fatal编程技术网

Java 我们可以称以下模式为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

请原谅我的无知。我对Java还很陌生。我学习过关于春迪的知识。我们提供的任何xml或基于注释的配置都将用于以声明方式注入依赖项。但是,我认为我们不能动态地改变依赖关系

因此,我创建了以下示例代码,它可以通过扩展类路径来提供依赖关系(扩展类路径的代码未显示)。我们能称之为DI吗

标记接口

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(我建议您阅读):

基本的选择是在服务定位器和依赖注入之间。第一点是,这两种实现都提供了原始示例中缺少的基本解耦——在这两种情况下,应用程序代码都独立于服务接口的具体实现。这两种模式之间的重要区别在于如何将实现提供给应用程序类。对于服务定位器,应用程序类通过给定位器的消息显式地请求它。使用注入时,没有显式的请求,服务出现在应用程序类中-因此控制反转

因为与大多数模式一样,它取决于您的需求。因此,我将它们称为权衡

在这种情况下,它们是主要优势(解耦)的结果。当设计模块化时,请记住最小化耦合,同时最大化内聚。因此,请确保您没有创建太细粒度的模块

要列出一些权衡:

  • 这会使代码不那么清晰
  • 使维护更加困难
  • 您丢失了一些编译时检查,这意味着潜在的运行时错误
我的建议是,只有当您有几个实现的稳定接口,或者至少您确信很快会有另一个实现时,才使用它。只是不要这么做,以防万一有一天你的接口可能会有另一个实现。如果您这样做,您很可能会发现自己过度工程化,而代价是编译时检查或IDE辅助

这是服务定位器和依赖注入模式的主要共同点。

这类似于DI,但更简单(您并不是真正注入DEP,只是动态地查找它们)。几个月前,我已经手推过类似的东西并使用了一段时间,我强烈建议尽快切换到轻型DI解决方案(可能是Guice)。坚持构造函数(param)注入,你不会搞得一团糟,但这不值得你自己去做。