有这个课程:
$ ./test.sh
service = 'default'
node = 'default'
$ ./test.sh -s hello -n world
service = 'hello'
node = 'world'
$ ./test.sh -n world -s hello
service = 'hello'
node = 'world'
$ ./test.sh -e eh
./test.sh: illegal option -- e
将方法public class DataPeriod {
private final String key;
private final LocalDate from;
private final LocalDate to;
private final Map<LocalDate, DataPoint> dataPoints = new HashMap<LocalDate, DataPoint>();
public DataPeriod(String key, LocalDate from, LocalDate to) {
this.key = key;
this.from = from;
this.to = to;
}
public LocalDate getFrom() {
return from;
}
public LocalDate getTo() {
return to;
}
public void hit(int id) {
DataPoint dataPoint = getOrCreate();
dataPoint.hit(id);
}
private DataPoint getOrCreate() {
LocalDate now = LocalDate.now();
String dataPointKey = key + now.toString();
if ( !dataPoints.containsKey(now) ) {
DataPoint dataPoint = new DataPoint(dataPointKey);
dataPoints.put(now, dataPoint);
}
return dataPoints.get(dataPointKey);
}
public long count() {
long count = 0l;
for (DataPoint dataPoint : dataPoints.values()) {
count += dataPoint.count();
}
return count;
}
和hit(int id)
作为服务或帮助程序提取到其他类可能是一个好主意。但是应该修改它们以便接收getOrCreate()
的参数。
我认为将它们包含在DataPoint
类中是有道理的,因为它们代表了DataPoint
必须知道的行为。
这种情况有一些模式吗?在并发提取或不提取方面这些方法有何不同?
答案 0 :(得分:0)
这似乎是使用 动态属性 的经典案例。
如果您有一个可以从其他属性计算出的属性,则将其声明为 dynamic 属性,并为其编写自定义的属性处理程序。
例如-如果您有持久性属性(您存储在数据库中的属性),例如生日,则可以声明 dynamic 属性 age 并在运行时对其进行计算,而无需将其存储在数据库中。您将需要像AgeAttributeHandler.java
这样的自定义动态属性处理程序。
具有动态属性的另一个动机是所有模型豆 必须 仅具有 getters 和 setters 。 业务逻辑应保留在模型bean之外。
您的情况是,首先创建一个DynamicAttributeHandler.java
-
public interface DynamicAttributeHandler<VALUE, MODEL extends Object> {
VALUE get(MODEL var1);
void set(MODEL var1, VALUE var2);
}
然后,为count
和dataPoints
创建动态属性处理程序。
public class CountAttributeHandler implements DynamicAttributeHandler<Long, DatePeriod> {
@Override
public Long get(DatePeriod model) {
long count = 0l;
Map<LocalDate, DataPoint> dataPoints = model.getDataPoints();
for (DataPoint dataPoint : dataPoints.values()) {
count += dataPoint.count();
}
return count;
}
@Override
public void set(DatePeriod model, Long aLong) {
}
}
public class DataPointsAttributeHandler implements DynamicAttributeHandler<Map<LocalDate, DataPoint>, DatePeriod> {
@Override
public Map<LocalDate, DataPoint> get(DatePeriod model) {
LocalDate now = LocalDate.now();
String dataPointKey = model.getKey() + now.toString();
Map<LocalDate, DataPoint> dataPoints = model.getDataPoints();
if (!dataPoints.containsKey(now)) {
DataPoint dataPoint = new DataPoint(dataPointKey);
dataPoints.put(now, dataPoint);
}
return dataPoints;
}
@Override
public void set(DatePeriod model, Map<LocalDate, DataPoint> localDateDataPointMap) {
}
}
现在,仅使用 getters 和 setters 即可创建模型。
public class DatePeriod {
private final String key;
private final LocalDate from;
private final LocalDate to;
private final Map<LocalDate, DataPoint> dataPoints;
private final long count;
public DatePeriod(String key, LocalDate from, LocalDate to, Map<LocalDate, DataPoint> dataPoints, long count) {
this.key = key;
this.from = from;
this.to = to;
this.dataPoints = dataPoints;
this.count = count;
}
public LocalDate getFrom() {
return from;
}
public LocalDate getTo() {
return to;
}
public String getKey() {
return key;
}
public Map<LocalDate, DataPoint> getDataPoints() {
DynamicAttributeHandler<Map<LocalDate, DataPoint>, DatePeriod> dataPointsAttributeHandler = new DataPointsAttributeHandler();
return dataPointsAttributeHandler.get(this);
}
public long getCount() {
DynamicAttributeHandler<Long, DatePeriod> countAttributeHandler = new CountAttributeHandler();
return countAttributeHandler.get(this);
}
}