java - Is there a more elegant way of doing this? (Cannot be more descriptive.) -
i have 3 non-static classes representing musical composition. these score, part , note class.
score contains instance variable arraylist<part>
representing multiple instrument parts of score, , part contains instance variable arraylist<note>
representing note sequence.
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; resolution = resolution; } public double getduration() { int duration = (double) (getendstamp() - getstartstamp()) / resolution; return duration; } }
duration of each note calculated using score resolution. resolution of particular score instance passed through note construtor each time note instantiated. note added arraylist<note> notes
of corresponding part instance, , part added arraylist<part> parts
of score instance.
my solution of using int resolution
note constructor parameter doesn't seem elegant, since there many notes belonging same score, i.e. resolution attribute of score rather attribute of note.
is there way resolution referencing corresponding score object inside note class, instead of passing resolution through constructor of note class, or perhaps other solution?
it seems resolution pertains score (based on design), not note - why not change method signature of note#getduration calculate duration @ specific resolution:
public double getduration(int resolution) { double duration = (double) (getendstamp() - getstartstamp()) / resolution; return duration;
}
the same note can added different scores, different resolutions.
or better, why don't return:
public long getduration() { return getendstamp() - getstartstamp();
}
and let calling code deal whatever conversion needs do?
Comments
Post a Comment