Is there a more elegant way to do this? (Could not be more descriptive.)

I have three non-static classes representing a musical composition. This is the class Score, Part and Note.

Score contains an ArrayList<Part> instance variable representing several parts of the tool, and Part contains an ArrayList<Note> instance variable representing a sequence of notes.

 public class Score { private ArrayList<Part> parts; private int resolution; public Score(int resolution) { parts = new ArrayList<Part>(); this.resolution = resolution; } public void addPart(Part part) { parts.add(part); } public ArrayList<Part> getParts() { return parts; } public int getResolution() { return resolution; } } public class Part { private ArrayList<Note> notes; public Part() { notes = new ArrayList<Note>(); } public void addNote(Note note) { notes.add(note); } public ArrayList<Note> getNotes() { return notes; } } public class Note() { private long startStamp; private long endStamp; private int resolution; public Note(long startStamp, long endStamp, int resolution) { this.startStamp = startStamp; this.endStamp = endStamp; this resolution = resolution; } public double getDuration() { int duration = (double) (getEndStamp() - getStartStamp()) / resolution; return duration; } } 

The duration of each note is calculated using the resolution resolution. The resolution of a particular instance of Score is passed through the Note construtor each time a note instance is created. A note is added to the ArrayList<Note> notes corresponding Part instance, and a part is added to the ArrayList<Part> parts Score instance.

My decision to use int resolution as a parameter of the Note constructor does not seem elegant, since there are many notes that belong to the same account, that is, the resolution is an evaluation attribute, not an attribute of a note.

Is there a way to get permission by referencing the corresponding Score object from the Note class, instead of passing the permission through the constructor of the Note class, or maybe some other solution?

+4
source share
3 answers

It seems that the resolution refers to Score (based on your design) and not to the note - why not change the signature of the Note # getDuration method to calculate the duration with a specific resolution:

 public double getDuration(int resolution) { double duration = (double) (getEndStamp() - getStartStamp()) / resolution; return duration; 

}

The same note can now be added to different ratings with different resolutions.

Or even better, why don't you just come back:

 public long getDuration() { return getEndStamp() - getStartStamp(); 

}

and let the calling code deal with any conversion that he needs to do?

+2
source

Either you combine them with density by skipping Score in the Note constructor, or simply create a function: getDuration (Node n) in the Score itself. Thus, only Score will know about the resolution, which seems more correct. (Assuming resolution is really a Score property. I'm generally disliked by music :()

Edit:

you could be more visual, for example, with this headline: Yes, Beethoven was dumb, but what if he was also a Java programmer? :)

0
source

When do you need the duration you need to calculate? I personally try to avoid the use of any logic in my domain objects and have a service designed for the duration of the recording beyond all three objects, where you could refer to the rating and the note. Perhaps instead of having the startStamp and endStamp properties, you can have the startStamp and duration attributes and calculate the duration of the creation of the note object.

 public class Note() { private long startStamp; private long duration; public Note(long startStamp, long duration) { this.startStamp = startStamp; this.duration = duration; } public double getDuration() { return duration; } } 
0
source

All Articles