mercredi 12 août 2015

How to not break the current code?

I'm having problem changing this little piece of codes to suits my situation. The codes:

class StuffFactory
{
  protected StuffFactory()
  {
    myDictionary.Add(Note4, SamsungStuff);
    myDictionary.Add(IPhone5, IPhoneStuff);
    myDictionary.Add(IPhone6, IPhoneStuff);
  }
}

internal class IPhoneStuff : PhoneStuff
{
  protected void DoWhatever1()  // There's like 4,5 more functions similar to this one
  {
    PortMonitor.UseHandler<IPhoneHandler>(  // and all of them have this use IPhoneHandler
      () =>
      {
        // yada yada
      });
  }

  protected void StreamThatVideo()
  {
    PortMonitor.UseHandler<IPhoneHandler>(
      () =>
      {
        // yada yada
      });
  }
}

internal class IPhoneHandler
{
  private void ActuallyDoTheStreaming()
  {
    // Actually does all the streaming stuffs
  }
}

There's a lot of stuffs inside the IPhoneStuff and IPhoneHandler. Problem I'm having is IPhone6 does streaming 99% similar to how the IPhone5 is doing - the only difference is only 3 lines of codes. How do I change the IPhoneHandler without breaking the current implementation of IPhoneHandler?

If I subclass IPhoneHandler, and just change ActuallyDoTheStreaming(), I have to determine the type the generic UseHandler() use dynamically - which is not possible in compile time (correct me if I'm wrong). I prefer not to use reflection, this code runs in embedded environment with multiple phones run at the same PC at one time. Other option is I just create my own PhoneStuff, but it'd be 99% similar to current codes, so a lot of duplicates...

Any design pattern I can use in this situation?

Mod: I don't know what title is suitable, feel free to change if you can find a better words.

Aucun commentaire:

Enregistrer un commentaire