简体   繁体   English

实现具有null属性的抽象类

[英]Implementing an abstract class with null properties

In the context of my current project, I am asked to implement something like this: 在我当前的项目中,要求我实现以下内容:

interface I {

    getA():String;
    setA(String);
    getB():String;
    setB(String);
    ...
    getE():String;
    setE(String);
}

class I1 implements I {

    // These are basic getters/setters for A, B and C.
    getA(){}
    setA(String){}
    getB():String{}
    setB(String){}
    getC():String{}
    setC(String){}

    @Deprecated
    getD(){
        return null;
    }

    @Deprecated
    setD(String d) {
        // Does nothing.
    }

    @Deprecated
    getE(){
        return null;
    }

    @Deprecated
    setE(String e) {
        // Does nothing.
    }

}


class I2 implements I {

    // These are basic getters/setters for C, D and E.
    getC(){}
    setC(String){}
    getD():String{}
    setD(String){}
    getE():String{}
    setE(String){}

    @Deprecated
    getA(){
        return null;
    }

    @Deprecated
    setA(String a) {
        // Does nothing.
    }

    @Deprecated
    getB(){
        return null;
    }

    @Deprecated
    setB(String b) {
        // Does nothing.
    }

}

So, basically, only the C property makes sense for both of the implementations. 因此,基本上,只有C属性对于这两种实现都有意义。

What is the point to have the getters/setters for A, B, D and E in the interface? 在接口中具有A,B,D和E的吸气剂/设置剂有什么意义? Why just not keep the getter/setter for C, so we don't have to implement the useless getters/setters in the implementations? 为什么不保留C的getter / setter,而不必在实现中实现无用的getter / setter呢?

The argument that I am told is: we are doing like this because we are used to do it this way. 我被告知的论点是:我们之所以这样做,是因为我们习惯于这样做。

So when we process some objects of type I (the interface) in our methods, we have to make null checks all the time. 因此,当我们在方法中处理某些类型为I(接口)的对象时,我们必须始终进行null检查。

What is the point to have the getters/setters for A, B, D and E in the interface? 在接口中具有A,B,D和E的吸气剂/设置剂有什么意义? Why just not keep the getter/setter for C, so we don't have to implement the useless getters/setters in the implementations? 为什么不保留C的getter / setter,而不必在实现中实现无用的getter / setter呢?

As you say, it does look like a bad design. 就像您说的那样,它确实看起来像是一个不良设计。 It seems there should be a base interface that has get/set C, and two sub-interfaces with one containing get/set A/B, and the other containing get/set D/E. 似乎应该有一个具有get / set C的基本接口,以及两个子接口,其中一个包含get / set A / B,另一个包含get / set D / E。

The argument that I am told is: we are doing like this because we are used to do it this way. 我被告知的论点是:我们之所以这样做,是因为我们习惯于这样做。

This argument is common for legacy enterprise applications to keep backward compatibility with existing apps clients may have built on top of an API. 对于旧版企业应用程序,此参数很常见,以保持与客户端可能已在API之上构建的现有应用程序的向后兼容性。 It makes it easier to convince clients to upgrade if their existing apps won't break. 如果他们的现有应用程序不会中断,则可以更容易地说服客户进行升级。

However, you might want to get more detail to make sure the case for doing your app like this is a valid one - "because we used to do it this way" sounds pretty evasive to me, and doesn't really help evaluate other options that may meet the requirements but with a better architecture. 但是,您可能想要获得更多详细信息,以确保像这样制作应用程序的情况是有效的-“因为我们以前是这样做的”对我来说似乎很规避,并且并没有真正帮助评估其他选项可能满足要求,但架构更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM