title: Passing in and returning custom data - are interfaces the right approach?
body: <p>I'm writing code in a C# library to do <a href="https://en.wikipedia.org/wiki/Cluster_analysis" rel="nofollow noreferrer">clustering</a> on a (two-dimensional) dataset - essentially breaking the data up into groups or clusters. To be useful, the library needs to take in "generic" or "custom" data, cluster it, and return the clustered data. </p>
  
  <p>To do this, I need to assume that each datum in the dataset being passed in has a 2D vector associated with it (in my case <code>Lat</code>, <code>Lng</code> - I'm working with co-ordinates). </p>
  
  <p>My first thought was to use generic types, and pass in two lists, one list of the generic data (i.e. <code>List&lt;T&gt;</code>) and another of the same length specifying the 2D vectors (i.e. <code>List&lt;Coordinate&gt;</code>, where <code>Coordinate</code> is my class for specifying a lat, lng pair), where the lists correspond to each other by index. But this is quite tedious because it means that in the algorithm I have to keep track of these indices somehow. </p>
  
  <p>My next thought was to use inferfaces, where I define an interface</p>
  
  <pre><code>public interface IPoint
  {
      double Lat { get; set; }
      double Lng { get; set; }
  }
  </code></pre>
  
  <p>and ensure that the data that I pass in implements this interface (i.e. I can assume that each datum passed in has a <code>Lat</code> and a <code>Lng</code>). </p>
  
  <p>But this isn't really working out for me either. I'm using my C# library to cluster stops in a transit network (in a different project). The class is called <code>Stop</code>, and this class is also from an external library, so I can't implement the interface for that class. </p>
  
  <p>What I did then was inherit from <code>Stop</code>, creating a class called <code>ClusterableStop</code>which looks like this: </p>
  
  <pre><code>public class ClusterableStop : GTFS.Entities.Stop, IPoint
  {        
  
      public ClusterableStop(Stop stop)
      {
          Id = stop.Id;
          Code = stop.Code;
          Name = stop.Name;
          Description = stop.Description;
          Latitude = stop.Latitude;
          Longitude = stop.Longitude;
          Zone = stop.Zone;
          Url = stop.Url;
          LocationType = stop.LocationType;
          ParentStation = stop.ParentStation;
          Timezone = stop.Timezone;
          WheelchairBoarding = stop.WheelchairBoarding;
      }
      public double Lat
      {
          get
          {
              return this.Latitude;
          }
      }
  
      public double Lng
      {
          get
          {
              return this.Longitude;
          }
      }
  }
  </code></pre>
  
  <p>which as you can see implements the <code>IPoint</code> interface. Now I use the constructor for <code>ClusterableStop</code> to first convert all <code>Stop</code>s in the dataset to <code>ClusterableStop</code>s, then run the algorithm and get the result as <code>ClusterableStop</code>s. </p>
  
  <p>This isn't really what I want, because I want to do things to the <code>Stop</code>s based on what cluster they fall in. I can't do that because I've actually instantiated <strong>new</strong> stops, namely <code>ClusterableStop</code>s !! </p>
  
  <p>I can still acheive what I want to, because e.g. I can retrieve the original objects by Id. But surely there is a much more elegant way to accomplish all of this? Is this the right way to be using interfaces? It seemed like such a simple idea - passing in and getting back custom data - but turned out to be so complicated. </p>