essay on programming languages, computer science, information techonlogies and all.

Monday, April 29, 2019

Composition using Generic

Composition or inheritance. Composition is perferred way in OOP. Though there is drawback like repetitive code. With Generic in C#, repetitive code can be factored into a generic class.

First here is IPlayer, DrJekyll and BruceBanner.

public interface IPlayer
{
  stirng LiveIn { get; }
  string Play();
}

public class DrJekyll : IPlayer
{
  public string LiveIn { get { return "London"; } }
  public string Play() { return "Strolling around " + LiveIn + " cautiously"; }
}

public class BruceBanner : IPlayer
{
  public string LiveIn { get { return "New York"; } }
  public string Study { get { return "gamma ray"; } } 
  public string Play() { return "Playing with " + Study + " and trying not to get angry in" + LiveIn; }
}


And DrJekyll turns to MrHyde, BruceBanner to Hulk using inheritance. But inheritance ask to put 'virtual' on the original source code. It may not be possible.

public class DrJekyll : IPlayer
{
...
  public virtual string Play() ... 
}

public class MrHyde : DrJekyll 
{
  public override string Play() { return "Sneaking around " + LiveIn + " viciously"; }
}


public class BruceBanner : IPlayer
{
...
  public virtual string Play() ...
}

public class Hulk : BruceBanner 
{
  public override string Play() { return "Hate " + Study + " and rampage around " + LiveIn; }
}


Or composition can be used like below. MrHyde has OtherSelf DrJekyll and Hulk BruceBanner. Though notice the repetitive codes - OtherSelf, LiveIn.

public class MrHyde : IPlayer
{
  public DrJekyll OtherSelf { get; private set; } 
  public string LiveIn { get { return OtherSelf.LiveIn; } }

  public MrHyde( DrJekyll p ) { OtherSelf = p; }
  public string Play() { return "Sneaking around " + LiveIn + " viciously"; }
}

public class Hulk : IPlayer
{
  public BruceBanner OtherSelf { get; private set; } 
  public string LiveIn { get { return OtherSelf.LiveIn; } }

  public Hulk ( BruceBanner p ) { Self = p; }
  public string Play() { return "Hate " + OtherSelf.Study + " and rampage around " + LiveIn; }
}


The repetitive code can be bundled up to a generic class OtherSelf. Then MrHyde and Hulk only needs its own Play. And it can refer its Self for any property.

public abstract class OtherSelf< T > : IPlayer where T : IPlayer
{
  public T Self{ get; private set; }
  public PlayerBase( T otherSelf ) { Self = otherSelf; }
  public string LiveIn { return Self.LiveIn; }
  public abstract string Play();
}

public class MrHyde : OtherSelf< DrJekyll >
{
  public MrHyde( DrJekyll p ) : base( p ) {}
  public override string Play() { return "Sneaking around " + LiveIn + " viciously"; }
}

public class Hulk : OtherSelf< BruceBanner >
{
  public Hulk( BruceBanner p ) : base( p ) {}
  public override string Play() { return "Hate " + Self.Study + " and jumping around " + LiveIn; }
}
I didn't bother to check syntax on above code so please treat above as pseudo code.