[英]Java - Generalize different classes, similar methods (without changing the subclasses?)
Not sure if this is possible, but I have a case in which two interfaces have the same method. 不确定这是否可行,但我有一个案例,其中两个接口具有相同的方法。 These are interfaces that are given, so I can't change them.
这些是给定的接口,所以我不能改变它们。
Given Interfaces 给定接口
interface SomeGivenService {
boolean authenticate(String username, String password);
Object someSpecialMethod(Object param);
}
interface AnotherGivenService {
boolean authenticate(String username, String password);
String aGreatMethod();
String sayHello();
}
To consume this service, I created a class and did some handling in case this service throws an error. 为了使用这个服务,我创建了一个类,并在此服务抛出错误的情况下进行了一些处理。
class SomeGivenServiceConsumer {
SomeGivenService a;
public SomeGivenServiceConsumer(SomeGivenService a) {
this.a = a;
}
public authenticate(MyUserPassBean bean) {
try {
a.authenticate(bean.username, bean.password);
} catch (Exception e) {
throw new MyException();
}
...
}
}
class AnotherGivenServiceConsumer {
AnotherGivenService a;
public AnotherGivenServiceConsumer(AnotherGivenService a) {
this.a = a;
}
public authenticate(MyUserPassBean bean) {
try {
a.authenticate(bean.username, bean.password);
} catch (Exception e) {
throw new MyException();
}
...
}
}
Is it possible to avoid this code duplication in my consumers? 我的消费者可以避免这种代码重复吗? I probably will have many of them and wanted to avoid this duplicated code.
我可能会有很多,并希望避免这些重复的代码。 I initially thought of changing my consumer to receive an interface that implements this authentication, but as I can't change the given interfaces, not sure if this is even possible.
我最初想过更改我的使用者以接收实现此身份验证的接口,但由于我无法更改给定的接口,因此不确定这是否可行。
Is it possible to have a "Generic interface which has a method?" 是否可以拥有“具有方法的通用接口?” or use some design pattern?
或使用一些设计模式? Any ideas?
有任何想法吗? What I was trying:
我在尝试什么:
class AnotherGivenServiceConsumer {
AnotherGivenService a;
GivenServiceAuthenticable b;
public AnotherGivenServiceConsumer(AnotherGivenService a,
GivenServiceAuthenticable b) {
this.a = a;
this.b = b;
}
public authenticate(MyUserPassBean bean) throws MyException {
return b.authenticate(bean.username, bean.password);
}
}
interface GivenServiceAuthenticable<T> {
boolean authenticate(T givenService, MyUserPassBean bean);
}
class GivenServiceAuthenticableImpl<T> implements GivenServiceAuthenticable<T> {
boolean authenticate(T givenService, MyUserPassBean bean) {
try {
//this won't compile as it's a generic class..
t.authenticate(bean.username, bean.password);
} catch (Exception e) {
throw new MyException();
}
...
}
}
Other problem is how to instantiate this object if I can't change it to implement my new objects? 其他问题是如果我无法更改它以实现我的新对象如何实例化此对象?
You can use the template pattern to implement the common functionality in a base class, while delegating the single varying line to subclasses: 您可以使用模板模式在基类中实现通用功能,同时将单个变化行委托给子类:
abstract class ConsumerBase {
public void authenticate(MyUserPassBean bean) {
try {
authenticate(bean.username, bean.password);
} catch (Exception e) {
throw new MyException();
}
//...
}
protected abstract boolean authenticate(String username, String password);
}
class SomeGivenServiceConsumer extends ConsumerBase {
SomeGivenService a;
public SomeGivenServiceConsumer(SomeGivenService a) {
this.a = a;
}
@Override
protected boolean authenticate(String username, String password) {
return a.authenticate(username, password);
}
}
class AnotherGivenServiceConsumer extends ConsumerBase {
AnotherGivenService a;
public AnotherGivenServiceConsumer(AnotherGivenService a) {
this.a = a;
}
@Override
protected boolean authenticate(String username, String password) {
return a.authenticate(username, password);
}
}
What you need to do is to use inheritance. 你需要做的是使用继承。 Then deal with the error in the superclass ie
然后处理超类中的错误即
class SomeGivenServiceConsumer {
SomeGivenService a;
AnotherGivenService b;
public SomeGivenServiceConsumer(SomeGivenService a) {
this.a = a;
try{
authenticate(MyUserPassBean);
}catch (Exception e) {
System.out.println("Thrown exception has been caught : "+e.getMessage());
}
}
public authenticate(MyUserPassBean bean) throws MyException {
//your implementation here
}
}
class AnotherGivenServiceConsumer extends SomeGivenServiceConsumer{
public AnotherGivenServiceConsumer(AnotherGivenService b) {
super(someGivenService);//construct superclass
authenticate(MyUserPassBean);//call subclass authenticate method
}
@override
public authenticate(MyUserPassBean bean) throws MyException {
super.authenticate(MyUserPassBean);//call superclass method
//your implementation here
}
}
then you construct the subclass: 然后你构造子类:
public static void main(String[] args) {
new AnotherGivenServiceConsumer(AnotherGivenService);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.