[英]A function that are existing only in one concrete implementation of an interface
I have a interface with one method:我有一个带有一种方法的接口:
public interface MyInterface {
public void doSomething();
}
And multiple concrete implementations of 'MyInterface':以及“MyInterface”的多个具体实现:
Implementation1:实施1:
public class Implementation1 implements MyInterface {
@Override
public void doSomething() {
// DO something for implementation1
}
}
Implementation2:实施2:
public class Implementation2 implements MyInterface {
@Override
public void doSomething() {
// DO something for implementation2
}
}
and implementation3:和实施3:
public class Implementation3 implements MyInterface {
@Override
public void doSomething() {
// DO something for implementation3
}
public void doSomething(int something) {
// DO something for implementation3
}
}
if I want to access 'doSomething(10)' with 'MyInterface' type I need to add this function to 'MyInterface' and other implementation ('Implementation1' and 'Implementation2' in my example) must to implement this function but do nothing because I don't need this function in 'Implementation1' and 'Implementation2'.如果我想使用 'MyInterface' 类型访问 'doSomething(10)' 我需要将此 function 添加到 'MyInterface' 和其他实现(在我的示例中为 'Implementation1' 和 'Implementation2' )必须实现此 ZC1C425268E68385D1AB4ZZC17A94F 但什么也不做,因为我在“实施1”和“实施2”中不需要这个function。
My question is: How to proceed in this case?我的问题是:在这种情况下如何进行? implement 'doSomething(int something)' and let them to do nothing in 'Implementation1' and 'Implementation2' only for 'Implementation3' or to cast instance variable to 'Implementation3' and in this way create a dependency to concrete type 'Implementation3'?
实现'doSomething(int something)'并让他们在'Implementation1'和'Implementation2'中只为'Implementation3'或将实例变量转换为'Implementation3'并以这种方式创建对具体类型'Implementation3'的依赖关系? I want to specify that I don't want to create dependencies on concrete implementations because I want to let the interfaces communicate with each other.
我想指定我不想创建对具体实现的依赖,因为我想让接口相互通信。
Thanks!谢谢!
One solution is to have 2 interfaces, the first one, which you already have:一种解决方案是拥有 2 个接口,第一个是您已经拥有的:
public interface MyInterface {
void doSomething();
}
and the second which extends from the first and so it already has the parameter-less method as well as the second method overload which takes an int parameter:第二个从第一个扩展,因此它已经具有无参数方法以及采用 int 参数的第二个方法重载:
// already has the default method
public interface MyInterface2 extends MyInterface {
void doSomething(int value);
}
Then if a concrete class needs both methods, it can implement the second interface:那么如果一个具体的 class 需要这两种方法,它可以实现第二个接口:
public class Implementation3 implements MyInterface2 {
@Override
public void doSomething() {
// DO something for implementation3
}
@Override
public void doSomething(int something) {
// DO something for implementation3
}
}
Note that instances of the above class can still be used in places where MyInterface type is expected.请注意,上述 class 的实例仍然可以在需要 MyInterface 类型的地方使用。
I want to clarify the the situation.我想澄清一下情况。 You have a interface, it represents for a behavior, in your case, the MyInterface is represents for all object can doSomething with nothing input parameter.
您有一个接口,它代表一种行为,在您的情况下,MyInterface 代表所有 object 可以在没有任何输入参数的情况下做某事。 After that, you want some your objects have another behavior: doSomething with int input parameter.
之后,您希望您的某些对象具有另一种行为:带 int 输入参数的 doSomething。 You can create new interface having doSomething(int value) and only Implementation3 implements it.
您可以创建具有 doSomething(int value) 的新接口,并且只有 Implementation3 实现它。
public interface MyInterface2 {
void doSomething(int something);
}
public class Implementation3 implements MyInterface, MyInterface2 {
@Override
public void doSomething() {
// DO something for implementation3
}
@Override
public void doSomething(int something) {
// DO something for implementation3
}
}
You can use MyInterface2.doSomething(1) with all class implemented MyInterface2.您可以将 MyInterface2.doSomething(1) 与所有 class 实现的 MyInterface2 一起使用。 I hope it help.
我希望它有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.