优化具有不同参数的重复函数
Optimize duplicated function with different parameter
我有两个完全相同的函数和一个以 TypeX
作为参数的不同函数。所有 TypeX
都具有相同的父级 class Type
。虚拟代码如下:
public void Append(TypeA item) { //same code }
public void Append(TypeB item) { //same code }
public void Append(TypeC item) { //different code }
请问有什么好的方法可以优化这些功能吗?我的代码需要根据 class 类型选择正确的函数,所以我不能在这里使用父 class 或泛型类型,因为这会影响 TypeC
的参与。
最好的是public void Append(TypeA item || TypeB item)
,但当然没有这样的东西可用。有什么想法吗?
要调用的方法在编译时根据对象的声明类型(而非实际类型)select编辑。将 TypeA 和 TypeB 的 Append 合并到一个参数为 Type item
的方法中是安全的——它与当前代码没有区别。
但是如果你想在实际类型上select,你需要使用instanceof
。或者你在 returns 类型上创建一个方法,无论你在 append
方法中需要什么。
public void append(Type item) {
if (item instanceof TypeC) {
// Do TypeC-specific stuff
} else {
// Do stuff for TypeA, TypeB, and any other subtype of Type
}
}
或清洁剂,如果唯一的区别在于您从物品中获得的东西:
public class Type {
public X getWhateverYouWantToAppend() {
// Return stuff for TypeA, TypeB, and any other subtype of Type
}
}
public class TypeC extends Type {
public X getWhateverYouWantToAppend() {
// Return stuff for TypeC specifically
}
}
public class YourOtherClass {
public void append(Type item) {
X thingToAppend = item.getWhateverYouWantToAppend();
// Do the appending
}
}
它在 java 中很简单,您可以使用方法重载
public class example{
public static void print(String s) {
System.out.println("string - "+s);
}
public static void print(int q) {
System.out.println("number - "+q);
}
public static void main(String[] args) {
print(12);
print("welcome");
}
}
输出
number - 12
string - welcome
鉴于@erwin-bolwidt提供的解决方案可行,我建议您也考虑
private void baseAppend (TypeParent item) { //same code }
public void Append(TypeA item) { baseAppend (item); }
public void Append(TypeB item) { baseAppend (item); }
public void Append(TypeC item) { //different code }
此方法允许松散的耦合,并有可能在未来进行专门的日志记录和扩展
您可以为 TypeA
和 TypeB
创建父级 class。然后使用这个类型作为重载函数的参数。
这样可以避免重复代码。
另一种方法是让这些 classes 实现一些接口,然后将此接口用作重载函数的参数。
第三,可以使用泛型,将实现委托给相应的函数。
我有两个完全相同的函数和一个以 TypeX
作为参数的不同函数。所有 TypeX
都具有相同的父级 class Type
。虚拟代码如下:
public void Append(TypeA item) { //same code }
public void Append(TypeB item) { //same code }
public void Append(TypeC item) { //different code }
请问有什么好的方法可以优化这些功能吗?我的代码需要根据 class 类型选择正确的函数,所以我不能在这里使用父 class 或泛型类型,因为这会影响 TypeC
的参与。
最好的是public void Append(TypeA item || TypeB item)
,但当然没有这样的东西可用。有什么想法吗?
要调用的方法在编译时根据对象的声明类型(而非实际类型)select编辑。将 TypeA 和 TypeB 的 Append 合并到一个参数为 Type item
的方法中是安全的——它与当前代码没有区别。
但是如果你想在实际类型上select,你需要使用instanceof
。或者你在 returns 类型上创建一个方法,无论你在 append
方法中需要什么。
public void append(Type item) {
if (item instanceof TypeC) {
// Do TypeC-specific stuff
} else {
// Do stuff for TypeA, TypeB, and any other subtype of Type
}
}
或清洁剂,如果唯一的区别在于您从物品中获得的东西:
public class Type {
public X getWhateverYouWantToAppend() {
// Return stuff for TypeA, TypeB, and any other subtype of Type
}
}
public class TypeC extends Type {
public X getWhateverYouWantToAppend() {
// Return stuff for TypeC specifically
}
}
public class YourOtherClass {
public void append(Type item) {
X thingToAppend = item.getWhateverYouWantToAppend();
// Do the appending
}
}
它在 java 中很简单,您可以使用方法重载
public class example{
public static void print(String s) {
System.out.println("string - "+s);
}
public static void print(int q) {
System.out.println("number - "+q);
}
public static void main(String[] args) {
print(12);
print("welcome");
}
}
输出
number - 12
string - welcome
鉴于@erwin-bolwidt提供的解决方案可行,我建议您也考虑
private void baseAppend (TypeParent item) { //same code }
public void Append(TypeA item) { baseAppend (item); }
public void Append(TypeB item) { baseAppend (item); }
public void Append(TypeC item) { //different code }
此方法允许松散的耦合,并有可能在未来进行专门的日志记录和扩展
您可以为 TypeA
和 TypeB
创建父级 class。然后使用这个类型作为重载函数的参数。
这样可以避免重复代码。
另一种方法是让这些 classes 实现一些接口,然后将此接口用作重载函数的参数。
第三,可以使用泛型,将实现委托给相应的函数。