gpt4 book ai didi

java - 如何在不将我的类绑定(bind)到特定实现的情况下获得实现继承的好处?

转载 作者:行者123 更新时间:2023-11-30 11:42:17 24 4
gpt4 key购买 nike

我正在开发一个基于另一个开发人员编写的类(我没有源代码)的应用程序。

我希望使用上述类的所有功能,但也想用其他功能扩展它。通常要实现这一点,我会定义一个接口(interface) (MyInterface) 并从我自己的 (MyClass) 扩展外部类 (TheirClass),同时实现MyInterface

public interface TheirClassInterface {
public void theirMethod1();
public void theirMethod2();
}

public class TheirClass implements TheirClassInterface {
public void theirMethod1() { ... }
public void theirMethod2() { ... }
}

public class TheirOtherClass {
public void theirOtherMethod1(TheirClassInterface o) { ... }
}

public interface MyInterface() {
public void myMethod1();
}

public class MyClass extends TheirClass implements MyInterface {
public void myMethod1() { ... }
}

public class MyNewClass extends MyClass {
public void MyNewClassMethod() { ... }
}

问题因以下事实而变得复杂:

  1. 我现在想创建一个新类 (MyNewClass),为 MyClass 添加额外的功能,但我不希望我的代码依赖于 TheirClass
  2. 我希望能够将我的类用作 TheirOtherClass 方法的参数。

为了解决这个问题,我重构了我的代码,改为使用组合而不是继承和实现 TheirClassInterface。这可行,但需要我实现许多方法并将它们委托(delegate)给 theirClassObject(实际上 TheirClassInterface 包含大量方法)。

public interface TheirClassInterface {
public void theirMethod1();
public void theirMethod2();
}

public class TheirClass implements TheirClassInterface {
public void theirMethod1() { ... }
public void theirMethod2() { ... }
}

public class TheirOtherClass {
public void theirOtherMethod1(TheirClassInterface o) { ... }
}

public interface MyInterface() {
public void myMethod1();
}

public class MyClass implements TheirClassInterface, MyInterface {
private TheirClass theirClassObject;

public void myMethod1() { ... }
public void theirMethod1() { theirClassObject.theirMethod1(); }
public void theirMethod2() { theirClassObject.theirMethod2(); }
}

public class MyNewClass extends MyClass {
public void MyNewClassMethod() { ... }
}

我的问题是我的方法在这种情况下是否合适,以及是否可以改进它,因为在我看来我的代码使用过多的委托(delegate)来完成工作。

非常感谢任何人对此提供的任何指导。

丹尼

最佳答案

首先,由于 java 是一种强类型的单继承语言,您无法逃避委托(delegate)。

但是您可以通过使用代理和反射的肮脏小技巧来避免编写大量委托(delegate)代码。代码如下

public interface Interface1 {
void m1();
}
public interface Interface2 {
void m2();
}
public class Class1 implements Interface1 {
public void m1() {
System.out.println(1);
}

}
public class Class2 implements Interface2 {
public void m2() {
System.out.println(2);
}
}
public interface MixinInterface extends Interface1, Interface2 {

}

奇迹就是这样发生的

package j.with.pseudo.multiple.inheritance;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MixinBuilder {

public static Object buildMixed(Class _interface, Object... impls){
InvocationHandler h = new MixinHandler(_interface.getInterfaces(), impls);
return Proxy.newProxyInstance(MixinBuilder.class.getClassLoader(),
new Class[]{_interface}, h);
}

public static void main(String[] args) {
Class1 o1 = new Class1();
Class2 o2 = new Class2();
MixinInterface almost_like_multiple_inheritance_guy =
(MixinInterface) buildMixed(MixinInterface.class, o1, o2);
almost_like_multiple_inheritance_guy.m1();
almost_like_multiple_inheritance_guy.m2();
}

private static class MixinHandler implements InvocationHandler{

private Class[] interfaces;
private Object[] impls;

public MixinHandler(Class[] interfaces, Object[] impls) {
this.interfaces = interfaces;
this.impls = impls;
}

public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
int i=0;
for(Class _interface : interfaces){
if(method.getDeclaringClass().isAssignableFrom(_interface)){
return method.invoke(impls[i], args);
}
i++;
}
// TODO Auto-generated method stub
throw new RuntimeException("Method not found: "+method);
}

}
}

很酷吧? :-)

关于java - 如何在不将我的类绑定(bind)到特定实现的情况下获得实现继承的好处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11796458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com