我有一个类结构:
public interface DBReader {
public Map<String, String> read(String primaryKey, String valueOfPrimaryKey,
boolean scanIndexForward, boolean consistentRead, int maxPageSize);
public int getA(String ___);
public int getB(String ___);
public int getC(String ___);
}
public class DynamoDBReader implements DBReader {
private DynamoDB dynamoDB;
private String tableName;
private Table table;
private int throughput;
private DynamoDBReader(Builder builder) {
this.throughput = builder.throughput;
this.tableName = builder.tableName;
this.dynamoDB = builder.dynamoDB;
this.table = dynamoDB.getTable(builder.tableName);
if (table == null) {
throw new InvalidParameterException(String.format("Table %s doesn't exist.", tableName));
}
}
@Override
public int getA(String ____) {
read(_________);
}
return ________;
}
@Override
public int getB(String ____) {
read(_________);
}
return ________;
}
@Override
public int getC(String ____) {
read(_________);
}
return ________;
}
@Override
public Map<String, String> read(String primaryKey, String valueOfPrimaryKey, boolean scanIndexForward,
boolean consistentRead, int maxPageSize) {
QuerySpec spec = new QuerySpec()
.withHashKey(primaryKey, valueOfPrimaryKey)
.withScanIndexForward(scanIndexForward)
.withConsistentRead(consistentRead)
.withMaxPageSize(maxPageSize);
ItemCollection<QueryOutcome> items = table.query(spec);
Iterator<Item> itemIterator = items.firstPage().iterator();
Map<String, String> itemValues = new HashMap<String, String>();
while (itemIterator.hasNext()) {
Item item = itemIterator.next();
}
return itemValues;
}
}
@VisibleForTesting
protected void setTable(Table table) {
this.table = table;
}
/**
* Returns a new builder.
*/
public static Builder builder() {
return new Builder();
}
public static class Builder {
private String tableName;
private int throughput;
private DynamoDB dynamoDB;
private Builder() { }
public Builder tableName(String tableName) {
this.tableName = tableName;
return this;
}
public Builder throughput(int throughput) {
this.throughput = throughput;
return this;
}
public Builder dynamoDB(DynamoDB dynamoDB) {
this.dynamoDB = dynamoDB;
return this;
}
public DynamoDBReader build() {
if (tableName == null) {
throw new InvalidParameterException("Table name can't be null.");
}
if (throughput <= 0) {
throw new InvalidParameterException("Throughput should be > 0.");
}
if (dynamoDB == null) {
throw new InvalidParameterException("dynamoDB can't be null.");
}
return new DynamoDBReader(this);
}
}
}
问题:getA(),getB(),getC()仅对特定的tableNames有效。对于表,getA()是有效的,但getB()和getC()没有任何意义。
如何将方法名称与表名联系起来,以便具有表名的人知道哪个函数有效。
为不同的getter创建子类的解决方案对我来说并不是一个好主意。
答案 0 :(得分:-1)
为不同的getter创建子类的解决方案对我来说并不是一个好主意。
你能详细说明原因吗?
我一直听到,“我不喜欢......”,“这看起来很难看......”,“它不应该这样做”。不喜欢特定解决方案的原因应该是客观原因,而不是个人意见。大多数时候,我们作为开发人员的直觉告诉我们,当它实际上违反某些软件开发原则时出现问题。但有时它只是普通的旧个人感觉,没有任何特别的逻辑推理。当发生这种情况时,我想了解细节。
您的解决方案违反了名为SRP的基本软件原则。
拥有table modules将是更好的解决方案。