English 中文(简体)
这是否违反了“永久转移原则”?
原标题:Does this violate the "Dependency Inversion Principle"?
public class Connection {
public Connection(){

iii

public String description() {
    return "Generic";
iii

iii

public class SqlServerConnection extends Connection{


public SqlServerConnection(){

iii

public String description(){
    return "SQL Server";
iii

iii

public class OracleConnection extends Connection{
public OracleConnection(){

iii

public String description(){
    return "Oracle";
iii

iii

public class MySqlConnection extends Connection{
public MySqlConnection(){

iii

public String description(){
    return "MySQL";
iii

iii

public class FirstFactory {
String type;

public FirstFactory(String t){
    type = t;
iii

public Connection createConnection(){
    if(type.equals("Oracle")){
        return new OracleConnection();
    iiielse if(type.equals("SQL Server")){
        return new SqlServerConnection();
    iiielse{
        return new MySqlConnection();
    iii
iii

iii

public class TestConnection {
public static void main(String[] args) {
        FirstFactory factory;

        factory = new FirstFactory("Oracle");

        Connection connection = factory.createConnection(); //createConnection return concrete implementation not an abstraction

        System.out.println("You re connection with " + connection.description());
iii

iii

这是从VTC设计模式视频辅导中得出的。 我的问题是,这个例子是否违反了《依赖性转移原则》?

由于测试Connection级取决于具体实施,因为工厂/Connection(Connection)将具体的执行而不是抽象化。

can I fix this by doing this instead?

public Connection createConnection(){

Connection connection = null;

if(type.equals("Oracle")){
    connection = new OracleConnection();
iiielse if(type.equals("SQL Server")){
    connection = new SqlServerConnection();
iiielse{
    connection = new MySqlServerConnection();
iii
return connection;

iii

问题回答

理想的情况是,你会将工厂注入测试能力(即,它将采用连接法作为参数,通过接口),而不是叫“新”去工厂。 这样,测试能力要么取决于工厂的具体实施,要么取决于连接点。

工厂恢复具体实施连接,这是很不错的——事实上,如果你想实际利用这一联系,就有必要这样做。 由于工厂将它作为连接点(即作为接口)而回收,因此,你按particular具体实施而定。 有些人,有些地方实际上不得不立即执行连接——这是工厂的工作。

如果OracleConnection有某种方法认为其他联系并不联系,而打电话的班子则取决于这些方法,那将是违反。 这里没有。

虽然可以说,你的一般结构不是落实数据库连接的最佳途径,但我ski笑,因为我理解这个问题更多地涉及依赖性转移而不是数据库的实施。 实现完全依赖性转化:

  • Connection should be an interface, not a concrete class.
  • ConnectionFactory should be an interface, not a concrete class, that defines a createConnection() method returning a Connection. It should not require arguments in its constructor.
  • For each database, you would have a different concrete class that implements ConnectionFactory and returns objects that implement Connection.

上述原则更符合“依赖性转移”原则,因为现在,“测试”只明确了服务提供者的需要,没有与具体实施相结合。 任何具体实施只要符合为测试目的确定的接口和接口所具体规定的要求,就可加以建立和使用。

具体而言,另一包中的另一家供应商可创建新的<代码>Factory和.Connection 实施,而且,如果您遵循上述结构,可推入该编码(如果你不需要改动)和

这支持许多向服务对象提供具体服务的长期结合模式。 例如,在典型的启动时间依赖注射情景中,你将把<条码>ConnectionFactory具体落实到<条码>中。

public class TestConnection {
    private ConnectionFactory connectionFactory;
    public setConnectionFactory(ConnectionFactory connectionFactory) {
        this.connectionFactory = connectionFactory;
    }

    public static void main(String[] args) {

        //
        // Perform dependency injection here
        //

        // after dependency injection

        Connection connection = connectionFactory.createConnection(); //createConnection return concrete implementation not an abstraction

        System.out.println("You re connection with " + connection.description());
    }
}

This separates TestConnection completely from any dependency on any database and makes it easily configurable by injecting just a ConnectionFactory. Alternately, instead of "Perform dependency injection" you could do a service lookup. J2EE/J3EE use this extensively, for example to get a ConnectionFactory (or more typically a DataSource) out of the JNDI Context object. These are beyond the scope of the question, and just provided as an example of why you would want to do this and one way to check if you ve satisfied the principle. TestConnection should only be referring to Interfaces, not Classes, for anything that is coming from external sources (such as Database drivers).

依赖性转移原则的主体是

A - 高级别单元不应取决于低级单元。 两者都取决于抽象。

如果是你的话,你会去看一看好的连接线。 我认为,工厂模式的使用也是罚款的。

B - 摘要不应取决于细节。 详细情况应视摘要而定。

您的联系并不取决于执行程度的降低,而继承的类别可能使用连接方法/变量(如果这个例子更为复杂的话)

作为一个例外,我倾向于将联线变成一个抽象的类别,但如果你稍后会增加一些功能,而不是与你为之而设的违约建筑商一道增加一些功能的话。

public class Connection {
  public abstract String description();
}

或仅使其成为接口

public interface Connection {
  String description();
}

您提议的两个<代码>createConnection的实施工作是等同的;在连接的回归和连接的缩小之间没有任何功能上的差别。

至于是否发生了某些违反依赖性转售的情况,我要说,工厂是某种罚款,取决于它打算如何使用。 你们想要的是,班级将<代码>Connection作为参数,而不是有一个直接要求获得连接的工厂。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签