以下是其他人在我们公司写的课程。
public class Reader1 extends PdsxAdapterBaseGemsReader {
@Override
public PdsxReader acquireReader(PdsxReadRequest pdsxReadRequest) {
return new PdsxUserReadOnlyGemsReader(pdsxReadRequest);
}
protected static class PdsxUserReadOnlyGemsReader extends PdsxAdapterBaseGemsReader.PdsxBaseGemsReader {
protected PdsxUserReadOnlyGemsReader(PdsxReadRequest pdsxReadRequest) {
super(pdsxReadRequest);
}
protected ClusterIdEnum getClusterId() {
return ClusterIdEnum.MARKEPLACE1;
}
@Override
public List<PdsxRecord> readData() {
List<UserCacheDoImpl> userDos = readUserCacheRecords(req, serverId,startLastModifiedDate, endLastModifiedDate);
// some code
}
private List<UserCacheDoImpl> readUserCacheRecords(PdsxReadRequest req, int serverId, Date startLastModifiedDate, Date endLastModifiedDate) {
List<UserCacheDoImpl> userDos = new ArrayList<UserCacheDoImpl>();
BackEndIterator userIter = new BackEndIterator(getClusterId().getId(), serverId, startLastModifiedDate, endLastModifiedDate, READ_BATCH_SIZE);
//some code
}
private PdsxRecord createPdsxRecordFromUserDO(UserCacheDoImpl userCache) {
//some code
}
private void xValidateAttrKey(PdsxAttrKey readAttr) {
// some code
}
}
}
问题陈述: -
我需要编写另一个具有相同功能的类,并使用所有方法名称。唯一的区别在于这种方法 -
protected ClusterIdEnum getClusterId() {
return ClusterIdEnum.MARKEPLACE1;
}
需要
protected ClusterIdEnum getClusterId() {
return ClusterIdEnum.MARKEPLACE2;
}
因此,如果我需要以非常老式的方式进行,我可以复制粘贴第一个Reader类中的所有内容,并通过更改cluster id方法创建一个新的Reader2类。就这样。但这不是一个好方法。
任何人都可以告诉我,在没有多次duplicate code
的情况下做同样事情的最佳方式是什么。?
我通过覆盖基类中的getLcusterId() method
来考虑这样的事情,代码就像这样 -
public class Reader2 extends Reader1 {
@Override
public PdsxReader acquireReader(PdsxReadRequest pdsxReadRequest) {
return new PdsxUserNewReadOnlyGemsReader(pdsxReadRequest);
}
protected static class PdsxUserNewReadOnlyGemsReader extends PdsxUserReadOnlyGemsReader {
protected PdsxUserNewReadOnlyGemsReader(PdsxReadRequest pdsxReadRequest) {
super(pdsxReadRequest);
}
@Override
protected ClusterIdEnum getClusterId() {
return ClusterIdEnum.MARKEPLACE2;
}
}
}
因为第一堂课的一切都是一样的所以我不需要在我的Reader2课程中实现任何东西吗?只是我可以覆盖getClusterId() method
。
如果我在这里遗漏任何东西,请告诉我。
答案 0 :(得分:0)
如果可以修改acquireReader
方法的签名,您可以在读者类中添加一个字段:
public class Reader1 extends PdsxAdapterBaseGemsReader {
@Override
public PdsxReader acquireReader(PdsxReadRequest pdsxReadRequest, ClusterIdEnum clusterId) {
return new PdsxUserReadOnlyGemsReader(pdsxReadRequest, clusterId);
}
protected static class PdsxUserReadOnlyGemsReader extends PdsxAdapterBaseGemsReader.PdsxBaseGemsReader {
private ClusterIdEnum clusterId;
protected PdsxUserReadOnlyGemsReader(PdsxReadRequest pdsxReadRequest, ClusterIdEnum clusterId) {
super(pdsxReadRequest);
this.clusterId = clusterId;
}
protected ClusterIdEnum getClusterId() {
return clusterId;
}
//...
}
或者,您可以在Reader1构造函数中添加它,但是您可以在创建reader对象时传递clusterId:
public class Reader1 extends PdsxAdapterBaseGemsReader {
private ClusterIdEnum clusterId;
public Reader1 (ClusterIdEnum clusterId) {
this.clusterId = clusterId;
}
@Override
public PdsxReader acquireReader(PdsxReadRequest pdsxReadRequest) {
return new PdsxUserReadOnlyGemsReader(pdsxReadRequest, clusterId);
}
protected static class PdsxUserReadOnlyGemsReader extends PdsxAdapterBaseGemsReader.PdsxBaseGemsReader {
private ClusterIdEnum clusterId;
protected PdsxUserReadOnlyGemsReader(PdsxReadRequest pdsxReadRequest, ClusterIdEnum clusterId) {
super(pdsxReadRequest);
this.clusterId = clusterId;
}
protected ClusterIdEnum getClusterId() {
return clusterId;
}
//...
}