0

下面是别人在我们公司写的课。

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 类中复制粘贴所有内容,并通过更改集群 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.

如果我在这里遗漏了什么,请告诉我。

4

1 回答 1

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 构造函数中添加它,但您可以在创建读取器对象时传递 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;
       }

       //...
}
于 2012-11-09T21:10:50.257 回答