接口使用和多态编码

时间:2018-03-11 23:30:57

标签: java

我有几节课。我想指出一点: 实现BaseDAOQueryable接口的Parcelable类。 Base 1有几个子类,我想调用方法

public static ResultSet getDataFor(Queryable queryable) throws SQLException {
    Statement statement = con.createStatement();
    return statement.executeQuery(queryable.getSelectAllQuery());
}

普遍地,Base的所有潜艇都有两条线。

但是现在这个电话看起来很丑陋,就像getDataFor(new Program())或不同的班级一样。

基本上,我的问题是有没有更好的方法来实现这种行为,而无需调用新实例。 我想尽可能保持简洁,所以随时提出问题

2 个答案:

答案 0 :(得分:1)

使$ curl http://example.com:86/DynamicsNAV80/WS/nasr/Page/Delivery -v --ntlm --negotiate -u nasr\\jason:tty5Rx53$_sj * Hostname was NOT found in DNS cache * Trying 21.55.152.170... * Connected to example.com (21.55.152.170) port 86 (#0) > GET /DynamicsNAV80/WS/nasr/Page/Delivery HTTP/1.1 > User-Agent: curl/7.38.0 > Host: example.com:86 > Accept: */* > < HTTP/1.1 401 Unauthorized < Content-Length: 0 * Server Microsoft-HTTPAPI/2.0 is not blacklisted < Server: Microsoft-HTTPAPI/2.0 < WWW-Authenticate: Negotiate < Date: Sun, 11 Mar 2018 23:20:34 GMT < * Connection #0 to host example.com left intact * Issue another request to this URL: 'http://example.com:86/DynamicsNAV80/WS/nasr/Page/Delivery' * Found bundle for host example.com: 0x5558f1e0d4e0 * Re-using existing connection! (#0) with host example.com * Connected to example.com (21.55.152.170) port 86 (#0) > GET /DynamicsNAV80/WS/nasr/Page/Delivery HTTP/1.1 > User-Agent: curl/7.38.0 > Host: example.com:86 > Accept: */* > < HTTP/1.1 401 Unauthorized < Content-Length: 0 * Server Microsoft-HTTPAPI/2.0 is not blacklisted < Server: Microsoft-HTTPAPI/2.0 * gss_init_sec_context() failed: : SPNEGO cannot find mechanisms to negotiate < WWW-Authenticate: Negotiate < Date: Sun, 11 Mar 2018 23:20:34 GMT < * Connection #0 to host example.com left intact ()成为getDataFor的实例方法。这样,它就可以访问seudo-argument BaseDAO,它将指向您将在其上执行该方法的实例。

答案 1 :(得分:1)

如果您不想修改课程,我可以在三个解决方案中思考。

使用枚举

如果您需要/需要更多控制它将收到的类型:

enum BaseType {
    PROGRAM(new Program()),
    ANOTHER(new Another());
    private BaseDAO baseDAO;
    BaseType (BaseDAO baseDAO) {
        this.baseDAO = baseDAO;
    }
    public BaseDAO getBaseDAO() {
        return baseDAO;
    }
}

public static ResultSet getDataFor(BaseType type) throws SQLException {
    Statement statement = con.createStatement();
    return statement.executeQuery(type.getBaseDAO().getSelectAllQuery());
}

使用静态导入

import static my.pack.BaseType;

getDataFor(PROGRAM);

没有静态导入

getDataFor(BaseType.PROGRAM);

使用反思

如果您不希望/需要更多控制它将收到的类型:

public static ResultSet getDataFor(Class<? extends BaseDao> type) throws SQLException {
    Statement statement = con.createStatement();
    return statement.executeQuery(type.newInstance()
        .getSelectAllQuery()); // handle exceptions
}

一样使用它
getDataFor(Program.class);

使用供应商

此外,如果您不希望/需要更多控制它将收到的类型:

public static ResultSet getDataFor(Supplier<? extends BaseDao> supplier) throws SQLException {
    Statement statement = con.createStatement();
    return statement.executeQuery(supplier.get().getSelectAllQuery());
}

一样使用它
getDataFor(Program::new); // looks good, at least to me

由于类型擦除,您无法使用泛型。如果java已经使用了泛型,你可以使用类似getDataFor<Program>()的东西,但是无法完成。