java - bussiness logic in domain objects -


having class:

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 = 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;     (datapoint datapoint : datapoints.values()) {         count += datapoint.count();     }     return count; } 

could idea extract methods hit(int id) , getorcreate() other class service or helper. should modified in order receive parameter datapoint.

i think has sense include them datapointclass because represent actions dataperiodmust know.

there pattern scenario? in terms of concurrency extract or not extract these methods makes difference?

i think better leave methods in same model class if use data inside same class, or in other words, if don't depend on other classes.

of course never access services model class, difficult decide if model class should use model class. depends on whether relation tight (e.g. 1 contains other, , contained class never used independently). if 2 model classes not related 1 should not manage other; in case leave work third external class (like service or model class containing others).

in case, dataperiod contains datapoints (which model class) think it's fine leave methods there.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -