title: Object orientation and serialization
body: <p>Consider an interface like</p>
  
  <pre><code>IMyInterface = interface
    procedure DoSomethingRelevant;
    procedure Load (Stream : TStream);
    procedure Save (Stream : TStream);
  end;
  </code></pre>
  
  <p>and several classes that implement the interface:</p>
  
  <pre><code>TImplementingClass1 = class (TInterfacedObject, IMyInterface)
    ...
  end;
  TImplementingClass2 = class (TInterfacedObject, IMyInterface)
    ...
  end;
  ...
  </code></pre>
  
  <p>I have a class that has a list of IMyInterface implementors:</p>
  
  <pre><code>TMainClass = class
  strict private
    FItems : TList &lt;IMyInterface&gt;;
  public
    procedure LoadFromFile (const FileName : String);
    procedure SaveToFile (const FileName : String);
  end;
  </code></pre>
  
  <p>Now to the question: how can I load the main class and especially the item list in an object-oriented manner? Before I can call the virtual Load method for the items, I have to create them and thus have to know their type. In my current implementation I store the number of items and then for each item </p>
  
  <ul>
  <li>a type identifier (IMyInterface gets an additional GetID function)</li>
  <li>call the Save method of the item</li>
  </ul>
  
  <p>But that means that during loading I have to do something like</p>
  
  <pre><code>ID := Reader.ReadInteger;
  case ID of
    itClass1 : Item := TImplementingClass1.Create;
    itClass2 : Item := TImplementingClass2.Create;
    ...
  end;
  Item.Load (Stream);
  </code></pre>
  
  <p>But that doesn't seem to be very object-oriented since I have to fiddle with existing code every time I add a new implementor. Is there a better way to handle this situation?</p>