patterns/composite/music-elements/SME.cs - The class SequentialMusicElement. | Lecture 8 - slide 23 : 37 Program 4 |
using System; using System.Collections.Generic; public class SequentialMusicElement: MusicElement{ private List<MusicElement> elements; public SequentialMusicElement(MusicElement[] elements){ this.elements = new List<MusicElement>(elements); } // Factory method: public static MusicElement MakeSequentialMusicElement(params MusicElement[] elements){ return new SequentialMusicElement(elements); } public override TimedNote[] Linearize(int startTime){ int time = startTime; List<TimedNote> result = new List<TimedNote>(); foreach(MusicElement me in elements){ result.AddRange(me.Linearize(time)); time = time + me.Duration; } return result.ToArray(); } public override int Duration{ get{ int result = 0; foreach(MusicElement me in elements){ result += me.Duration; } return result; } } public override MusicElement Transpose(int levels){ List<MusicElement> transposedElements = new List<MusicElement>(); foreach(MusicElement me in elements) transposedElements.Add(me.Transpose(levels)); return new SequentialMusicElement(transposedElements.ToArray()); } }