Java - 概括不同的类,相似的方法(不改变sub类?)
Java - Generalize different classes, similar methods (without changing the subclasses?)
不确定这是否可行,但我遇到过两个接口具有相同方法的情况。这些是给定的接口,所以我无法更改它们。
给定接口
interface SomeGivenService {
boolean authenticate(String username, String password);
Object someSpecialMethod(Object param);
}
interface AnotherGivenService {
boolean authenticate(String username, String password);
String aGreatMethod();
String sayHello();
}
为了使用此服务,我创建了一个 class 并做了一些处理以防此服务抛出错误。
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();
}
...
}
}
是否可以在我的消费者中避免这种代码重复?我可能会有很多,并且想避免这种重复的代码。
我最初想更改我的消费者以接收实现此身份验证的接口,但由于我无法更改给定的接口,因此不确定这是否可能。
是否可以使用 "Generic interface which has a method?" 或使用某种设计模式?有任何想法吗?
我在尝试什么:
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();
}
...
}
}
另一个问题是如果我不能改变它来实现我的新对象,如何实例化这个对象?
你需要做的就是使用继承。然后处理超类中的错误即
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
}
}
然后你构造子类:
public static void main(String[] args) {
new AnotherGivenServiceConsumer(AnotherGivenService);
}
您可以使用 template pattern 在基础 class 中实现通用功能,同时将单个可变行委托给 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);
}
}
不确定这是否可行,但我遇到过两个接口具有相同方法的情况。这些是给定的接口,所以我无法更改它们。
给定接口
interface SomeGivenService {
boolean authenticate(String username, String password);
Object someSpecialMethod(Object param);
}
interface AnotherGivenService {
boolean authenticate(String username, String password);
String aGreatMethod();
String sayHello();
}
为了使用此服务,我创建了一个 class 并做了一些处理以防此服务抛出错误。
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();
}
...
}
}
是否可以在我的消费者中避免这种代码重复?我可能会有很多,并且想避免这种重复的代码。 我最初想更改我的消费者以接收实现此身份验证的接口,但由于我无法更改给定的接口,因此不确定这是否可能。
是否可以使用 "Generic interface which has a method?" 或使用某种设计模式?有任何想法吗? 我在尝试什么:
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();
}
...
}
}
另一个问题是如果我不能改变它来实现我的新对象,如何实例化这个对象?
你需要做的就是使用继承。然后处理超类中的错误即
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
}
}
然后你构造子类:
public static void main(String[] args) {
new AnotherGivenServiceConsumer(AnotherGivenService);
}
您可以使用 template pattern 在基础 class 中实现通用功能,同时将单个可变行委托给 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);
}
}