Code:
public interface IFactory
{
IHuman CreateBaby(int age, double height, string hairColor, string eyeColor);
IHuman CreateToddler(int age, double height, string hairColor, string eyeColor);
IHuman CreateChild(int age, double height, string hairColor, string eyeColor);
IHuman CreateTeenager(int age, double height, string hairColor, string eyeColor);
}
public class HumanFactory : IFactory
{
public IHuman CreateBaby(int age, double height, string hairColor, string eyeColor)
{
return new Baby(age, height, hairColor,eyeColor);
}
public IHuman CreateToddler(int age, double height, string hairColor, string eyeColor)
{
return new Toddler(age, height, hairColor,eyeColor);
}
public IHuman CreateChild(int age, double height, string hairColor, string eyeColor)
{
throw new NotImplementedException();
}
public IHuman CreateTeenager(int age, double height, string hairColor, string eyeColor)
{
throw new NotImplementedException();
}
}
public interface IHuman
{
int Age { get; set; }
double Height { get; set; }
void AssignToFamily();
}
public abstract class HumanBase:IHuman
{
#region Interface members
public int Age { get; set; }
public double Height { get; set; }
public void AssignToFamily()
{
}
#endregion
#region base members
protected string HairColor { get; set; }
protected void BuyClothes(string destination)
{
}
#endregion
#region Abstract members
public abstract string EyeColor { get; set; }
public abstract void WearGlasses();
#endregion
#region Constructors
public HumanBase()
{ }
public HumanBase(int age, double height, string hairColor)
{
this.Age = age;
this.Height = height;
this.HairColor = hairColor;
}
#endregion
}
public class Baby : HumanBase,IHuman
{
public Baby(int age, double height, string hairColor, string eyeColor)
: base()
{
this.Age = age;
this.Height = height;
this.HairColor = hairColor;
this.EyeColor = eyeColor;
}
public override string EyeColor
{
get;
set;
}
public override void WearGlasses()
{
throw new NotImplementedException();
}
}
public class Toddler : HumanBase,IHuman
{
public Toddler(int age, double height, string hairColor, string eyeColor)
: base(age, height, hairColor)
{
//add normal Toddler stuff only
this.EyeColor = eyeColor;
}
public override string EyeColor
{
get;
set;
}
public override void WearGlasses()
{
throw new NotImplementedException();
}
}
public class Program
{
public static void Main()
{
IFactory humanFactory = new HumanFactory();
Baby clientBaby = humanFactory.CreateBaby(1, 20, "brown","blue") as Baby;
Toddler clientToddler = humanFactory.CreateToddler(3, 36, "blonde","blue") as Toddler;
/* I can access the IHuman properties and methods even though
* this is the concrete objects because both the concrete and
* the abstract base implement the same interface. But it is the
* abstract base that actually places it's implementation in
* one place
*/
int babyAge = clientBaby.Age;
double toddlerHeight = clientToddler.Height;
/*error here though because this is the abstract base class's property
* string hairColor = clientBaby.HairColor;
* The point here is only designate an abstract base class's property as
* abstract if you want every concrete class to override it so that it
* can be used here. But that means it will be a signature only abstract
* property or method.
*/
/*I can, however, access the abstracted properties and methods
*/
string babyEyeColor = clientBaby.EyeColor;
/*I can also treat them as objects of the interface type and
* leverage their properties and methods
*/
List<IHuman> humanEntities = new List<IHuman>()
{
clientBaby,
clientToddler
};
foreach (IHuman humanEntity in humanEntities)
{
Console.WriteLine("This human is {0} years old and is {1} inches tall.", humanEntity.Age, humanEntity.Height);
}
Console.ReadLine();
}
}