Back to slide -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'                patterns/composite/music-elements/PME.cs - The class ParallelMusicElement.Lecture 8 - slide 23 : 37
Program 5

using System;
using System.Collections.Generic;

public class ParallelMusicElement: MusicElement{
  private List<MusicElement> elements;
  
  public ParallelMusicElement(MusicElement[] elements){
    this.elements = new List<MusicElement>(elements);
  }

  // Factory method:
  public static MusicElement 
    MakeParallelMusicElement(params MusicElement[] elements){
      return new ParallelMusicElement(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 = startTime;
    }

    return result.ToArray();
  }

  public override int Duration{
    get{
      int result = 0;
   
      foreach(MusicElement me in elements){
        result = Math.Max(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 ParallelMusicElement(transposedElements.ToArray());
  }
}