如何将两个相似的方法合二为一
How to refactor two similar methods in one
只有一种方法。在一种情况下,我需要 return 一种类型的变量。在另一种情况下,我需要 return 具有另一种类型的变量。我创建了两种方法。但我知道这不是一个好主意,因为代码重复。如何修复它并使用一种方法?
public ClassA doSomething(String aString, String bString, String cString, String dString) {
ClassA client;
ClientFactory clientFactory = new ClientFactoryImpl();
client = clientFactory.createNewClient(aString, bString);
ClassB conn = clientFactory.connect(cString, dString);
return client;
}
public ClassB doSomething(String aString, String bString, String cString, String dString) {
ClassA client;
ClientFactory clientFactory = new ClientFactoryImpl();
client = clientFactory.createNewClient(aString, bString);
ClassB conn = clientFactory.connect(cString, dString);
return conn;
}
一般情况下,当client和conn相互依赖时:
class SomethingResult {
ClassA client;
ClassB conn;
SomethingResult(ClassA client, ClassB conn) {
this.client = client;
this.conn = conn;
}
// Getters
}
public SomethingResult doSomething(String aString, String bString, String cString, String dString) {
// Create client and conn and execute any other logic
return new SomethingResult(client, conn);
}
然后,比如获取客户端:
ClassA client = doSomething(aString, bString, cString, dString).getClient();
在您的具体情况下,不需要这样做;你可以简单地做:
public ClassA getClient(String aString, String bString) {
ClientFactory clientFactory = new ClientFactoryImpl();
return clientFactory.createNewClient(aString, bString);
}
public ClassB getConn(String cString, String dString) {
ClientFactory clientFactory = new ClientFactoryImpl();
return clientFactory.connect(cString, dString);
}
只有一种方法。在一种情况下,我需要 return 一种类型的变量。在另一种情况下,我需要 return 具有另一种类型的变量。我创建了两种方法。但我知道这不是一个好主意,因为代码重复。如何修复它并使用一种方法?
public ClassA doSomething(String aString, String bString, String cString, String dString) {
ClassA client;
ClientFactory clientFactory = new ClientFactoryImpl();
client = clientFactory.createNewClient(aString, bString);
ClassB conn = clientFactory.connect(cString, dString);
return client;
}
public ClassB doSomething(String aString, String bString, String cString, String dString) {
ClassA client;
ClientFactory clientFactory = new ClientFactoryImpl();
client = clientFactory.createNewClient(aString, bString);
ClassB conn = clientFactory.connect(cString, dString);
return conn;
}
一般情况下,当client和conn相互依赖时:
class SomethingResult {
ClassA client;
ClassB conn;
SomethingResult(ClassA client, ClassB conn) {
this.client = client;
this.conn = conn;
}
// Getters
}
public SomethingResult doSomething(String aString, String bString, String cString, String dString) {
// Create client and conn and execute any other logic
return new SomethingResult(client, conn);
}
然后,比如获取客户端:
ClassA client = doSomething(aString, bString, cString, dString).getClient();
在您的具体情况下,不需要这样做;你可以简单地做:
public ClassA getClient(String aString, String bString) {
ClientFactory clientFactory = new ClientFactoryImpl();
return clientFactory.createNewClient(aString, bString);
}
public ClassB getConn(String cString, String dString) {
ClientFactory clientFactory = new ClientFactoryImpl();
return clientFactory.connect(cString, dString);
}