多个接口以Java扩展 [英] Multiple Interfaces extending in Java
问题描述
我需要在Java中实现具有多个接口继承的Facade设计模式,或者是具有Bridge设计模式的正确Facade。我知道这是可能的,因为我把它作为一个系统的一部分看到了,但我不记得实现得很好。
这是我到目前为止的实现:
I need to implement a Facade design pattern with multiple inheritance of interfaces in Java or to be correct Facade with Bridge design pattern. I know this is possible because I saw it in as a part of one system, but I don't remember the implementation very well.
Here is my implementation so far:
public interface IOne {
public void methodOneIOne();
public void methodTwoIOne();
}
及其实施:
public class One implements IOne {
@Override
public void methodOneIOne() {
System.out.println("methodOneIOne()");
}
@Override
public void methodTwoIOne() {
System.out.println("methodTwoIOne()");
} }
public interface ITwo {
public void methodOneITwo();
public void methodTwoITwo();
}
public class Two implements ITwo {
@Override
public void methodOneITwo() {
System.out.println("methodOneITwo()");
}
@Override
public void methodTwoITwo() {
System.out.println("methodTwoITwo()");
}}
门面:
public interface IFacade extends IOne, ITwo {}
所以,从这里我不知道去哪里。如果我创建一个实现IFacade的类,那么它将需要实现所有方法,这不是我想要的。
So, from here I don't know where to go. If I create a class that implements IFacade, then it will be required to implement all of the methods, and this isn't what I want.
推荐答案
你真正试图用门面课做什么?如果你只是试图通过大多数方法,可能修改一些,然后动态代理可能就是您想要的。
What are you actually trying to do with your facade class? If you are just trying to pass through most methods, possibly modifying a few then a dynamic proxy might be what you want.
否则你实际上必须实现 IFacade
的所有方法,因为Java不允许你使用多个继承,所以你不能扩展一个
和两个
。
Otherwise you would in fact have to implement all methods of IFacade
, as Java does not allow you to use multiple inheritance, so you could not extend both One
and Two
.
这篇关于多个接口以Java扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!